IndoLance.com | Home

PHP: Simple Dynamic Graphs with PHP + MySql

May 22, 2008 Category: Computers, PHP & SQL, Programming Stuff | Tags: , ,

PHP: 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 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.

Related Posts

Comments

Leave a Reply

IndoLance.com © 2007 - 2008 - All Rights Reserved