Google Charts in Rails (gc4r)

Well I m going to cover the topic how to generate charts using rails. there are number of options available such as follows

Here are the plugins through which we can generate charts in rails

1) Gruff charts http://nubyonrails.com/articles/gruff-graphing-library-for-ruby

2) Sparklines http://nubyonrails.com/articles/sparklines-graph-library-for-ruby

3) Scruffy charts http://scruffy.rubyforge.org/

4) Ziya plugin http://liquidrail.com/2007/1/4/charting-the-rails/ (flash charts)

I found google charts the best of these..( ofcourse that suited my requirements). The gc4r plugin in rails is really helpful for easy integration of google charts in your rails application.

Here is how Charts are generated by Google API.

Install: ruby script/plugin install http://gc4r.googlecode.com/svn/trunk/

Supported features:
1.line chart
2.bar chart (vertical and horizontal)
3.pie chart (both 2D and 3D)
4.title, title color and size
5.data colors and legend
6.data scaling
7.multiple axis

#Controller
use_google_charts

#In method of controller

Default Chart or Hello World Chart
@chart = GoogleChart.new

Set the width of chart
@chart = GoogleLineChart.new :width => 300, :height => 200

Set the data
dataset = GoogleChartDataset.new :data => [10,50,4,10,16]
data = GoogleChartData.new :datasets => dataset
@chart = GoogleLineChart.new :width => 300, :height => 200
@chart.data = data

add a chart title
dataset = GoogleChartDataset.new :data => [10,50,4,10,16]
data = GoogleChartData.new :datasets => dataset
@chart = GoogleLineChart.new :width => 200, :height => 150, :title => ‘Java vs. Ruby Montly Job Opportunities’
@chart.data = data

Set title in array instead of string
dataset = GoogleChartDataset.new :data => [10,50,4,10,16]
data = GoogleChartData.new :datasets => dataset
@chart = GoogleLineChart.new :width => 200, :height => 150, :title => [‘Java vs. Ruby’, ‘Montly Job Opportunities’]
@chart.data = data

Multiple data in chart
dataset1 = GoogleChartDataset.new :data => [10,50,4,10,16]
dataset2 = GoogleChartDataset.new :data => [99, 81, 25, 54, 80]
data = GoogleChartData.new :datasets => [dataset1, dataset2]
@chart = GoogleLineChart.new :width => 200, :height => 150, :title => [‘Java vs. Ruby’, ‘Montly Job Opportunities’]
@chart.data = data

Add colors
dataset1 = GoogleChartDataset.new :data => [10,50,4,10,16],:color => ‘FF0000?
dataset2 = GoogleChartDataset.new :data =>[99,81,25,54,80],:color => ‘0000FF’
data = GoogleChartData.new :datasets => [dataset1, dataset2]
@chart = GoogleLineChart.new :width => 200, :height => 150, :title => [‘Java vs. Ruby’, ‘Montly Job Opportunities’]
@chart.data = data

Define legend
dataset1 = GoogleChartDataset.new :data =>[10,50,4,10,16],:color => ‘FF0000?, :title => ‘Java’
dataset2 = GoogleChartDataset.new :data=>[99,81,25,54,80],:color => ‘0000FF’, :title => ‘Ruby’
data = GoogleChartData.new :datasets => [dataset1, dataset2]
@chart = GoogleLineChart.new :width => 200, :height => 150, :title => [‘Java vs. Ruby’, ‘Montly Job Opportunities’]
@chart.data = data

Define Axis
dataset1 = GoogleChartDataset.new :data =>[10,50,4,10,16],:color => ‘FF0000?, :title => ‘Java’
dataset2 = GoogleChartDataset.new :data=>[99,81,25,54,80],:color => ‘0000FF’, :title => ‘Ruby’
data = GoogleChartData.new :datasets => [dataset1, dataset2]
axis = GoogleChartAxis.new :axis  => [GoogleChartAxis::LEFT, GoogleChartAxis::BOTTOM]
@chart = GoogleLineChart.new :width => 250, :height => 150, :title => [‘Java vs. Ruby’, ‘Montly Job Opportunities’]
@chart.data = data
@chart.axis = axis

Define Right and X Axis
dataset1 = GoogleChartDataset.new :data=> [10,50,4,10,16],:color => ‘FF0000?,  :title => ‘Java’
dataset2 = GoogleChartDataset.new :data=>[99,81,25,54,80],:color => ‘0000FF’, :title => ‘Ruby’
data = GoogleChartData.new :datasets => [dataset1, dataset2]
axis = GoogleChartAxis.new :axis  => [GoogleChartAxis::LEFT, GoogleChartAxis::BOTTOM,  GoogleChartAxis::RIGHT, GoogleChartAxis::BOTTOM]
@chart = GoogleLineChart.new :width => 300, :height => 200, :title => [‘Java vs. Ruby’, ‘Montly Job Opportunities’]
@chart.data = data
@chart.axis = axis

Define Bar Chart:
@chart = GoogleBarChart.new :width => 300, :height => 200, :title => [‘Java vs. Ruby’, ‘Montly Job Opportunities’]

Define 3D Pie Chart:
@chart = GooglePieChart.new :width => 400, :height => 200, :title => [‘Java vs. Ruby’, ‘Montly Job Opportunities’], :chart_type  => GooglePieChart::PIE_3D

#view:
<%= image_tag @chart.to_url %>

Be Sociable, Share!

8 comments

  • Hi, this looks quite promising.

    I used scruffy and the :as => ‘png’ was causing a lot of trouble. I have also used google chart.. simply html ones. Anyway, will use the google charts with Rails now. Lets see how it turns out.. Thanks once again for this post.

    Regarding my post on your other blog.. I’m sorry it was a mistake.. had immediately removed my post 🙂 sorry for the inconvenience caused

  • dhaval parikh

    hi thanks for the comment. No worries about the other post. Let me know if you are not able to do something with gc4r i would surely help u out.

  • sure! I’ll get back to you!

  • Hi Dhaval!

    hope you’re doing great.. well I used gc4r and i used gchartrb (http://gchartrb.rubyforge.org/)

    i had some issues with gc4r – especially with datasets… don’t think I have the time to tell everything 🙁 but just that it doesn’t identify GoogleChartDataset. Anyway, something like

    @mychart.data = [1,2,3,4,5,6] #works fine with gc4r – it gives a simple line chart

    also, I will have to look into how multiple datasets work.

    I’m using Rails 2.0.2 on Windows InstantRails 😐

    –Anyway thanks a lot for the headway 🙂
    And please look at gchartrb too.. although you must already be knowing. 😉

    time for me to be off! see you!

  • Hi, nice post. I have been wondering about this topic,so thanks for posting. I will certainly be subscribing to your blog.

  • The best information i have found exactly here. Keep going Thank you

  • dipak

    How to define X and Y axis

  • Hi Dipak

    Sorry for the late reply. If I understand correctly you want to define X and Y axis titles. If so you can do it using @titleX =”whatever” and @titleY = “whatever”.

    If you want any other info do let me know.

    Thanks

Leave a Reply

Your email address will not be published. Required fields are marked *