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
Using pChart with MySQL
Playing with colors
Setting X labels
Series are drawn black?
Debugging charts
Call to undefined function
Digging with pChart
Basic Examples
Advanced Examples
Create multiple charts

It can be useful to draw multiple charts in a single picture. Some things like scaling need to be taken in consideration. While invoking the drawScale() function the chart area coordinates are computed including the maximum and minimum values of the dataset.

Before drawing another chart you must call the clearScale() function.

Drawing multiple chart with a single Data set

This example is showing how to draw multiple charts in a single picture using the same dataset.

  • We are drawing the stacked bar chart using an automatic scale including a left & right border. Nothing new here, everything is done like when rendering a simple chart.


  • Then we are clearing the scale calling the clearScale() function of the pChart class. This will reset all the dynamically computed variables (YStep,XStep, Margins,..)


  • We redefine the graph area calling the setGraphArea() function of the pChart class.


  • We draw the second chart.

It can be quite convenient to draw the same dataset with different chart type to highlight specific values or series. As the dataset is the same for the two chart, invoking the drawLegend legend function will write the accurate legend that will be the same for all the charts.





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

 // Dataset definition 
 $DataSet = new pData;
 $DataSet->AddPoint(array(1,2,5),"Serie1");
 $DataSet->AddPoint(array(3,2,2),"Serie2");
 $DataSet->AddPoint(array(3,4,1),"Serie3");
 $DataSet->AddPoint(array("A#~1","A#~2","A#~3"),"Labels");
 $DataSet->AddAllSeries();
 $DataSet->RemoveSerie("Labels");
 $DataSet->SetAbsciseLabelSerie("Labels");
 $DataSet->SetSerieName("Alpha","Serie1");
 $DataSet->SetSerieName("Beta","Serie2");
 $DataSet->SetSerieName("Gama","Serie3");
 $DataSet->SetXAxisName("Samples IDs");
 $DataSet->SetYAxisName("Test Marker");
 $DataSet->SetYAxisUnit("Ám");

 // Initialise the graph
 $Test = new pChart(380,400);
 $Test->drawGraphAreaGradient(132,153,172,100,TARGET_BACKGROUND);

 // Graph area setup
 $Test->setFontProperties("Fonts/tahoma.ttf",8);
 $Test->setGraphArea(110,180,350,360);
 $Test->drawGraphArea(213,217,221,FALSE);
 $Test->drawScale($DataSet->GetData(),$DataSet->GetDataDescription(),SCALE_ADDALLSTART0,213,217,221,TRUE,0,2,TRUE);
 $Test->drawGraphAreaGradient(162,183,202,50);
 $Test->drawGrid(4,TRUE,230,230,230,20);

 // Draw the title   
 $Title = "  Average growth size for selected\r\n  DNA samples  ";   
 $Test->setLineStyle(2);
 $Test->drawLine(41,-2,41,402,0,0,0);   
 $Test->setLineStyle(1);
 $Test->drawTextBox(0,0,40,400,$Title,90,255,255,255,ALIGN_BOTTOM_CENTER,TRUE,0,0,0,30);   

 // Draw the bar graph
 $Test->drawStackedBarGraph($DataSet->GetData(),$DataSet->GetDataDescription(),50);

 // Second chart
 $DataSet->SetXAxisName("");
 $Test->clearScale();
 $Test->setGraphArea(110,20,350,140);
 $Test->drawGraphArea(213,217,221,FALSE);
 $Test->drawScale($DataSet->GetData(),$DataSet->GetDataDescription(),SCALE_START0,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->drawFilledCubicCurve($DataSet->GetData(),$DataSet->GetDataDescription(),.1,40);

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

 // Finish the graph
 $Test->addBorder(1);
 $Test->Render("MyPicture.png");
?>


Last updated on 08/26/2008