A Ruby Gem for the Google Visualization API. Write Ruby code. Generate Javascript. Display a Google Chart.
GoogleVisualr, is a wrapper around the Google Charts that allows anyone to create beautiful charts with just plain Ruby. You don't have to write any JavaScript at all.
It's good for any Ruby (Rails/Sinatra) setup, and you can handle the entire charting logic in Ruby.
Please refer to the GoogleVisualr API Reference site for a complete list of Google charts that you can create with GoogleVisualr.
chart.to_js(div_id)and that renders JavaScript into the HTML output.
GoogleVisualr is not a 100% complete wrapper for Google Chart Tools.
Methodsand
Eventsas described in Google's API Docs - for use after a chart has been rendered, are not implemented because they feel more natural being written as JavaScript functions, within the views or .js files.
Assuming you are on Rails 3/4, include the gem in your Gemfile.
gem "google_visualr", ">= 2.5"
This is a basic implementation of
GoogleVisualr::DataTableand
GoogleVisualr::AreaChart.
For detailed documentation, please refer to the GoogleVisualr API Reference or read the source.
In your Rails layout, load Google Ajax API in the head tag, at the very top.
In your Rails controller, initialize a GoogleVisualr::DataTable object with an empty constructor.
data_table = GoogleVisualr::DataTable.new
Populate data_table with column headers, and row values.
# Add Column Headers data_table.new_column('string', 'Year' ) data_table.new_column('number', 'Sales') data_table.new_column('number', 'Expenses')Add Rows and Values
data_table.add_rows([ ['2004', 1000, 400], ['2005', 1170, 460], ['2006', 660, 1120], ['2007', 1030, 540] ])
Create a GoogleVisualr::AreaChart with data_table and configuration options.
option = { width: 400, height: 240, title: 'Company Performance' } @chart = GoogleVisualr::Interactive::AreaChart.new(data_table, option)
In your Rails view, render the Google chart.
The initializer of
GoogleVisualr::takes in two parameters:
data_tableand
options.
data_table
data_tableis an instance of
GoogleVisualr::DataTableand contains headers and rows of data.
options
optionsis a hash of configuration options for the Google chart (e.g. width, height, colors etc).
The available configuration options are exactly the same as those specified in Google's API Docs.
For example: - Configuration options for AreaChart - Configuration options for BarChart
At the same time, you can also specify
version,
languageand
materialas configuration options.
version
The default version of Google Charts library loaded is
1.0.
However, some charts (e.g. Gantt Charts) require the latest version of the Google Charts library, so you will have to specify
versionas
1.1in the configuration options
.... @chart = GoogleVisualr::GanttChart.new(data_table, { version: '1.1' })
Read more about it on Google's API Docs.
language
The default locale of all Google Charts is
en.
You can override this default by specifying
languagein the configuration options.
.... @chart = GoogleVisualr::BarChart.new(data_table, { language: 'ja' })
Read more about it on Google's API Docs.
material
Google has also enabled
Materialdesign for some charts:
In 2014, Google announced guidelines intended to support a common look and feel across its properties and apps (such as Android apps) that run on Google platforms. We call this effort Material Design. We'll be providing "Material" versions of all our core charts; you're welcome to use them if you like how they look.
To use material design, you will have to specify
materialas
truein the configuration options.
.... @chart = GoogleVisualr::BarChart.new(data_table, { material: true })
For an example usage of
listeners, please refer to this comment.
Besides
listenersyou can now also redraw charts in your JavaScript maunally by calling
draw_()function. See this commit for more details.
Please submit all feedback, bugs and feature-requests to GitHub Issues Tracker.
Feel free to fork the project, make improvements or bug fixes and submit pull requests (with tests!).
I would like to collect some data about who's using this Gem. Please drop me a line.
GoogleVisualr is maintained by Winston Teo.
Who is Winston Teo? You should follow Winston on Twitter, or find out more on WinstonYW.
Copyright © 2015 Winston Teo Yong Wei. Free software, released under the MIT license.