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 #16 - Playing with line style, Custom X Scale, Picture inclusion

This example shows various graphing techniques playing with line graph style using the setLineStyle fonction. The X scale is also stretched showing only one label every 3 points using the last parameter of the drawScale function. We're also including the external picture Sample/logo.png calling the drawFromPNG function with a default alpha transparency of 0.

This code can looks a bit tricky because using various exotic functionalities but is also more representative of what you'll probably do to customize a bit your charts!

Output :


Source code :

  1. <?php  
  2.  // Standard inclusions        
  3.  include("pChart/pData.class");     
  4.  include("pChart/pChart.class");     
  5.    
  6.  // Dataset definition      
  7.  $DataSet = new pData;  
  8.  $DataSet->AddPoint(array(10,9.4,7.7,5,1.7,-1.7,-5,-7.7,-9.4,-10,-9.4,-7.7,-5,-1.8,1.7),"Serie1");  
  9.  $DataSet->AddPoint(array(0,3.4,6.4,8.7,9.8,9.8,8.7,6.4,3.4,0,-3.4,-6.4,-8.6,-9.8,-9.9),"Serie2");  
  10.  $DataSet->AddPoint(array(7.1,9.1,10,9.7,8.2,5.7,2.6,-0.9,-4.2,-7.1,-9.1,-10,-9.7,-8.2,-5.8),"Serie3");  
  11.  $DataSet->AddPoint(array("Jan","Jan","Jan","Feb","Feb","Feb","Mar","Mar","Mar","Apr","Apr","Apr","May","May","May"),"Serie4");  
  12.  $DataSet->AddAllSeries();  
  13.  $DataSet->SetAbsciseLabelSerie("Serie4");  
  14.  $DataSet->SetSerieName("Max Average","Serie1");  
  15.  $DataSet->SetSerieName("Min Average","Serie2");  
  16.  $DataSet->SetSerieName("Temperature","Serie3");  
  17.  $DataSet->SetYAxisName("Temperature");  
  18.  $DataSet->SetXAxisName("Month of the year");  
  19.    
  20.  // Initialise the graph     
  21.  $Test = new pChart(700,230);  
  22.  $Test->reportWarnings("GD");  
  23.  $Test->setFixedScale(-12,12);  
  24.  $Test->setFontProperties("Fonts/tahoma.ttf",8);     
  25.  $Test->setGraphArea(65,30,570,185);     
  26.  $Test->drawFilledRoundedRectangle(7,7,693,223,5,240,240,240);     
  27.  $Test->drawRoundedRectangle(5,5,695,225,5,230,230,230);     
  28.  $Test->drawGraphArea(255,255,255,TRUE);  
  29.  $Test->drawScale($DataSet->GetData(),$DataSet->GetDataDescription(),SCALE_NORMAL,150,150,150,TRUE,0,2,TRUE,3);     
  30.  $Test->drawGrid(4,TRUE,230,230,230,50);  
  31.   
  32.  // Draw the 0 line     
  33.  $Test->setFontProperties("Fonts/tahoma.ttf",6);     
  34.  $Test->drawTreshold(0,143,55,72,TRUE,TRUE);     
  35.    
  36.  // Draw the area  
  37.  $DataSet->RemoveSerie("Serie4");  
  38.  $Test->drawArea($DataSet->GetData(),"Serie1","Serie2",239,238,227,50);  
  39.  $DataSet->RemoveSerie("Serie3");  
  40.  $Test->drawLineGraph($DataSet->GetData(),$DataSet->GetDataDescription());     
  41.   
  42.  // Draw the line graph  
  43.  $Test->setLineStyle(1,6);  
  44.  $DataSet->RemoveAllSeries();  
  45.  $DataSet->AddSerie("Serie3");  
  46.  $Test->drawLineGraph($DataSet->GetData(),$DataSet->GetDataDescription());     
  47.  $Test->drawPlotGraph($DataSet->GetData(),$DataSet->GetDataDescription(),3,2,255,255,255);     
  48.   
  49.  // Write values on Serie1 & Serie2  
  50.  $Test->setFontProperties("Fonts/tahoma.ttf",8);     
  51.  $Test->writeValues($DataSet->GetData(),$DataSet->GetDataDescription(),"Serie3");     
  52.    
  53.  // Finish the graph     
  54.  $Test->setFontProperties("Fonts/tahoma.ttf",8);     
  55.  $Test->drawLegend(590,90,$DataSet->GetDataDescription(),255,255,255);     
  56.  $Test->setFontProperties("Fonts/tahoma.ttf",10);     
  57.  $Test->drawTitle(60,22,"example 15",50,50,50,585);  
  58.   
  59.  // Add an image  
  60.  $Test->drawFromPNG("Sample/logo.png",584,35);  
  61.   
  62.  // Render the chart  
  63.  $Test->Render("example15.png");        
  64. ?>  
If you want to send the picture directly in a browser, replace the Render() command by Stroke().


Last updated on 07/27/2008