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 :
 <?php
  // Standard inclusions   
  include("pChart/pData.class");
  include("pChart/pChart.class");

  // Dataset definition 
  $DataSet = new pData;

  // Compute the points
  for($i=0;$i<=360;$i=$i+10)
   {
    $DataSet->AddPoint(cos($i*3.14/180)*80+$i,"Serie1");
    $DataSet->AddPoint(sin($i*3.14/180)*80+$i,"Serie2");
   }

  $DataSet->SetSerieName("Trigonometric function","Serie1");
  $DataSet->AddSerie("Serie1");
  $DataSet->AddSerie("Serie2");
  $DataSet->SetXAxisName("X Axis");
  $DataSet->SetYAxisName("Y Axis");

  // Initialise the graph
  $Test = new pChart(300,300);
  $Test->drawGraphAreaGradient(0,0,0,-100,TARGET_BACKGROUND);

  // Prepare the graph area
  $Test->setFontProperties("Fonts/tahoma.ttf",8);
  $Test->setGraphArea(55,30,270,230);
  $Test->drawXYScale($DataSet->GetData(),$DataSet->GetDataDescription(),"Serie1","Serie2",213,217,221,TRUE,45);
  $Test->drawGraphArea(213,217,221,FALSE);
  $Test->drawGraphAreaGradient(30,30,30,-50);
  $Test->drawGrid(4,TRUE,230,230,230,20);

  // Draw the chart
  $Test->setShadowProperties(2,2,0,0,0,60,4);
  $Test->drawXYGraph($DataSet->GetData(),$DataSet->GetDataDescription(),"Serie1","Serie2",0);
  $Test->clearShadow();

  // Draw the title
  $Title = "Drawing X versus Y charts trigonometric functions  ";
  $Test->drawTextBox(0,280,300,300,$Title,0,255,255,255,ALIGN_RIGHT,TRUE,0,0,0,30);

  // Draw the legend
  $Test->setFontProperties("Fonts/pf_arma_five.ttf",6);
  $DataSet->RemoveSerie("Serie2");
  $Test->drawLegend(160,5,$DataSet->GetDataDescription(),0,0,0,0,0,0,255,255,255,FALSE);

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


Last updated on 09/19/2008