<?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( 'RootDirectory.inc.php' );
   require_once( 
$RootDirectory "Includes/XY_Plot/XY_Plot.php" );

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

   // Size of the image
   
$ImageWidth     500;
   
$ImageHeight    350;

   
// Margins
   
$LeftMargin     15;
   
$RightMargin    40;
   
$TopMargin      25;
   
$BottomMargin   35;

   
// Verticle scale
   
$Y_MajorScale   10;
   
$Y_MinorScale   $Y_MajorScale 5;
   
//-----------------------------------------------------------

   // Create image
   
$Image = @imagecreate$ImageWidth $ImageHeight )
       or die( 
"Cannot Initialize new GD image stream" );

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

   
$ColorMap"Background" ] = imagecolorallocate$Image 255 255 255 );
   
   
// Create a standard color palette
   
$ColorMap"Black"       ] = imagecolorallocate$Image ,   ,   ,   );
   
$ColorMap"Red"         ] = imagecolorallocate$Image 192 ,   ,   );
   
$ColorMap"Green"       ] = imagecolorallocate$Image ,   192 ,   );
   
$ColorMap"Blue"        ] = imagecolorallocate$Image ,   ,   192 );
   
$ColorMap"Brown"       ] = imagecolorallocate$Image ,  48 ,  48 ,   );
   
$ColorMap"Cyan"        ] = imagecolorallocate$Image ,   192 192 );
   
$ColorMap"Purple"      ] = imagecolorallocate$Image 192 ,   192 );
   
$ColorMap"LightGray"   ] = imagecolorallocate$Image 192 192 192 );

   
$ColorMap"DarkGray"    ] = imagecolorallocate$Image ,  48 ,  48 ,  48 );
   
$ColorMap"LightRed"    ] = imagecolorallocate$Image 255 ,   ,   );
   
$ColorMap"LightGreen"  ] = imagecolorallocate$Image ,   255 ,   );
   
$ColorMap"LightBlue"   ] = imagecolorallocate$Image ,   ,   255 );
   
$ColorMap"Yellow"      ] = imagecolorallocate$Image 255 255 ,   );
   
$ColorMap"LightCyan"   ] = imagecolorallocate$Image ,   255 255 );
   
$ColorMap"LightPurple" ] = imagecolorallocate$Image 255 ,   255 );
   
$ColorMap"White"       ] = imagecolorallocate$Image 255 255 255 );

   
// New plot
   
$XY_Plot = new XY_PlotClass$Image );
   
   
// Setup boundries
   
$XY_Plot->SizeWindow
     
$LeftMargin $TopMargin 
     
$ImageWidth $RightMargin $ImageHeight $BottomMargin );
   
   
// Set plot colors
   
$XY_Plot->SetColor$ColorMap"LightRed" ] );
   
   
// Set linear regression color
   
$XY_Plot->SetAverageColor$ColorMap"LightPurple" ] );
   
$XY_Plot->SetLinearRegressionColor$ColorMap"LightBlue" ] );
   
   
// Set point size
   
$XY_Plot->SetCircleSize);

   
// Load data points from file
   
$Data file$RootDirectory "Examples/Example1Data.csv" );

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

     
// Add data to plot
     
$XY_Plot->AddData$x $y );
   }

   
// Automaticlly adjust verticle scale
   
$XY_Plot->AutoScaleX_MinMax();
   
$XY_Plot->AutoScaleY_MinMax$Y_MajorScale );

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

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

   
// Round lowest horizontal point to first of month
   
$X_Min mktime(
       
,
       
date"m" $Value ) ,
       
,
       
date"Y" $Value )
     );

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

   
// Round highest horizontal point to the next highest month
   
$X_Max mktime(
       
,
       
date"m" $Value ) + ,
       
,
       
date"Y" $Value )
     );

   
// Set new horizontal scale
   
$XY_Plot->SetX_Span$X_Min $X_Max );

   
$XY_Plot->SetAverageWidth);

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

   // Setup and draw minor horizontal scale (right to left)
   
$XY_Plot->SetX_MinorDivisionColor$ColorMap"Cyan" ] );
   
$XY_Plot->SetX_MinorCustomIncrement"CustomMinorIncFunction" );
   
$XY_Plot->SetX_MinorCustomStart$X_Min );
   
$XY_Plot->DrawX_MinorDivisions();

   
// Setup and draw minor verticle scale (top to bottom)
   
$XY_Plot->SetY_MinorDivisionScale$Y_MinorScale );
   
$XY_Plot->SetY_MinorDivisionColor$ColorMap"LightGray" ] );
   
$XY_Plot->DrawY_MinorDivisions();

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

   // Extend lines 5 pixels past margins for lable
   
$XY_Plot->SetX_MajorDivisionExtention);

   
// Division lines are blue
   
$XY_Plot->SetX_MajorDivisionColor$ColorMap"Blue" ] );

   
// Text lables are drak gray
   
$XY_Plot->SetX_MajorDivisionTextColor$ColorMap"DarkGray" ] );

   
// Custom callback for text
   
$XY_Plot->SetX_MajorTextCallback"HorizontalStringCallback" );

   
// Custom callback for incrementing major scale
   // (This is for incrementing scale by months with Unix dates)
   
$XY_Plot->SetX_MajorCustomIncrement"CustomMajorIncFunction" );

   
// Where to start with lables
   
$XY_Plot->SetX_MajorCustomStart$X_Min );

   
// Draw it
   
$XY_Plot->DrawX_MajorDivisions();

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

   // Extend lines 5 pixels past margins for lable
   
$XY_Plot->SetY_MajorDivisionExtention);

   
// Scale
   
$XY_Plot->SetY_MajorDivisionScale$Y_MajorScale );

   
// Divisions in drak gray
   
$XY_Plot->SetY_MajorDivisionColor$ColorMap"DarkGray" ] );

   
// Lables in dark gray
   
$XY_Plot->SetY_MajorDivisionTextColor$ColorMap"DarkGray" ] );

   
// Custom callback to add dollor signs
   
$XY_Plot->SetY_MajorTextCallback"VerticleTextScale" );

   
// Draw it
   
$XY_Plot->DrawY_MajorDivisions();

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

   // Setup timeframe
   
$XY_Plot->SetMaxX_Distance60 60 24 ); 

   
// Render the points and lines to image
   
$XY_Plot->RenderMeanPlot();
   
$XY_Plot->RenderLinearRegression();
   
$XY_Plot->RenderWithLines();
   
$XY_Plot->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 
     
,
     
$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 negitive 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 verticle text-- just return value with dollor sign in front
//---------------------------------------------------------------------------
function VerticleTextScale$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(
        
,
        
date"m" $Value ) + ,
        
,
        
date"Y" $Value )
      );
  else
    
// Next increment is 10 days
    
$Value mktime(
        
,
        
date"m" $Value ) ,
        
$NewDay ,
        
date"Y" $Value )
      );

  return 
$Value;
}

//---------------------------------------------------------------------------
// Custom major increment function
// Increment by month
//---------------------------------------------------------------------------
function CustomMajorIncFunction$Value )
{
  
$Value mktime(
      
,
      
date"m" $Value ) + ,
      
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 
     
,
     
,
     
$ImageWidth ,
     
$ImageHeight ,
     
$ColorMap"Black" ] );
}

?>
1