Bryan's thoughts on web design and development

Simple dynamic graphs with PHP+mysql

Graphing in PHP has always been an annoyance for me - while it would be cool feature to have, the need doesn’t come up nearly enough to warrant paying the exorbitant fees associated with most commercial graphing libraries. Besides, commercial PHP libraries always bring out the miser in me - it just doesn’t seem to fit with the open source model. Enter Elliot Brueggeman’s Lightweight PHP Graphing Library - with it I’ve finally found a PHP graphing library that’s simple to operate, generates image-based graphs, and is free to boot.



This is a graph of future email client requests on xobni.com. It tells us what clients other than Outlook people are using to manage their email, and thus helps us plan out future directions for Xobni Insight.

Elliot’s graphing library supports bar, line, and pie charts. As far as I can tell, it doesn’t yet support multiple lines per graph, and the graphs are not anti-aliased (yuck!). But for admin-only graphs, its simplicity is its best asset. The dynamic graph above was produced by this code below:

include("lib/phpgraphlib.php");
include("lib/phpgraphlib_pie.php");

$title = "Future Requests by Client";

$dataArray=array();
$sql = "SELECT client,COUNT(id) as counter FROM Users GROUP BY client";
$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result)) {
	$counter = $row["counter"];
	$dataArray[$row["which_client"]]=$counter;
}

$graph=new PHPGraphLibPie(265,265);
$graph->addData($dataArray);
$graph->setTitle($title);
$graph->setLabelTextColor("50,50,50");
$graph->setLegendTextColor("50,50,50");
$graph->createGraph();

That’s all there is to creating an image-based graph with Elliot’s library. In some ways, I prefer this over the flash charting library I’ve used in the past - while the flash animation is beautiful, it’s nice that you can just right click and save these image-based graphs for use in powerpoint presentations, etc. Besides, while it took me a good 5 hours to set up the flash library, I had this set up in less than an hour of experimentation.

Download needed files here

There are a few other svg+css+javascript graphing libraries that I plan to check out soon; but for now, the Lightweight PHP graphing library did the trick by producing simple graphs with relatively little effort.

4 Responses to “Simple dynamic graphs with PHP+mysql”

  1. got it to work like a charm, here is the total code for a mac osx, running MAMP with a database, etc… all documented below…

    file is filename.php
    ——–cut here—————-
    addData($dataArray);
    $graph->setTitle($title);
    $graph->setLabelTextColor(”50,50,50″);
    $graph->setLegendTextColor(”50,50,50″);
    $graph->createGraph();
    ?>
    ————-CUT HERE——————————–

  2. the program ate all the code i pasted!!!!

  3. Yeah, wordpress seems to have munched your code. Wordpress isn’t very good about that. How about trying again but this time surrounding your code with tags?

  4. To draw antialiased pie charts, you could replace the standard php arc/ellipse function with the following: http://mierendo.com/software/antialiased_arcs/

Leave a Reply