SourceForge.net  |  Sunyday.net
Last release: 1.27d
a PHP Class to build Charts
pChart 2.x is born, you can start moving your script to this new version. The new website is at www.pchart.net.
Overview  |  Screenshots  |  Download  |  Add-ons  |  Demonstration  |  Support  |  :: Documentation ::
Documentation Contents
Overview
Class definition
FAQ
Digging with pChart
Basic Examples
Advanced Examples
Graphs labels
Small "Fast Graphs"
Using pCache
Line style, Pics inclusion
Importing CSV data
Error reporting
Missing values
Playing with axis formats
Playing with backgrounds
High quality graphics
Customizing plot charts
2nd Y axis and shadows
Example #21 - Playing with backgrounds

Since release 1.27b it is now possible to customize backgrounds (Whole picture or only the graph area) by drawing gradient overlays. In this example we are filling the background calling drawGraphAreaGradient and specifying the target to be the background (Whole picture) TARGET_BACKGROUND. We are calling a second time this function specifying no target, by default it will be applyed to the graph area. The title of the graph is written calling the drawTextBox function specifying the box position to be on the bottom of the picture, the fill color, the text alignement. We're also applying a shading to the text content and setting the transparency of the background box to be of 30%.

Finally we're calling the AddBorder function to draw a 2px black border around the picture. This call must be done at the end of the chart drawing because it will resize the picture.


Output :


Source code :

 <?php
  // Standard inclusions   
  include("pChart/pData.class");
  include("pChart/pChart.class");

  // Dataset definition 
  $DataSet = new pData;
  $DataSet->AddPoint(array(9,9,9,10,10,11,12,14,16,17,18,18,19,19,18,15,12,10,9),"Serie1");
  $DataSet->AddPoint(array(10,11,11,12,12,13,14,15,17,19,22,24,23,23,22,20,18,16,14),"Serie2");
  $DataSet->AddPoint(array(4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22),"Serie3");
  $DataSet->AddAllSeries();
  $DataSet->RemoveSerie("Serie3");
  $DataSet->SetAbsciseLabelSerie("Serie3");
  $DataSet->SetSerieName("January","Serie1");
  $DataSet->SetSerieName("February","Serie2");
  $DataSet->SetYAxisName("Temperature");
  $DataSet->SetYAxisUnit("C");
  $DataSet->SetXAxisUnit("h");

  // Initialise the graph
  $Test = new pChart(700,230);
  $Test->drawGraphAreaGradient(132,153,172,50,TARGET_BACKGROUND);
  $Test->setFontProperties("Fonts/tahoma.ttf",8);
  $Test->setGraphArea(60,20,585,180);
  $Test->drawGraphArea(213,217,221,FALSE);
  $Test->drawScale($DataSet->GetData(),$DataSet->GetDataDescription(),SCALE_NORMAL,213,217,221,TRUE,0,2);
  $Test->drawGraphAreaGradient(162,183,202,50);
  $Test->drawGrid(4,TRUE,230,230,230,20);

  // Draw the line chart
  $Test->drawLineGraph($DataSet->GetData(),$DataSet->GetDataDescription());
  $Test->drawPlotGraph($DataSet->GetData(),$DataSet->GetDataDescription(),2);

  // Draw the legend
  $Test->setFontProperties("Fonts/tahoma.ttf",8);
  $Test->drawLegend(605,142,$DataSet->GetDataDescription(),236,238,240,52,58,82);

  // Draw the title
  $Title = "Average Temperatures during the first months of 2008  ";
  $Test->drawTextBox(0,210,700,230,$Title,0,255,255,255,ALIGN_RIGHT,TRUE,0,0,0,30);

  // Render the picture
  $Test->AddBorder(2);
  $Test->Render("example21.png");
 ?>
 
If you want to send the picture directly in a browser, replace the Render() command by Stroke().


Last updated on 07/27/2008