Nav links

Sunday, 4 February 2007

PHP Graphing

In the past I've used the GD library in PHP to manually create graphs. For example, the bar charts that regularly appear in this blog are formed on the fly by a specially created program. However, it's often quicker and easier to use graphing libraries that other people have made available, especially if your graphing requirements are relatively standard.

The most full-featured for graphing library for PHP appears to be JpGraph, which is free for non-commercial use. Oddly, its excellent html-formatted manual is not provided officially online, but only as part of the JpGraph download. This would probably put off a number of people who like to see proper documentation before they go to the trouble of downloading and installing a package. If you do a Google search for "jpgraph manual 2005" you may find a copy hosted elsewhere.

Another potential disincentive to using JpGraph is its size. It's a 5MB download, which uncompresses to 9MB. However, much of that won't be necessary to install onto your live site, as it includes documentation, images and library code. To work out what you do need, just upload your script that references JpGraph, but don't upload JpGraph itself. Then, when you run it, an error message stating the files which are missing will be displayed. Just install those files, and try again. For my project, that whittled it down to a very reasonable amount of just over 300 kB.

As for JpGraph itself, it is an excellent product. It's easy to use, highly configurable, and has the best documentation that I've seen in a long time.

Update 16 Feb: I have used JpGraph to produce my new Australian Weather Chart tool. Although I could have built the charts from scratch, to create something as complex as the Weather Chart, I'd be tweaking it for weeks, and even then I'd just have to start from scratch again for the next one. Another point to note is the extreme customisability of JpGraph. It let me overlay the rainfall bar chart on top of the temperature line chart with no fuss, and happily provided two y-axes for these measurements. In addition, the plethora of formatting options ensured that any ugliness in the graph was entirely my fault. Finally, the extremely simple method of using caching ensured that no programmer would have an excuse not to use it.