Using Google Charts from rails

A week ago i got an interesting ticket – update the Newsdesk statistics charts for printing and sending via email. Although people here are quite satisfied whith the look and feel of our current solutions Fusion Charts, we feel that not being able to print the whole statistics page in one job is a huge drawback. Browsing through the different alternatives for graphic charting, once again I stumbled upon a free of charge, easy to use google service called Google Charts.

Google Charts seemed pretty straight forward and fairly easy to implement so it was an easy decision to go for it. My goal was to substitute the Fusion Charts with Google Charts in the event of printing or emailing the stats (we keep FC for online viewing since, after all, it provide a huge amount of fluff and motional joy for the end user).

I found no less than three different ruby wrappers for the Google Charts API:

To save you the time, here is a quick comparison between the three:

Googlecharts
install: gem install googlecharts

Example:

Gchart.bar(:data => google_data, 
  :size => "#{options[:width]}x#{options[:height]}", // SIZE DEFINED AS STRING WITH BOTH WIDTH AND HEIGHT, NOT GOOD
  :axis_with_labels => 'x,y', 
  :axis_labels => [labels, [0, (max/2).to_i, max.to_i]],
  :format => 'image_tag', 
  :bar_colors => 'E04300',
  :max_value => max, 
  :bar_width_and_spacing => "#{options[:bar_width]},#{options[:bar_spacing]}")

Pros:
- it’s possible to set a custom key with stuff that are not included in the wrapper(grids for example)
- nice and clean syntax with options-hash like arguments
- possible to specify ouput format such as file or image_tag

Cons:
- not implemented grid, line_thickness, and ranges (to my knowledge). This is fairly easy to implement by yourself using the custom option though.

gchartrb
install: gem install gchartrb

Example:

GoogleChart::BarChart.new("#{options[:width]}x#{options[:height]}", options[:name]) do |lc| // SIZE DEFINED AS STRING WITH BOTH WIDTH AND HEIGHT, NOT GOOD
  lc.show_legend = false
  lc.data 'Visningar', google_data, 'E04300'
  lc.axis :x, :color => '333333', :font_size => 10, :alignment => :center, :labels => labels
  lc.axis :y, :range => [0,max], :color => '333333', :font_size => 10, :alignment => :center
  lc.width_spacing_options(:bar_width => options[:bar_width], :bar_spacing => options[:bar_spacing]) 
end.to_url 
image_tag(url)

Pros:
- easy handling of axises (above)
- block syntax is always nice

Cons:

google-charts-on-rails
install: script/plugin install http://google-charts-on-rails.googlecode.com/svn/google_charts_on_rails/

Example: (i have none of my own)

GoogleChart.100x200_pie(10,20,40,30).to_url

or

sales_chart = GoogleChart.newsales_chart.type = :pie
sales_chart.height = 200
sales_chart.width = 150

Pros:
- it’s not nothing

Cons:

- weird method_missing -like syntax
- poor doc
- much of Google Charts API not implemented

Conclusion:
You should use either gchartrb or googlecharts depending on code taste and exact needs.
Google Charts API is not a good solution for exact and scientificly correct graphs since it seems to render the data, labels and grids independently. Labels for example are spaced evenly along a certain axis and have no connection to the actual data points in the graph.

Posted by marten Posted in Ruby on Rails Tagged , , , , , , ,
  • http://www.newsdesk.se Jakob Törnell

    For some reason I feel kind of exkluded in the development of the site. I quess it´s the “Ruby on Rails” who mess my brain up.

  • http://www.pastbedti.me/2008/11/the-developers-behind-newsdesk-have-a-weblog/ The developers behind Newsdesk have a weblog! – pastbedti.me

    [...] Finally my friends at newsdesk have a blog. They have started off real good with a comparison of three ruby wrappers for Google charts. [...]

  • http://www.stjernstrom.se/ Mathias Stjernström

    Which one did you choose?

  • http://www.newsdesk.se Richard

    Mathias: We are using googlecharts, gem version 1.3.6.

  • developer

    Can i save these charts in a pdf format?

blog comments powered by Disqus