<?php
/*=========================================================================*/
/* Name: Example4.php                                                      */
/* Uses:                                                                   */
/* Date: 12/30/2007                                                        */
/* Author: Andrew Que (http://www.DrQue.net/)                              */
/* Revisions:                                                              */
/*  1.0 - 12/30/2007 - QUE - Creation                                      */
/*                                                                         */
/* ----------------------------------------------------------------------- */
/*                                                                         */
/* This program is free software: you can redistribute it and/or modify    */
/* it under the terms of the GNU General Public License as published by    */
/* the Free Software Foundation, either version 3 of the License, or       */
/* (at your option) any later version.                                     */
/*                                                                         */
/* This program is distributed in the hope that it will be useful,         */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of          */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           */
/* GNU General Public License for more details.                            */
/*                                                                         */
/* You should have received a copy of the GNU General Public License       */
/* along with this program.  If not, see <http://www.gnu.org/licenses/>.   */
/*                                                                         */
/* ----------------------------------------------------------------------- */
/*                                                                         */
/*                           (C) Copyright 2007                            */
/*                               Andrew Que                                */
/*                                   ≡|>                                   */
/*=========================================================================*/

   // Include the X/Y Plot library
   
require_once( __DIR__ "/../vendor/xyPlot/xyPlot.php" );

   
//-----------------------------------------------------------

   // Size of the image
   
$imageWidth     600;
   
$imageHeight    350;

   
// Margins
   
$leftMargin     15;
   
$rightMargin    135;
   
$topMargin      25;
   
$bottomMargin   20;

   
// vertical scale
   
$yMajorScale   0.5;
   
$yMinorScale   $yMajorScale 5.0;
   
$xMajorScale   1.0 10.0;
   
$xMinorScale   $xMajorScale 5.0;
   
//-----------------------------------------------------------

   // Create image
   
$image = @imageCreate$imageWidth$imageHeight )
       or die( 
"Cannot Initialize new GD image stream" );

   
//---------------------------------
   // Create basic color map
   //---------------------------------
   
$colorMap = array();

   
$colorMap"Background" ] = imageColorAllocate$image255255255 );

   
// Create a standard color palette
   
$colorMap"Black"       ] = imageColorAllocate$image,   0,   0,   );
   
$colorMap"Red"         ] = imageColorAllocate$image192,   0,   );
   
$colorMap"Green"       ] = imageColorAllocate$image,   0192,   );
   
$colorMap"Blue"        ] = imageColorAllocate$image,   0,   0192 );
   
$colorMap"Brown"       ] = imageColorAllocate$image,  48,  48,   );
   
$colorMap"Cyan"        ] = imageColorAllocate$image,   0192192 );
   
$colorMap"Purple"      ] = imageColorAllocate$image192,   0192 );
   
$colorMap"LightGray"   ] = imageColorAllocate$image192192192 );

   
$colorMap"DarkGray"    ] = imageColorAllocate$image,  48,  48,  48 );
   
$colorMap"LightRed"    ] = imageColorAllocate$image255,   0,   );
   
$colorMap"LightGreen"  ] = imageColorAllocate$image,   0255,   );
   
$colorMap"LightBlue"   ] = imageColorAllocate$image,   0,   0255 );
   
$colorMap"Yellow"      ] = imageColorAllocate$image255255,   );
   
$colorMap"LightCyan"   ] = imageColorAllocate$image,   0255255 );
   
$colorMap"LightPurple" ] = imageColorAllocate$image255,   0255 );
   
$colorMap"White"       ] = imageColorAllocate$image255255255 );

   
$colorMap"Gray10"      ] = imageColorAllocate$image,  26,  26,  26 );
   
$colorMap"Gray20"      ] = imageColorAllocate$image,  51,  51,  51 );
   
$colorMap"Gray30"      ] = imageColorAllocate$image,  77,  77,  77 );
   
$colorMap"Gray40"      ] = imageColorAllocate$image102102102 );
   
$colorMap"Gray50"      ] = imageColorAllocate$image128128128 );
   
$colorMap"Gray60"      ] = imageColorAllocate$image154154154 );
   
$colorMap"Gray70"      ] = imageColorAllocate$image180180180 );
   
$colorMap"Gray80"      ] = imageColorAllocate$image205205205 );
   
$colorMap"Gray90"      ] = imageColorAllocate$image230230230 );

   
$colorMap"DarkYellow"  ] = imageColorAllocate$image128128,   );

   
// New plot
   
$xyPlot = new XY_Plot$image );

   
// Setup boundaries
   
