This Ruby library, GoogleVisualr, is a wrapper around the Google Visualization API and allows you to create the same visualizations with just pure Ruby; you don’t have to write any JavaScript at all.
Good for any Ruby on Rails setup whereby you prefer to work your logic in models or controllers.
Please refer to the GoogleVisualr reference site for a complete list of visualizations that you can create with GoogleVisualr.
-
In your model or controller, write Ruby code to create your visualization (e.g. Area Chart, Bar Chart, even Spark Lines etc).
-
Configure your visualization with all the options as listed in Google Visualization API Docs. E.g. Area Chart’s configuration options.
-
In your view, just call a visualization.render(div_id) method that will magically generate and insert JavaScript into the final HTML output.
-
You get your visualization, and you didn’t write any JavaScript!
Do note that GoogleVisualr is not a 100% complete wrapper for the Google Visualization API. These are not implemented for sake of simplicity:
-
JavaScript Methods for use after a visualization has been rendered in a view. E.g. Area Chart’s methods.
-
JavaScript Events for use after a visualization has been rendered in a view. E.g. Area Chart’s events.
-
Visualizations not created by Google, and a few image-only charts.
1) As gem (Rails 2.3.8):
> gem install google_visualr > rails my_app > cd my_app > vim config/environment.rb
Insert:
gem.config “google_visualr”, :version=>“1.0.0”
2) As plugin
Just install the GitHub repository into your app/vendor/plugin folder.
> rails my_app > cd my_app > script/plugin install git://github.com/msalzburg/google_visualr.git
This section describes a basic implementation of the GoogleVisualr::Visualizations::AreaChart class.
Please review the Docs for detailed documentation and advanced implementation of constructors and methods.
In your Rails controller, create a DataTable and populate with column headers, and row values.
# Create DataTable data = GoogleVisualr::DataTable::DataTable.new
# Add Column Headers data.add_column(‘string’, ‘Year’ ) data.add_column(‘number’, ‘Sales’) data.add_column(‘number’, ‘Expenses’)
# Add Rows and Values data.add_rows(4) data.set_value(0, 0, ‘2004’) data.set_value(0, 1, 1000) data.set_value(0, 2, 400) data.set_value(1, 0, ‘2005’) data.set_value(1, 1, 1170) data.set_value(1, 2, 460) data.set_value(2, 0, ‘2006’) data.set_value(2, 1, 1500) data.set_value(2, 2, 660) data.set_value(3, 0, ‘2007’) data.set_value(3, 1, 1030) data.set_value(3, 2, 540)
# Initialize a visualization (area chart) and pass options and the DataTable @chart = GoogleVisualr::Visualizations::AreaChart.new({ :width => 400, :height => 240 }, data )
In your Rails layout, load Google Ajax API in the head tag, at the very top.
<script src=‘www.google.com/jsapi’></script>
In your Rails view, render visualization.
<div id=‘chart’></div> <%= @chart.render(‘chart’) %>
Please submit all feedback, bugs and feature-requests to GitHub Issues Tracker.
Please also feel free to clone/fork the project, and improve it further!
GoogleVisualr is maintained by Winston Teo, and his Ninja Penguin.
Who is Winston Teo? Find out more on WinstonYW, LinkedIn, or follow Winston on Twitter.
Copyright © 2010 Winston Teo Yong Wei. Free software, released under the MIT license.