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

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

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

   
// Margins
   
$LeftMargin     15;
   
$RightMargin    135;
   
$TopMargin      25;
   
$BottomMargin   20;

   
// Verticle scale
   
$Y_MajorScale   0.5;
   
$Y_MinorScale   $Y_MajorScale 5.0;
   
$X_MajorScale   1.0 10.0;
   
$X_MinorScale   $X_MajorScale 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$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 );

   
$ColorMap"DarkYellow"  ] = imagecolorallocate$Image 128 128 ,   );

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

   
// Linear regression thickness
   
$XY_Plot->SetLinearRegressionThickness);

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

   
// Automaticlly adjust verticle scale
   
$XY_Plot->SetX_Span0.0  1.0 );
   
$XY_Plot->SetY_Span( -0.5 2.5 );

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

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

   
$TextAreaLeft  $ImageWidth 100;
   
$TextAreaRight $ImageWidth 8;

   
$TextArea 
     new 
Window
       
$TextAreaLeft ,
       
,
       
$TextAreaRight ,
       
$ImageHeight 8
     
);

   
$TextOffset_X = ( $TextArea->GetWidth() / ) + $TextAreaLeft;
   
$TextOffset_Y 8;

   
ImageRectangle
     
$Image 
     
$TextArea->GetX() ,
     
$TextArea->GetY() ,
     
$TextArea->GetXX() ,
     
$TextArea->GetYY() ,
     
$ColorMap"Black" ] );

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

   
$XY_Plot->SetColor$ColorMap"Red" ] );
   
$XY_Plot->SetLinearRegressionColor$ColorMap"LightRed" ] );
   
DrawGraph$RootDirectory "Examples/ScatterData_Slope1.4_25.csv" );

   
$XY_Plot->SetColor$ColorMap"Green" ] );
   
$XY_Plot->SetLinearRegressionColor$ColorMap"LightGreen" ] );
   
DrawGraph$RootDirectory "Examples/ScatterData_Slope1.2_25.csv" );

   
$XY_Plot->SetColor$ColorMap"Blue" ] );
   
$XY_Plot->SetLinearRegressionColor$ColorMap"LightCyan" ] );
   
DrawGraph$RootDirectory "Examples/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 ,
     
,
     
"Multiple Scatter Graph" ,
     
$FontSize ,
     
$ColorMap"Black" ] );

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

   exit;

function 
DrawGraph$FileName )
{
   global 
$XY_Plot;
   global 
$TextOffset_Y;
   global 
$TextOffset_X;
   global 
$TextArea;
   global 
$ColorMap;

   
// Load data points from file
   
$Data file$FileName );

   
$XY_Plot->ResetData();

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

   
// Render the points and lines to image
   
$XY_Plot->RenderPoints();
   
$XY_Plot->RenderLinearRegression();

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

   // Slope
   
WriteText(
     
$TextOffset_X $TextOffset_Y "Slope" 
     
number_format$XY_Plot->GetSlope() , ) );

   
// Y-Intercept
   
WriteText(
     
$TextOffset_X $TextOffset_Y "Y-Intercept" 
     
number_format$XY_Plot->GetY_Intercept() , ) );

   
// Correlation
   
WriteText(
     
$TextOffset_X $TextOffset_Y "Correlation" 
     
number_format$XY_Plot->GetCorrelation() * 100 ) . "%" );

   
$TextOffset_Y += ImageFontHeight);
}

//---------------------------------------------------------------------------
// Write a lable 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 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;
}

//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
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 ,
     
$x 
     
$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 
     
,
     
,
     
$ImageWidth ,
     
$ImageHeight ,
     
$ColorMap"Black" ] );
}

?>
1