pChart is one of the most powerful php library to generate charts or graphs
using php GD library. You can add data to your chart from MySQL tables or from
CSV file. Usually chart library are not free but pChart is free and providing
lots of options.
Click here
to know more about pChart.
Lets Create a simple line chart
First of all include the required library files.
include("pChart/pData.class");
include("pChart/pChart.class");
Generate the chart
In this example we are retrieving the data from csv file and generate an
image called example1.png.
// Dataset definition
$DataSet = new pData;
$DataSet->ImportFromCSV("Sample/bulkdata.csv",",",array(1,2,3),FALSE,0);
$DataSet->AddAllSeries();
$DataSet->SetAbsciseLabelSerie();
$DataSet->SetSerieName("January","Serie1");
$DataSet->SetSerieName("February","Serie2");
$DataSet->SetSerieName("March","Serie3");
$DataSet->SetYAxisName("Average age");
$DataSet->SetYAxisUnit("us");
// Initialise the graph
$Test = new pChart(700,230);
$Test->setFontProperties("Fonts/tahoma.ttf",8);
$Test->setGraphArea(70,30,680,200);
$Test->drawFilledRoundedRectangle(7,7,693,223,5,240,240,240);
$Test->drawRoundedRectangle(5,5,695,225,5,230,230,230);
$Test->drawGraphArea(255,255,255,TRUE);
$Test->drawScale($DataSet->GetData(),$DataSet->GetDataDescription(),
SCALE_NORMAL,150,150,150,TRUE,0,2);
$Test->drawGrid(4,TRUE,230,230,230,50);
// Draw the 0 line
$Test->setFontProperties("Fonts/tahoma.ttf",6);
$Test->drawTreshold(0,143,55,72,TRUE,TRUE);
// Draw the line graph
$Test->drawLineGraph($DataSet->GetData(),$DataSet->GetDataDescription());
$Test->drawPlotGraph($DataSet->GetData(),
$DataSet->GetDataDescription(),3,2,255,255,255);
// Finish the graph
$Test->setFontProperties("Fonts/tahoma.ttf",8);
$Test->drawLegend(75,35,$DataSet->GetDataDescription(),255,255,255);
$Test->setFontProperties("Fonts/tahoma.ttf",10);
$Test->drawTitle(60,22,"example 1",50,50,50,585);
$Test->Render("example1.png");
HTML Code
<img src="example1.png" border="0">
Output
Let's create another graphical chart
This time the chart will be generated from array.
$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(660,280);
$Test->drawGraphAreaGradient(90,90,90,90,TARGET_BACKGROUND);
$Test->setFixedScale(0,40,4);
// Graph area setup
$Test->setFontProperties("Fonts/pf_arma_five.ttf",6);
$Test->setGraphArea(60,40,640,230);
$Test->drawGraphArea(200,200,200,FALSE);
$Test->drawScale($DataSet->GetData(),$DataSet->GetDataDescription(),
SCALE_NORMAL,200,200,200,TRUE,0,2);
$Test->drawGraphAreaGradient(40,40,40,-50);
$Test->drawGrid(4,TRUE,230,230,230,10);
// Draw the line chart
$Test->setShadowProperties(3,3,0,0,0,30,4);
$Test->drawCubicCurve($DataSet->GetData(),$DataSet->GetDataDescription());
$Test->clearShadow();
$Test->drawPlotGraph($DataSet->GetData(),$DataSet->GetDataDescription(),
3,0,-1,-1,-1,TRUE);
// Write the title
$Test->setFontProperties("Fonts/MankSans.ttf",18);
$Test->setShadowProperties(1,1,0,0,0);
$Test->drawTitle(0,0,"Average temperatures",255,255,255,700,30,TRUE);
$Test->clearShadow();
// Draw the legend
$Test->setFontProperties("Fonts/tahoma.ttf",8);
$Test->drawLegend(580,5,$DataSet->GetDataDescription(),0,0,0,0,0,0,255,
255,255,FALSE);
// Render the picture
$Test->Render("example25.png")
Output:
Click on the following download link to get more examples.
Download File
Total Downloads: 1726