Plotting a sparklines chart using gchartrb and Google Chart API On Thursday, December 13, 2007

There are two ways to create a sparkline chart using the Google Chart API

  • According to this post on the Google Chart API mailing list, it is possible to plot a sparklines chart using some undocumented parameters
  • Another undocumented feature is the presence of a new chart type called lfi, which is used in this blog post (right at the end under ‘Did someone mention Edward Tufte?’)

Here is some sample code using gchartrb. It uses the custom parameters feature to specify the undocumented part as a hash just before generating the URL.


# Plotting a sparklines chart
sparklines = GoogleChart::LineChart.new('100x50', nil, false)
sparklines.data "Test", [4,3,2,4,6,8,10]
sparklines.show_legend = false
sparklines.axis :x, :labels => [] # Empty labels
sparklines.axis :y, :labels => [] # Empty labels
puts sparklines.to_url(:chxs => "0,000000,10,0,_|1,000000,10,0,_")

Generates the following URL

http://chart.apis.google.com/chart?chxl=0:||1:|&chd=s:YSMYkw9&chxs=0,000000,10,0,_|1,000000,10,0,_&cht=lc&chxt=x,y&chs=100x50

and the following image

Google Chart

You can encapsulate this inside a method and call it repeatedly to generate a sparkline chart.

Once the option is documented, I should be adding it to gchartrb.