December 6, 2007

Dynamically Generated Charts

I've always wondered what's behind www.google.com/chart, the strange directory used by Google to dynamically generate charts. They use this charts at Google Video to show stats like this one (if you're brave, look at the image location):



The problem is that Google also adds a hash to the URL so you can't play around with the parameters and create your own charts. But now there's Google Chart API, a way to make these charts available to everyone.

"The Google Chart API returns a PNG-format image in response to a URL. Several types of image can be generated: line, bar, and pie charts for example. For each image type you can specify attributes such as size, colors, and labels. You can include a Chart API image in a webpage by embedding a URL within an <img> tag. When the webpage is displayed in a browser the Chart API renders the image within the page."

All these charts can be created dynamically by building some admittedly complicated URLs.







Let's look at the 3D pie chart's URL:

http://chart.apis.google.com/chart?cht=p3&chd=s:Uf9a&chs=200x100&chl=A|B|C|D

cht indicates the chart type, chd provides the plotted data that's encoded using letters and digits (for example, U represents 20, f - 31, 9 - 61, a - 26), chs is used for chart's size, while chl separates labels using the pipe character.

This is a great way to create simple charts programmatically and I'm sure many web apps will use them. The query limit of 50,000 queries per user per day is more than sufficient.

Update: It seems that the limit is not that generous. "The user is the webpage that links to the chart. We would like to make sure that all users get their charts fast and reliably. Therefore we may temporarily block users (websites) that exceed the limit. A chart request is not counted if the chart image is cached by the browser or by a proxy," clarified Uwe Maurer from Google Zürich.

7 comments:

  1. > The query limit of 50,000 queries per user per day is
    > more than sufficient.

    Depends on the use case. Even including such a chart in a blog post is risky because e.g. as soon as you're dugg, and the story stays on the digg frontpage for some time, you may get more than 50,000 hits.

    ReplyDelete
  2. It might be usefull for small company's business web applications, like some administrative intranet or something. But I doubt the copiright terms in wich it's released allows you to use it that way...

    ReplyDelete
  3. Philipp, I think the limit says you have 50,000 queries / IP / day. There's no key to track your usage and I don't think the limit is restricted to a site.

    ReplyDelete
  4. You can always cache the image ...

    ReplyDelete
  5. > I don't think the limit is restricted to
    > a site

    If Google reads their help group we'll soon have an answer...

    ReplyDelete
  6. This is an excellent feature.
    jpGraph produces good graphs in PHP but the images arent smooth and 'cool' enough.

    But with the 50k limit per day, it's going to be difficult to rely on this for websites.

    ReplyDelete
  7. Assuming the charts don't change too frequently, you can cache them on your servers or rely on Google Gadgets' caching mechanism.

    ReplyDelete