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

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

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

   
// Margins
   
$LeftMargin     20;
   
$RightMargin    45;
   
$TopMargin      25;
   
$BottomMargin   20;

   
// Verticle scale
   
$Y_MajorScale   0.5;
   
$Y_MinorScale   $Y_MajorScale 5;
   
$X_MajorScale   0.01;
   
$X_MinorScale   $X_MajorScale 10;
   
//-----------------------------------------------------------

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

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

   
// 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" ] );
   
   
// Average line size
   
$XY_Plot->SetAverageWidth);

   
// Set point size
   
$XY_Plot->SetCircleSize12 );

   
// Load data points from file
   
$Data file$RootDirectory "Examples/ScatterData_Slope0_50.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->SetX_Span0.05 );
   
$XY_Plot->SetY_Span( -);

   
//------------------------------------------------------
   // 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_MinorDivisionScale$X_MinorScale );
   
$XY_Plot->SetX_MinorDivisionColor$ColorMap"Gray90" ] );
   
$XY_Plot->DrawX_MinorDivisions();

   
// Setup and draw minor verticle scale (top to bottom)
   
$XY_Plot->SetY_MinorDivisionScale$Y_MinorScale );
   
$XY_Plot->SetY_MinorDivisionColor$ColorMap"Gray90" ] );
   
$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);

   
// Scale
   
$XY_Plot->SetX_MajorDivisionScale$X_MajorScale );

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

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

   
// Scale callback
   
$XY_Plot->SetX_MajorTextCallback"TextScale" );
   
   
// 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"Gray70" ] );

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

   
// Scale callback
   
$XY_Plot->SetY_MajorTextCallback"TextScale" );
   
   
// Draw it
   
$XY_Plot->DrawY_MajorDivisions();

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

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

   
// Render the points and lines to image
   
$XY_Plot->RenderWithLines();
   
$XY_Plot->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 
     
,
     
$x $FontSize ,
     
$Title ,
     
$ColorMap"Black" ] );

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

   exit;

//---------------------------------------------------------------------------
// Custom verticle text-- just return value with dollor sign in front
//---------------------------------------------------------------------------
function TextScale$Value )
{
   return 
number_format$Value );
}

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

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