December 08, 2007

Google's API for creating Charts and Graphs

Google just keeps innovating! They just released their new API which is all about creating charts and graphs. It’s called Google Chart API. In one look, it’s a down to earth API for creating charts and graphs by simply passing values to an URL. This is what struck me the most! The API has only an URL to which you pass values like chart size, data and chart type. In turn the API creates the chart as image and you have it! Creating chart is as simple as that. Let’s have a peep inside…

This might be one of the best API’s available now. The reason? Everything is through the URL. Google defines their new charting API as:


The Google Chart API is an extremely simple tool that lets you easily create a chart from some data and embed it in a web page. You embed the data and formatting parameters in an HTTP request, and Google returns a PNG image of the chart. Many types of chart are supported, and by making the request into an image tag you can simply include the chart in a web page.


The API provides different types of charts like Line, Bar, Pie, Venn diagram etc. To create any chart, we just have to pass some parameters to the URL: http://chart.apis.google.com/chart, specifying chart size, type and date. The generic syntax will be as follows :

http://chart.apis.google.com/chart?<parameter1>&<parameter2>&<parametern>
For a simple pie chart, here is the code (URL):




So, you just created your Hello World chart using the API. All charts are generated as PNG images. Now lets look at the analyze the “Hello World” chart we just wrote.
You should have noticed that we passed 4 parameters, cht, chd, chs and chl. “ch” obviously stands for chart.. cht represents the chart type, chd the data, chs the size of chart and chl the charts label. The API provides different type of data encoding like simple, text and extended encodings.

Here’s some example..





Let me dig deeper and see how I can make use of this library! :)

No comments :