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
Line graph
Cubic curve graph
Bar Graph
Stacked Bar Graph
Overlay Bar graph
Line + Area
Limits graph
Filled line graph
Filled cubic graph
Radar graph
Basic Pie graph
Exploded Pie graph
3D Pie graph
Scatter charts
Advanced Examples
Example #25 - Scatter charts

Like the pie charts, scatter charts require a special use of the pData class.

To draw a scatter plot you need to define at least two data serie. In this exemple Serie1/Serie2 will be drawn as line. Data are filled using some maths functions (cos/sin) to render circles. We'll keep the scaling dynamic.

Before rendering a scatter chart you must invoke the drawXYScale() function that will create both X and Y axis. By default, min/max values will be retrieved from the series, anyway you can fix the values while calling the setFixedScale function (not shown in this example) : We can creating a box (-25,25)-(25,25) with 5 vertical and horizontal divisions.

To draw the line scatter chart we are calling the drawXYGraph function specifying the series name and the 1st palette color.

Running this script will create a example24.png file in the current directory.

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.   
  9.  // Compute the points  
  10.  for($i=0;$i<=360;$i=$i+10)  
  11.   {  
  12.    $DataSet->AddPoint(cos($i*3.14/180)*80+$i,"Serie1");  
  13.    $DataSet->AddPoint(sin($i*3.14/180)*80+$i,"Serie2");  
  14.   }  
  15.   
  16.  $DataSet->SetSerieName("Trigonometric function","Serie1");  
  17.  $DataSet->AddSerie("Serie1");  
  18.  $DataSet->AddSerie("Serie2");  
  19.  $DataSet->SetXAxisName("X Axis");  
  20.  $DataSet->SetYAxisName("Y Axis");  
  21.   
  22.  // Initialise the graph  
  23.  $Test = new pChart(300,300);  
  24.  $Test->drawGraphAreaGradient(0,0,0,-100,TARGET_BACKGROUND);  
  25.   
  26.  // Prepare the graph area  
  27.  $Test->setFontProperties("Fonts/tahoma.ttf",8);  
  28.  $Test->setGraphArea(55,30,270,230);  
  29.  $Test->drawXYScale($DataSet->GetData(),$DataSet->GetDataDescription(),"Serie1","Serie2",213,217,221,TRUE,45);  
  30.  $Test->drawGraphArea(213,217,221,FALSE);  
  31.  $Test->drawGraphAreaGradient(30,30,30,-50);  
  32.  $Test->drawGrid(4,TRUE,230,230,230,20);  
  33.   
  34.  // Draw the chart  
  35.  $Test->setShadowProperties(2,2,0,0,0,60,4);  
  36.  $Test->drawXYGraph($DataSet->GetData(),$DataSet->GetDataDescription(),"Serie1","Serie2",0);  
  37.  $Test->clearShadow();  
  38.   
  39.  // Draw the title  
  40.  $Title = "Drawing X versus Y charts trigonometric functions  ";  
  41.  $Test->drawTextBox(0,280,300,300,$Title,0,255,255,255,ALIGN_RIGHT,TRUE,0,0,0,30);  
  42.   
  43.  // Draw the legend  
  44.  $Test->setFontProperties("Fonts/pf_arma_five.ttf",6);  
  45.  $DataSet->RemoveSerie("Serie2");  
  46.  $Test->drawLegend(160,5,$DataSet->GetDataDescription(),0,0,0,0,0,0,255,255,255,FALSE);  
  47.   
  48.  $Test->Render("example24.png");  
  49. ?>  
If you want to send the picture directly in a browser, replace the Render() command by Stroke().


Last updated on 09/19/2008