Tuesday, April 14, 2009

Google Chart API is Cool

I've been working on an internal web application to track sales, which has given me a good reason to dive into Django. I've read some swooning reviews of Django, but have to admit that I'm somewhere between lukewarm and glowing in my opinion of it right now. It's a good framework but does have a modest learning curve. It took me a bit to get comfortable with how the various Django pieces fit together but I love the compactness of the code.

Since I'm writing a sales application, I needed a way to produce charts. I've played around with the <canvas> tag on a previous project, but really wanted something higher level. My boyfriend Jerry suggested I look at the Google Chart API. It's wonderfully simple: you build a URL like http://chart.apis.google.com/chart?cht=p3&chd=t:60,40&chs=250x100&chl=Hello|World and use it in an <img> tag. Google renders up your chart as a PNG and sends it back to your browser, like this:

Google gives you a lot of chart types and formatting options, most of them well documented, though a few tricky option combinations could benefit from more verbose explanations. Fortunately it's pretty easy to experiment in the browser -- just tweak the URL and refresh.

My only criticism is the brevity of the URL parameters (cht is chart type, chxl is axis labels, etc.) and the various ways of delimiting multivalued parameters (data takes a comma delimited list, labels are pipe '|' delimited) can be trying to work with at times. Still, after a couple of hours of modest twiddling, I had a couple of Python functions to build URLs for a bar chart and pie chart, respectively and my sales app has two respectable looking charts on the home page. Google Chart API is definately worth taking a look at.

1 comment:

Ville said...

There are already a couple of Python wrappers for the Google Chart API.

I've used GChartWrapper for making a graph showing compiler warning trends.