PHP Chart Class

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

Download chart.php v0.2

Version Changes

Changes:
0.1 – first release
0.2 – added most default values

6 Comments

  1. Christo says:

    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.

  2. Klaus-Dieter Brinkmann says:

    in the examples on my computer are no labels Jan, Feb ..
    Any idea ?
    Thanks
    Klaus-Dieter

  3. 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.

  4. 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

    • admin says:

      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

      <img src="filename.png"/>
  5. andrea (mr) says:

    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.

Sorry, the comment form is closed at this time.

Isometric Projection with JavaScript in HTML5 vs Flash and Actionscript

Recently I decided to build part of a game that used an isometric projection for displaying the game board using HTML5.
Dirty plagiarist that I am, I found an excellent (albeit very old) tutorial by Danko Kozar for doing this. However it was written in Actionscript. No bother – I just modified the code to work in HTML5. This meant I could more or less ignore the theory of how isometric projection works and just get down to the business of doing it. Porting the code was easy. As it says in the tutorial:

These tutorials can be used for programming an isometric game in many programming languages not just in Flash. They are more about game programming than about Flash programming.

Here’s the swf from Danko’s tutorial:
Details »

Dynamic Leaderboard

So you want to create a dynamic leaderboard that changes on the fly using jQuery? Well do ya?

Here’s the scenario: you have some cool (to you at least) data that you want to put up on a webpage/kiosk/touchscreen/smartphone that animates beautifully in real time to update the results.

Details »

Handy Linux Shortcuts and How To’s

Here’s some linux tricks I use in my line of work. I find them useful, you may not. You decide.
Details »

Friend Photomosaics


Turn your Facebook profile picture into a photomosaic made up of your friends profile pictures.

Details »

PHP Chart Class

Create line or bar charts and graphs within php. They can be output directly or image files created.

Details »

Drawing Gallery

Create your own drawing

Details »

Create Flash Drawing

A collection of many spontaneous drawings generated with Flash

Over a thousand random drawings created since 2003.

Details »

Font List


Details »

PHP Thumbnail Script

A script for converting images to different sizes inline, with support for server caching.

Details »

Flash Chess

Chess program designed to learn from player interaction. Made with flash and PHP server technology, the finished version will (in theory) be able to beat all players that aren’t as good as players that have played the system often.

Details »