You can create graphs and charts in PHP using GDChart extension. GDChart comes with the extension. This ext/gdchart extension, in turn, requires you to have GD support compiled into your PHP build. On UNIX systems, this is accomplished by downloading and installing the GD libraries from http://www.boutell.com/gd
After downloading you can install using pecl command
~$ pecl install gdchart
Also you can manually download the source code archive from http://pecl.php.net/package/GDChart and compile it as shown below
~$ cd gdchart-1.2
~$ phpsize
~$ ./configure
~$ make
~$ make install
Note: You should be as super user or you should be in root shell while installing this
The above procedure will create a loadable PHP module named gdchart. Now restart your webserver and check this in phpinfo();

Now lets start with a simple example that will demonstrate the drawing of graphs
<?PHP
// initialize object with chart type
$gd = new GDChart(GDChart::BAR);
// add data points
$gd->addValues(array(18234, 16484, 16574, 17464, 19474));
// add X-axis labels
$gd->setLabels(array(‘2002′, ‘2003′, ‘2004′, ‘2005′, ‘2006′));
// generate PNG chart and send to client
header(‘Content-Type:image/png’);
echo $gdc->out(300,200,GDChart::PNG);
?>
The above example is simple understandable. Firstly it will initializes an object of GDChart with BAR type graph. In the next statement we have assigned data points to the object which adds to Y-Axis and then X-axis labels. After giving the points and labels we are printing the graph in PNG format. Here is the graph looks like

GDChart supports 20 different chart formats, so it’s pretty easy to represent the same data as, say, a line graph, by merely changing the values in the statement $gd = new GDChart(GDChart::BAR);. Its really easy to construct different formas of graphs. Below mentioned are the formats available
| Constant | Chart Type |
| LINE | Line |
| BAR | Bar |
| AREA | Area |
| FLOATINGBAR | Floating Bar |
| HILOCLOSE | High/Low/Close (HLC) |
| COMBO_LINE_BAR | Combination Line and Bar |
| COMBO_HLC_BAR | Combination HLC and Bar |
| COMBO_LINE_AREA | Combination Line and Area |
| COMBO_LINE_LINE | Combination Line and Line |
| COMBO_HLC_AREA | Combination HLC and Area |
| PIE_2D | 2D Pie |
| PIE_3D | 3D Pie |
| BAR_3D | 3D Bar |
| AREA_3D | 3D Area |
| LINE_3D | 3D Line |
| HILOCLOSE_3D | 3D HLC |
| COMBO_LINE_BAR_3D | 3D Combination Line and Bar |
| COMBO_LINE_AREA_3D | 3D Combination Line and Area |
| COMBO_LINE_LINE_3D | 3D Combination Line and Line |
| COMBO_HLC_BAR_3D | 3D Combination HLC and Bar |
| COMBO_HLC_AREA_3D | 3D Combination HLC and Area |
| FLOATINGBAR_3D | 3D Floating Bar |
Comparing graphs
You can compare two graphs using addValue() method with different set of values GDChart will automatically create a new plot for you, making comparative analysis easy.
<?PHP
$gdc = new GDChart(GDChart::LINE);
// add data points
$gdc->addValues(array(18234, 16484, 16574, 17464, 19474));
$gdc->addValues(array(8234, 6484, 6574, 7464, 10474));
// add X-axis labels
$gdc->setLabels(array(‘2002′, ‘2003′, ‘2004′, ‘2005′, ‘2006′));
// generate chart
header(‘Content-Type:image/png’);
echo $gdc->out(300,200,GDChart::PNG);
?>
Here is the output
Working with colors
You can have different colors for the charts using setColor() and ‘bgColor’, ‘gridColor’ and ‘lineColor’ properties. This method will accept colors in hexadecimal format. Let’s try with an example
<?PHP
$gdc = new GDChart(GDChart::BAR);
$gdc->addValues(array(18234, 16484, 16574, 17464, 19474));
$gdc->setLabels(array(‘2002′, ‘2003′, ‘2004′, ‘2005′, ‘2006′));
// set titles
$gdc->title = ‘ANNUAL RAINFALL’;
$gdc->xtitle = ‘MM’;
$gdc->ytitle = ‘YEAR’;
// set colors
$gdc->setColors(array(0×0000a0));
$gdc->bgColor = 0xffffff;
$gdc->gridColor = 0xff0080;
$gdc->lineColor = 0xff0080;
// generate chart
header(‘Content-Type:image/png’);
echo $gdc->out(300,200,GDChart::PNG);
?>
You can also set a background image
$gdc->bgImage = ‘myimage.gif’;
Pie Chart
A pie chart is created in the same manner as other types of charts: initialize a GDChart object with the chart type, attach values and descriptive labels to the pie slices with the addValues() and setLabels() methods, and render the chart with out().
<?PHP
$gdc = new GDChart(GDChart::PIE_3D);
// set pie slices and labels
$gdc->addValues(array(50, 30, 100, 20));
$gdc->setLabels(array(‘UK’, ‘US’, ‘Europe’, ‘India’));
// set chart title
$gdc->title = ‘ANNUAL SALES BY LOCATION’;
// generate chart
header(‘Content-Type:image/png’);
echo $gdc->out(300,200,GDChart::PNG);
?>
We can integrate this graphs with mysql also by picking the values dynamically.
Reference: http://devzone.zend.com
<?PHP
// initialize object with chart type
$gdc = new GDChart(GDChart::BAR);
// add data points
$gdc->addValues(array(18234, 16484, 16574, 17464, 19474));
// add X-axis labels
$gdc->setLabels(array('2002', '2003', '2004', '2005', '2006'));
// set titles
$gdc->title = 'ANNUAL RAINFALL';
$gdc->xtitle = 'MM';
$gdc->ytitle = 'YEAR';
// set colors
$gdc->setColors(array(0x0000a0));
$gdc->bgColor = 0xffffff;
$gdc->gridColor = 0xff0080;
$gdc->lineColor = 0xff0080;
// generate chart
header('Content-Type:image/png');
echo $gdc->out(300,200,GDChart::PNG);
?>

Posted in
Tags: 



