$xyPlot->sizeWindow(
     
$leftMargin$topMargin,
     
$imageWidth $rightMargin$imageHeight $bottomMargin );

   
// Set plot colors
   
$xyPlot->setColor$colorMap"LightRed" ] );

   
// Set linear regression color
   
$xyPlot->setAverageColor$colorMap"LightPurple" ] );
   
$xyPlot->setLinearRegressionColor$colorMap"LightBlue" ] );

   
// Average line size
   
$xyPlot->setAverageWidth);

   
// Linear regression thickness
   
$xyPlot->setLinearRegressionThickness);

   
// Set point size
   
$xyPlot->setCircleSize);

   
// Automatically adjust vertical scale
   
$xyPlot->setX_Span0.0 1.0 );
   
$xyPlot->setY_Span( -0.52.5 );

   
//------------------------------------------------------
   // Draw grids and labels
   // NOTE: Always draw minor grids first so the major
   // grids are on top
   //------------------------------------------------------

   // Setup and draw minor horizontal scale (right to left)
   
$xyPlot->setX_MinorDivisionScale$xMinorScale );
   
$xyPlot->setX_MinorDivisionColor$colorMap"Gray90" ] );
   
$xyPlot->drawX_MinorDivisions();

   
// Setup and draw minor vertical scale (top to bottom)
   
$xyPlot->setY_MinorDivisionScale$yMinorScale );
   
$xyPlot->setY_MinorDivisionColor$colorMap"Gray90" ] );
   
$xyPlot->drawY_MinorDivisions();

   
//----------------------------------
   // Setup and draw major horizontal scale (right to left)
   //----------------------------------

   // Extend lines 5 pixels past margins for label
   
$xyPlot->setX_MajorDivisionExtension);

   
// Scale
   
$xyPlot->setX_MajorDivisionScale$xMajorScale );

   
// Division lines are blue
   
$xyPlot->setX_MajorDivisionColor$colorMap"Gray70" ] );

   
// Text labels are dark gray
   
$xyPlot->setX_MajorDivisionTextColor$colorMap"Black" ] );

   
// Scale callback
   
$xyPlot->setX_MajorTextCallback"TextScale" );

   
// Draw it
   
$xyPlot->drawX_MajorDivisions();

   
//----------------------------------
   // Setup and draw major vertical scale (top to bottom)
   //----------------------------------

   // Extend lines 5 pixels past margins for label
   
$xyPlot->setY_MajorDivisionExtension);

   
// Scale
   
$xyPlot->setY_MajorDivisionScale$yMajorScale );

   
// Divisions in dark gray
   
$xyPlot->setY_MajorDivisionColor$colorMap"Gray70" ] );

   
// labels in dark gray
   
$xyPlot->setY_MajorDivisionTextColor$colorMap"Black" ] );

   
// Scale callback
   
$xyPlot->setY_MajorTextCallback"TextScale" );

   
// Draw it
   
$xyPlot->drawY_MajorDivisions();

   
//----------------------------------

   
$textAreaLeft  $imageWidth 100;
   
$textAreaRight $imageWidth 8;

   
$textArea =
     new 
Window(
       
$textAreaLeft,
       
6,
       
$textAreaRight,
       
$imageHeight 8
     
);

   
$textOffset_X = ( $textArea->GetWidth() / ) + $textAreaLeft;
   
$textOffset_Y 8;

   
imageRectangle(
     
$image,
     
$textArea->GetX(),
     
$textArea->GetY(),
     
$textArea->GetXX(),
     
$textArea->GetYY(),
     
$colorMap"Black" ] );

   
//----------------------------------

   
$xyPlot->setColor$colorMap"Red" ] );
   
$xyPlot->setLinearRegressionColor$colorMap"LightRed" ] );
   
drawGraph__DIR__ "/scatterData_Slope1.4_25.csv" );

   
$xyPlot->setColor$colorMap"Green" ] );
   
$xyPlot->setLinearRegressionColor$colorMap"LightGreen" ] );
   
drawGraph__DIR__ "/scatterData_Slope1.2_25.csv" );

   
$xyPlot->setColor$colorMap"Blue" ] );
   
$xyPlot->setLinearRegressionColor$colorMap"LightCyan" ] );
   
drawGraph__DIR__ "/scatterData_Slope0.75_25.csv" );


   
// Draw borders
   
drawBorders(
     
$leftMargin$topMargin,
     
$rightMargin$bottomMargin );

   
//----------------------------------
   
$x  $imageWidth;
   
