While recently experimenting with charting for web applications, and the various charting libraries popular among the PHP community, I came across the Google Chart API. I’ve since decided to outsource, as it were, all my charting needs to the API, a service provided by Google to generate just about any graph you could possibly need in your web application; after the jump, here’s why you should do the same.
After all, the APIs offer:
1. Visually appealing graphs (just pick some nice colours!)
2. No processing strain on our server – the APIs are good for 250K hits per day
3. Speedier page loads – charts on another domain is a performance best practice
4. Greater reliability – image libraries (gd or otherwise) are often the first php extension to break in a server upgrade / move.
Charting has always been one of those relatively insignificant areas of a website that presents some serious issues from a technical standpoint. Serving just a couple of uncached graphs on a page every few seconds can create major load on any server. Google is now seriously into charting in its applications, with some obvious examples including Google Analytics (although this is now client-side Flash-based charting) and Google Trends. With this Chart API, you can now use the power of Google’s in house data graphing knowledge to produce visually attractive illustrations for your page without a single additional HTTP request on your server.
Here’s a sample chart URL:
And here’s the chart generated by that URL:
A simple, clean and effective solution. As you can see, the data for the dataset, the labels and the general chart properties are passed to the APIs through standard URL query parameters. You can easily extract your dataset from your database, implode() it with some semicolons and insert it straight into the URL of the <img> tag.
Google offers a relatively simple, convenient and powerful solution for generating graphs. So why go through the trouble of using dated, third-party libraries? Dive into the thorough API docs and give Google’s Chart API a go.