The Google Charts is a simple API for generating charts and graphs. It’s designed so it can easily be embedded into web sites. The API consists of sending different parameters to a URL hosted on Google’s servers. It makes more sense when looking at an example:

http://chart.apis.google.com/chart?cht=p3&chd=t:60,40&chs=250x100&chl=Hello|World

This URL, copied from the manual, will display a pie chart with 2 distinct pieces of data; Hello, and, World. Examining the URL you can see that it consists of data, expressed through the “chd” parameter, labels “chl” and other properties such as the dimensions “chs”. The type of the chart is signified by the “cht” parameter.

gchartrb is a Ruby wrapper for the Google Charts API. It provides a neat interface for modifying all the properties, and adding data without having to get your hands dirty by creating URLs. For example using the GoogleChart::PieChart class it is very easy to create a simple pie chart:

GoogleChart::PieChart.new('320x200', "Chart Title",false) do |chart|
    data.each {|entry|
        chart.data entry['name'], entry['cost']
    }
end

Here you can see that the chart object is being manipulated to add data from an array of objects. A URL can then be generated for this chart by calling the “to_url” method.

An image of this chart can be downloaded using the following simple code.

uri = URI.parse(chart.to_escaped_url)
   Net::HTTP.start(uri.host) { |http|
     resp = http.get("#{uri.path}?#{uri.query}")
     open("chart.png", "wb") { |file|
       file.write(resp.body)
     }
   }

Notice that “to_escaped_url” is being called - this brings the URL back in a format which can be downloaded directly.

These APIs have allowed me to easily visualize the phone call data obtained through my Virgin Media script.

[caption id=”attachment_23” align=”alignnone” width=”300” caption=”A visualization of my phone call data”]A visualization of my phone call data[/caption]