<?php
/*=========================================================================*/
/* Name: Example1.php                                                      */
/* Uses: Example of creating a simple chart                                */
/* Date: 12/28/2007                                                        */
/* Author: Andrew Que (http://www.DrQue.net/)                              */
/* Revisions:                                                              */
/*  1.0 - 12/28/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     15;
   
$rightMargin    40;
   
$topMargin      25;
   
$bottomMargin   35;

   
// vertical scale
   
$yMajorScale   10;
   
$yMinorScale   $yMajorScale 5;
   
//-----------------------------------------------------------

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

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

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

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

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

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

   
// Automatically adjust vertical scale
   
$xyPlot->AutoScaleX_MinMax();
   
$xyPlot->AutoScaleY_MinMax$yMajorScale );

   
//----------------------------------
   // Rescale horizontal axis to even months
   //----------------------------------

   // Find lowest X value
   
$value $xyPlot->FindX_Min();

   
// Round lowest horizontal point to first of month
   
$xMin mktime(
       
000,
       
date"m"$value ),
       
1,
       
date"Y"$value )
     );

   
// Find highest X value
   
$value $xyPlot->FindX_Max();

   
// Round highest horizontal point to the next highest month
   
$xMax mktime(
       
000,
       
date"m"$value ) + 1,
       
1,
       
date"Y"$value )
     );

   
// Set new horizontal scale
   
$xyPlot->setX_Span$xMin$xMax );

   
$xyPlot->setAverageWidth);

   
//------------------------------------------------------
   // 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_MinorDivisionColor$colorMap"Cyan" ] );
   
$xyPlot->setX_MinorCustomIncrement"customMinorIncFunction" );
   
$xyPlot->setX_MinorCustomStart$xMin );
   
$xyPlot->drawX_MinorDivisions();

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

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

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

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

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

   
// Custom callback for text
   
$xyPlot->setX_MajorTextCallback"horizontalStringCallback" );

   
// Custom callback for incrementing major scale
   // (This is for incrementing scale by months with Unix dates)
   
$xyPlot->setX_MajorCustomIncrement"customMajorIncFunction" );

   
// Where to start with labels
   
$xyPlot->setX_MajorCustomStart$xMin );

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

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

   
// Custom callback to add dollar signs
   
$xyPlot->setY_MajorTextCallback"verticalTextScale" );

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

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

   // Setup time frame
   
$xyPlot->setMaxX_Distance60 60 24 );

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

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

   
//----------------------------------
   // Center a title above the chart
   //----------------------------------
   
$title "750 GB Drive Prices";
   
$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;

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

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

//---------------------------------------------------------------------------
// Horizontal string callback
// This function places the text on the chart itself.  The x/y location of
// the division is passed so the text can be drawn.  By returning nothing,
// the calling function doesn't places any text itself
//---------------------------------------------------------------------------
function horizontalStringCallback$value$x$y )
{
   global 
$colorMap;
   global 
$image;

   
$string date"M", (int)$value );
   
$fontSize 2;

   
// Center the text
   
$centerBias getCenterBias$string$fontSize );

   
// Price for this division
   
imageString(
     
$image,
     
$fontSize,
     
$x $centerBias$y,
     
$string,
     
$colorMap"Black" ] );

   
$y += imageFontHeight);
   
$string date"y", (int)$value );

   
// Center the text
   
$centerBias getCenterBias$string$fontSize );

   
// Price for this division
   
imageString(
     
$image,
     
$fontSize,
     
$x $centerBias$y,
     
$string,
     
$colorMap"Black" ] );

   
// Return nothing as this function has produced the text
   
return "";
}

//---------------------------------------------------------------------------
// Custom minor increment function
// Incroment by 10 days, skipping day 30 of th month
//---------------------------------------------------------------------------
function customMinorIncFunction$value )
{
  
// Next day
  
$NewDay date"d"$value ) + 10;

  
// Too close to end of month?
  
if ( $NewDay 25 )
    
// Next increment is first of month
    
$value mktime(
        
000,
        
date"m"$value ) + 1,
        
1,
        
date"Y"$value )
      );
  else
    
// Next increment is 10 days
    
$value mktime(
        
000,
        
date"m"$value ),
        
$NewDay,
        
date"Y"$value )
      );

  return 
$value;
}

//---------------------------------------------------------------------------
// Custom major increment function
// Increment by month
//---------------------------------------------------------------------------
function customMajorIncFunction$value )
{
  
$value mktime(
      
000,
      
date"m"$value ) + 1,
      
date"d"$value ),
      
date"Y"$value )
    );

  return 
$value;
}

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

?>