<?php
/*=========================================================================*/
/* Name: Example5.php                                                      */
/* Uses: Clipping example                                                  */
/* Date: 12/30/2007                                                        */
/* Author: Andrew Que (http://www.DrQue.net/)                              */
/* Revisions:                                                              */
/*  1.0 - 12/31/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     500;
   
$imageHeight    350;

   
// Margins
   
$leftMargin     20;
   
$rightMargin    45;
   
$topMargin      25;
   
$bottomMargin   20;

   
// vertical scale
   
$yMajorScale   0.5;
   
$yMinorScale   $yMajorScale 5;
   
$xMajorScale   0.01;
   
$xMinorScale   $xMajorScale 10;
   
//-----------------------------------------------------------

   // 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 );

   
// 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);

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

   
// Load data points from file
   
$data file__DIR__ "/scatterData_Slope0_50.csv" );

   
// 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 );
   }

   
// Automatically adjust vertical scale
   
$xyPlot->setX_Span00.05 );
   
$xyPlot->setY_Span( -1);

   
//------------------------------------------------------
   // 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();

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

   // Setup timeframe
   
$xyPlot->setMaxX_Distance60 60 24 );

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

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

   
//----------------------------------
   // Center a title above the chart
   //----------------------------------
   
$title "Clipping Example";
   
$fontSize 5;

   
// Calculate the horizontal position to center text
   
$x round$imageWidth ) + getCenterBias$title$fontSize );

   
// Place text
   
imageString(
     
$image,
     
5,
     
$x$fontSize,
     
$title,
     
$colorMap"Black" ] );

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

   exit;

//---------------------------------------------------------------------------
// 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;
}

//---------------------------------------------------------------------------
// 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" ] );
}

?>