Zend - The PHP Company




Graphs

Add Code


Code exaple of using the phplot library  

Type: code fragment
Added by: karakas
Entered: 28/11/2002
Last modified: 02/11/2001
Rating: - (fewer than 3 votes)
Views: 12378
This piece of code demostrates the use of phplot (see Code Gallery entry) to create inline superimposed graphs from large arrays of data. To facilitate this, random image files are generated in a temporary directory on the server, which are then displayed inline. When the temporary dir becomes too large, its contents are purged recursively.


<?php
//*******************************  Graphics ******************************//
// This part Copyright (c) 2002 Chris Karakas <chris@karakas-online.de>
// http://www.karakas-online.de
//
// Compute the graph here
include ( "./phplot.php");

if (isset(
$balance_array["1"])){
  
  
$graph = new PHPlot;
  
  
$tmpdir "./tmp";
  
$prefix "chart";
  
$maxbytes 100000;
  
//Create a file with random name in the temp dir, starting with $prefix
  
$temp tempnam($tmpdir,$prefix);
  
//Add the .png ending and rename it, otherwise some web servers will not serve it.
  
$temp_png $temp '.' "png";
  
rename($temp,$temp_png); 
  
//Specify this random file to be the output file of the image
  
$graph->SetOutputfile($temp_png);
  
$graph->SetFileFormat("png"); // is default anyway
  
  //Don't draw the image yet
  
$graph->SetPrintImage(0); 
  
//Don't output the file headers - we're inline! 
  
$graph->SetIsInline("1");
   
  
$graph->SetDataType"linear-linear");
  
//First dataset to plot
  
$graph->SetDataValues($balance_array);


  
//Specify plotting area details 
  
$graph->SetImageArea(600,400);
  
$graph->SetPlotType"lines");
  
$graph->SetTitleFontSize"2");
  
$graph->SetTitle"Remaining balance");
  
$graph->SetPlotAreaWorld(0,0,($current_year*12*1.1),($financing_price 1.1));
  
$graph->SetPlotBgColor"white");
  
$graph->SetPlotBorderType"left");
  
$graph->SetBackgroundColor"white");
  
  
//Define the X axis 
  
$graph->SetXLabel"Month");
  
$graph->SetHorizTickIncrement"24");
  
$graph->SetXGridLabelType"plain");
  
  
//Define the Y axis 
  
$graph->SetVertTickIncrement(($sale_price 10));
  
$graph->SetPrecisionY(0);
  
$graph->SetYGridLabelType"data");
  
$graph->SetLightGridColor"blue");

  
$graph->SetDataColors( array( "red"), array( "black") );


  
//Check size of tmp directory
  
$tmpdirsize dirsize($tmpdir);
  
//If the size of the tmp dir has exceeded the maximum in bytes that we allow,
  //delete every file in it! We check this only once per run.
  
if ($tmpdirsize $maxbytes){
    
runlink($tmpdir);
  }

  
//Draw the chart
  
$graph->DrawGraph();
  
//Now, the image is ready - print it to the file!
  
$graph->PrintImage();

  
//Set correct permissions
  
chmod($temp_png,0644);
  
//Determine the image size in order to set it correct in the img tag below. 
  
$size getimagesize ("$temp_png");
  echo 
"<a name="graphics"><H3 align="center">Graphics</H3></a><p align="center"><img src="$temp_png{$size[3]}>";

}

// Compute the second image here

if (isset($principal_array["1"])){

  
$graph = new PHPlot;

  
//Create a file with random name in the temp dir, starting with $prefix
  
$temp tempnam($tmpdir,$prefix);
  
//Add the .png ending and rename it, otherwise some web servers will not serve it.
  
$temp_png $temp '.' "png";
  
rename($temp,$temp_png);
  
//Specify this random file to be the output file of the image
  
$graph->SetOutputfile($temp_png);
  
$graph->SetFileFormat("png"); // is default anyway

  //Don't draw the image yet
  
$graph->SetPrintImage(0);
  
//Don't output the file headers - we're inline! 
  
$graph->SetIsInline("1");

  
$graph->SetDataType"linear-linear");
  
//First dataset to plot
  
$graph->SetDataValues($principal_array);


  
//Specify plotting area details 
  
$graph->SetImageArea(600,400);
  
$graph->SetPlotType"lines");
  
$graph->SetTitleFontSize"2");
  
$graph->SetTitle"Interest vs. principal (monthly)");
  
$graph->SetPlotAreaWorld(0,0,($current_year*12*1.1),($monthly_payment 1.1));
  
$graph->SetPlotBgColor"white");
  
$graph->SetPlotBorderType"left");
  
$graph->SetBackgroundColor"white");


  
//Define the X axis 
  
$graph->SetXLabel"Month");
  
$graph->SetHorizTickIncrement"24");
  
$graph->SetXGridLabelType"plain");

  
//Define the Y axis 
  
$graph->SetVertTickIncrement(($monthly_payment 10));
  
$graph->SetPrecisionY(0);
  
$graph->SetYGridLabelType"data");
  
$graph->SetLightGridColor"blue");

  
$graph->SetDataColors( array("green"), array("black") );

  
//Draw the first chart
  
$graph->DrawGraph();

  
//Second chart on the image

  //Second dataset to plot
  
$graph->SetDataValues($interest_array);

  
//$graph->SetDataColors( array( "blue"), array( "black") );

  //We already got them in the first graph
  
$graph->SetDrawXDataLabels(0);

  
//Draw a legend
  //Define the legend texts
  
$graph->SetLegend(array("Principal","Interest"));
  
//Set the colors for the legend texts
  
$graph->SetDataColors( array( "green","blue"), array( "black","black") );
  
//Draw the legend
  
$graph->DrawLegend(450,175,"");

  
//Set the correct color for the second chart
  
$graph->SetDataColors( array( "blue"), array( "black") );
  
//Draw the New data over the first chart
  
$graph->DrawLines();

  
//Now, the image is ready - print it to the file!
  
$graph->PrintImage();

  
//Set correct permissions
  
chmod($temp_png,0644);
  
//Determine the image size in order to set it correct in the img tag below.
  
$size getimagesize ("$temp_png");
  echo 
"<p align="center"><img src="$temp_png{$size[3]}>";
}



function 
dirsize($directory){
   if (!
is_dir($directory)) return -1;
   
$size 0;
   if (
$DIR opendir($directory)){
      while ((
$dirfile readdir($DIR)) !== false){
         if (
is_link($directory '/' $dirfile) || $dirfile == '.' || $dirfile == '..')
           continue;
         if (
is_file($directory '/' $dirfile))
           
$size += filesize($directory '/' $dirfile);
         else if (
is_dir($directory '/' $dirfile)){
           
$dirSize dirsize($directory '/' $dirfile);
           if (
$dirSize >= 0$size += $dirSize;
           else return -
1;
         }
      }
      
closedir($DIR);
   }
   return 
$size;
}


function 
runlink($directory){
   if (!
is_dir($directory)) return -1;
   if (
$DIR opendir($directory)){
      while ((
$dirfile readdir($DIR)) !== false){
         if (
is_link($directory '/' $dirfile) || $dirfile == '.' || $dirfile == '..')
           continue;
         if (
is_file($directory '/' $dirfile))
           
unlink($directory '/' $dirfile);
         else if (
is_dir($directory '/' $dirfile)){
           
runlink($directory '/' $dirfile);
         }
      }
      
closedir($DIR);
   }
}

//******************************* End of  Graphics ******************************//



?>


Usage Example


See the example


Rate This Script





Search



This Category All Categories