Description:
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
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.
4:31pm 27 Apr 08