Create line or bar charts and graphs within php. They can be output directly or image files created.
Create line or bar charts and graphs within php. They can be output directly or image files created.
Usage
Simple bar chart
<?php require_once 'Chart.php'; $bars = array(41,52,53,12,85,61,53,8,79,10,92,36); $graph = new Chart(); $graph->addBars($bars, 'ff0000'); $graph->output(); ?>
To output the chart to an image instead of directly to the browser:
$graph->output('filename.png');
Add X labels
<?php
require_once('Chart.php');
$bars = array(41,52,53,12,85,61,53,8,79,10,92,36);
$dates = array('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
$graph = new Chart(); $graph->addBars($bars, 'ff0000');
$graph->addXLabels($dates, '000000');
$graph->output();
?>
Add Y scale
<?php
require_once('Chart.php');
$bars = array(41,52,53,12,85,61,53,8,79,10,92,36);
$dates = array('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
$graph = new Chart();
$graph->addBars($bars, 'ff0000');
$graph->addXLabels($dates, '000000');
$graph->addYScale('000000');
$graph->output();
?>
Combine another bar graph
<?php
require_once('Chart.php');
$bars1 = array(41,52,53,12,85,61,53,8,79,10,92,36);
$bars2 = array(61,22,83,42,95,21,67,3,19,99,11,64);
$dates = array('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
$graph = new Chart();$graph->addBars($bars1, 'ff0000');
$graph->addBars($bars2, '0000ff');
$graph->addXLabels($dates, '000000');
$graph->addYScale('000000');
$graph->output();
?>
Style with gradients
<?php
require_once('Chart.php');
$bars1 = array(41,52,53,12,85,61,53,8,79,10,92,36);
$bars2 = array(61,22,83,42,95,21,67,3,19,99,11,64);
$dates = array('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
$graph = new Chart();
$graph->gradient = true;
$graph->addBars($bars1, 'ff0000');
$graph->addBars($bars2, '0000ff');
$graph->addXLabels($dates, '000000');
$graph->addYScale('000000');
$graph->output();?>
Add a line graph
<?php
require_once('Chart.php');
$bars1 = array(41,52,53,12,85,61,53,8,79,10,92,36);
$bars2 = array(61,22,83,42,95,21,67,3,19,99,11,64);
// create line data based on sine waves
$lines = array();
for($i=0; $i < 36; $i+=5) {
$x = $i + 5;
$y1 = 1.5 + sin($i*0.2);
$y2 = 3.0 * (1.5 + sin($i * 0.2));
$lines[] = $y1*20; $lines[] = $y2*20;
}
$dates = array('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
$graph = new Chart();
$graph->
gradient = true;
$graph->addBars($bars1, 'ff0000');
$graph->addBars($bars2, '0000ff');
$graph->addLines($lines, 'ffcc00');
$graph->addXLabels($dates, '000000');
$graph->addYScale('000000');
$graph->output();
?>
Requires:
PHP 5.0 / GD Library
Installation:
The script should work with GD version 2, PHP 5.0 and Linux and Windows servers
Licensed under the GNU Lesser General Public License as published by the Free Software Foundation.
Copyright © 2007 Chris Tomlinson.
Download
Version Changes
Changes:
0.1 – first release
0.2 – added most default values

The code relies on the font being available. The font file required is ‘arial.ttf’.
If you put this font into the same place as Chart.php it should work.
in the examples on my computer are no labels Jan, Feb ..
Any idea ?
Thanks
Klaus-Dieter
Very sorry for that last comment, hadn’t seen the file download! I can get everything working now except for the axis labeling, for some reason this won’t render.
I am attempting to use your class on my site but am having trouble. My phpinfo indicates that PHP 5.3.4 and GD 2.0.34 are running on my server. I have moved Chart.php into the same directory as the page which uses it and included the code for the first chart. Unfortunately, all I get is gibberish:
�PNG IHDR�,b�r�UIDATx���Aj�@Am�����Bg�Q��:�﮽�P�q�
Any help would be greatly appreciated!
Thanks,
Mitchell
Try adding this:
header("Content-type: image/png);just before you do $graph->output();
Note that this won’t work if you’ve ‘echo’d anything before this. If you use
$graph->output("filename.png");instead, then you can view the graph created with
Hello Chris, I have integrated the chart class but I have troubles with negative numbers. I don’t want to fiddle with the addBar method, is it possible that I have missed some settings to handle them? Thanks for your work.