<?php
// Load the least-square regression class.
require_once( 'RootDirectory.inc.php' );
require_once( $RootDirectory . 'Includes/XY_Plot/XY_Plot.php' );
function plot( $data, $regression, $coefficients, $functionString, $isScale = false )
{
//-----------------------------------------------------------
$imageWidth = 450;
$imageHeight = 200;
if ( $isScale )
{
$leftMargin = 8;
$rightMargin = 25;
$topMargin = 8;
$bottomMargin = 35;
}
else
{
$leftMargin = 2;
$rightMargin = 2;
$topMargin = 2;
$bottomMargin = 15;
}
// Verticle scale
$Y_MajorScale = 1;
$Y_MinorScale = $Y_MajorScale / 5;
$X_MajorScale = 1;
$X_MinorScale = $X_MajorScale / 5;
//-----------------------------------------------------------
// Create image
$image = imageCreate( $imageWidth , $imageHeight );
//---------------------------------
// Create basic color map
//---------------------------------
$colorMap = array();
$colorMap[ "Background" ] = imageColorAllocate( $image , 255 , 255 , 255 );
//imagecolortransparent( $image , $colorMap[ "Background" ] );
// Create a standard color palette
$colorMap[ "Black" ] = imagecolorallocate( $image, 0, 0, 0 );
$colorMap[ "Red" ] = imagecolorallocate( $image, 192, 0, 0 );
$colorMap[ "Green" ] = imagecolorallocate( $image, 0, 192, 0 );
$colorMap[ "Blue" ] = imagecolorallocate( $image, 0, 0, 192 );
$colorMap[ "Brown" ] = imagecolorallocate( $image, 48, 48, 0 );
$colorMap[ "Cyan" ] = imagecolorallocate( $image, 0, 192, 192 );
$colorMap[ "Purple" ] = imagecolorallocate( $image, 192, 0, 192 );
$colorMap[ "LightGray" ] = imagecolorallocate( $image, 192, 192, 192 );
$colorMap[ "DarkGray" ] = imagecolorallocate( $image, 48, 48, 48 );
$colorMap[ "LightRed" ] = imagecolorallocate( $image, 255, 0, 0 );
$colorMap[ "LightGreen" ] = imagecolorallocate( $image, 0, 255, 0 );
$colorMap[ "LightBlue" ] = imagecolorallocate( $image, 0, 0, 255 );
$colorMap[ "Yellow" ] = imagecolorallocate( $image, 255, 255, 0 );
$colorMap[ "LightCyan" ] = imagecolorallocate( $image, 0, 255, 255 );
$colorMap[ "LightPurple" ] = imagecolorallocate( $image, 255, 0, 255 );
$colorMap[ "White" ] = imagecolorallocate( $image, 255, 255, 255 );
$colorMap[ "Gray10" ] = imagecolorallocate( $image , 26 , 26 , 26 );
$colorMap[ "Gray20" ] = imagecolorallocate( $image , 51 , 51 , 51 );
$colorMap[ "Gray30" ] = imagecolorallocate( $image , 77 , 77 , 77 );
$colorMap[ "Gray40" ] = imagecolorallocate( $image , 102 , 102 , 102 );
$colorMap[ "Gray50" ] = imagecolorallocate( $image , 128 , 128 , 128 );
$colorMap[ "Gray60" ] = imagecolorallocate( $image , 154 , 154 , 154 );
$colorMap[ "Gray70" ] = imagecolorallocate( $image , 180 , 180 , 180 );
$colorMap[ "Gray80" ] = imagecolorallocate( $image , 205 , 205 , 205 );
$colorMap[ "Gray90" ] = imagecolorallocate( $image , 230 , 230 , 230 );
// Place text
imageString
(
$image,
2,
$leftMargin,
$imageHeight - 15,
$functionString,
$colorMap[ "Black" ]
);
// New plot
$plot = new XY_PlotClass( $image );
// Setup boundaries
$plot->sizeWindow
(
$leftMargin,
$topMargin,
$imageWidth - $rightMargin,
$imageHeight - $bottomMargin
);
// Draw border around graph area.
imageRectangle
(
$image,
$leftMargin,
$topMargin,
$imageWidth - $rightMargin,
$imageHeight - $bottomMargin,
$colorMap[ "LightGray" ]
);
foreach ( $data as $point )
$plot->addData( $point[ 0 ], $point[ 1 ] );
$plot->setColor( $colorMap[ "Red" ] );
$plot->autoScaleX_MinMax( 0 );
$plot->autoScaleY_MinMax( 1 );
if ( $isScale )
{
// Setup and draw minor horizontal scale (right to left)
$plot->SetX_MinorDivisionScale( $X_MinorScale );
$plot->SetX_MinorDivisionColor( $colorMap[ "Gray90" ] );
$plot->DrawX_MinorDivisions();
// Setup and draw minor vertical scale (top to bottom)
$plot->SetY_MinorDivisionScale( $Y_MinorScale );
$plot->SetY_MinorDivisionColor( $colorMap[ "Gray90" ] );
$plot->DrawY_MinorDivisions();
//----------------------------------
// Setup and draw major horizontal scale (right to left)
//----------------------------------
// Extend lines 5 pixels past margins for label
$plot->SetX_MajorDivisionExtention( 5 );
// Scale
$plot->SetX_MajorDivisionScale( $X_MajorScale );
// Division lines are blue
$plot->SetX_MajorDivisionColor( $colorMap[ "Gray70" ] );
// Text labels are dark gray
$plot->SetX_MajorDivisionTextColor( $colorMap[ "Black" ] );
// Draw it
$plot->DrawX_MajorDivisions();
//----------------------------------
// Setup and draw major vertical scale (top to bottom)
//----------------------------------
// Extend lines 5 pixels past margins for label
$plot->SetY_MajorDivisionExtention( 5 );
// Scale
$plot->SetY_MajorDivisionScale( $Y_MajorScale );
// Divisions in drak gray
$plot->SetY_MajorDivisionColor( $colorMap[ "Gray70" ] );
// Lables in dark gray
$plot->SetY_MajorDivisionTextColor( $colorMap[ "Black" ] );
// Draw it
$plot->DrawY_MajorDivisions();
}
$min = $plot->getX_Min();
$max = $plot->getX_Max();
// Render the points to image
$plot->setCircleSize( 8 );
$plot->renderPoints();
$plot->resetData();
$frameWidth = $imageWidth - $leftMargin - $rightMargin;
for ( $xIndex = 0; $xIndex < $frameWidth; ++$xIndex )
{
$x = $xIndex * ( $max - $min ) / $frameWidth + $min;
$y = $regression->getFunction( $x, $coefficients );
$plot->addData( $x, $y );
}
$plot->setColor( $colorMap[ "LightRed" ] );
$plot->renderWithLines();
// Output image
header( "Content-Type: image/png" );
imagePNG( $image );
}
// //-----------------------------------------------------------------------------
// // This function is here until PHP 5.5 is released. Alas, it's not here yet.
// //-----------------------------------------------------------------------------
// function array_column( $data, $column )
// {
// $result = array();
// foreach ( $data as $row )
// array_push( $result, $row[ $column ] );
//
// return $result;
// }
?>