///Outsource your charting to Google Chart API!

Outsource your charting to Google Chart API!

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.

The charts are actually served directly from Google’s servers. The API guide explains this in more detail, but essentially your graph is constructed from a serious of GET URL query parameters, and can be accessed from that same URL at any time – essentially serving as a permalink. Now, you can then load that image onto your servers, or you can just reference it via an <img> tag in your page. The moment you go down this path, your users get to communicate directly with Google’s servers, saving your server load and quite possibly taking advantage of Google’s enhanced content delivery network in the process. (Just as they’ve been doing with javascript libraries.)

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.

2010-05-20T17:38:02+00:00 May 28th, 2008|General|0 Comments

About the Author:

Leave A Comment