$x -= $leftMargin $rightMargin;
   
$x /= 2;
   
$x += $leftMargin;

   
$fontSize 5;

   
printCenteredText(
     
$x,
     
5,
     
"Multiple Scatter Graph",
     
$fontSize,
     
$colorMap"Black" ] );

   
// Output image
   
header"Content-Type: image/png" );
   
imagePNG$image );

   exit;

function 
drawGraph$fileName )
{
   global 
$xyPlot;
   global 
$textOffset_Y;
   global 
$textOffset_X;
   global 
$textArea;
   global 
$colorMap;

   
// Load data points from file
   
$data file$fileName );

   
$xyPlot->resetData();

   
// For each data point
   
foreach ( $data as $dataPoint )
   {
     
// Split line into X/Y componets
      
list( $x$y ) = explode","trim$dataPoint ) );

     
// Add data to plot
     
$xyPlot->addData$x$y );
   }

   
// Render the points and lines to image
   
$xyPlot->renderPoints();
   
$xyPlot->renderLinearRegression();

   
//----------------------------------
   // Print chart data
   //----------------------------------

   // Slope
   
writeText(
     
$textOffset_X$textOffset_Y"Slope",
     
number_format$xyPlot->GetSlope(), ) );

   
// Y-Intercept
   
writeText(
     
$textOffset_X$textOffset_Y"Y-Intercept",
     
number_format$xyPlot->GetY_Intercept(), ) );

   
// Correlation
   
writeText(
     
$textOffset_X$textOffset_Y"Correlation",
     
number_format$xyPlot->GetCorrelation() * 100) . "%" );

   
$textOffset_Y += imageFontHeight);
}

//---------------------------------------------------------------------------
// Write a label with a value in the status column
//---------------------------------------------------------------------------
function writeText$x, &$y$label$value )
{
   global 
$colorMap;

   
$labelFontSize 3;
   
$valueFontSize 2;

   
// Label
   
printCenteredText(
     
$x$y,
     
$label,
     
$labelFontSize,
     
$colorMap"Black" ] );

   
// Next
   
$y += imageFontHeight$labelFontSize ) + 2;

   
// Value
   
printCenteredText(
     
$x$y,
     
$value,
     
$valueFontSize,
     
$colorMap"Black" ] );

   
// Next
   
$y += ( imageFontHeight$valueFontSize ) * 1.5 );
}

//---------------------------------------------------------------------------
// Custom vertical text-- just return value with dollar sign in front
//---------------------------------------------------------------------------
function TextScale$value )
{
   return 
number_format$value);
}

//---------------------------------------------------------------------------
// Calculate the center bias of a graphics string
// Returns a negative number to be added to a point from with to center
//---------------------------------------------------------------------------
function getCenterBias$string$fontSize )
{
   
$centerBias  strlen$string );
   
$centerBias *= imageFontWidth$fontSize );
   
$centerBias /= 2;
   
$centerBias  = -round$centerBias );

   return 
$centerBias;
}

//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
function printCenteredText$x$y$string$fontSize$color )
{
   global 
$image;

   
// Calculate the horizontal position to center text
   
$x += getCenterBias$string$fontSize );

   
// Place text
   
imageString(
     
$image,
     
$fontSize,
     
intval$x ),
     
intval$y ),
     
$string,
     
$color );
}

//---------------------------------------------------------------------------
// Simple function to draw a border around a box
//---------------------------------------------------------------------------
function drawBorder$image$x$y$xx$yy$color )
{
   
imageLine$image,  $x,  $y$xx,  $y$color );
   
imageLine$image,  $x,  $y,  $x$yy$color );
   
imageLine$image$xx,  $y$xx$yy$color );
   
imageLine$image,  $x$yy$xx$yy$color );
}

//---------------------------------------------------------------------------
// Draw borders around the outside of the image and the chart
//---------------------------------------------------------------------------
function drawBorders(
  
$leftMargin$topMargin$rightMargin$bottomMargin )
{
   global 
$image;
   global 
$imageWidth;
   global 
$imageHeight;
   global 
$colorMap;

   
// Draw borders around chart area
   
drawBorder(
     
$image,
     
$leftMargin,
     
$topMargin,
     
$imageWidth $rightMargin,
     
$imageHeight $bottomMargin,
     
$colorMap"Black" ] );

   
// Draw borders around entire image
   
drawBorder(
     
$image,
     
0,
     
0,
     
$imageWidth 1,
     
$imageHeight 1,
     
$colorMap"Black" ] );
}

?>