• Stars
    star
    428
  • Rank 101,481 (Top 2 %)
  • Language
    Ruby
  • License
    MIT License
  • Created over 14 years ago
  • Updated over 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A Ruby Gem for the Google Visualization API. Write Ruby code. Generate Javascript. Display a Google Chart.

GoogleVisualr

Gem Version Build Status License

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.

tl:dr

  • In your model or controller, write Ruby code to create your chart (e.g. Area Chart, Bar Chart etc).
  • Configure your chart with any of the options as listed in Google's API Docs. E.g. Area Chart.
  • In your view, call chart.to_js(div_id) and that renders JavaScript into the HTML output.
  • You get your awesome Google chart, and you didn't write any JavaScript!

Limitations

GoogleVisualr is not a 100% complete wrapper for Google Chart Tools.

Methods and Events as 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.

Install

Assuming you are on Rails 3/4, include the gem in your Gemfile.

gem "google_visualr", ">= 2.5"

Basics

This is a basic implementation of GoogleVisualr::DataTable and 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.

<script src='https://www.google.com/jsapi'></script>

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.

<div id='chart'></div>
<%= render_chart(@chart, 'chart') %>

Chart Initializer

The initializer of GoogleVisualr::<ChartName> takes in two parameters: data_table and options.

data_table

data_table is an instance of GoogleVisualr::DataTable and contains headers and rows of data.

options

options is 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:

At the same time, you can also specify version, language and material as 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 version as 1.1 in 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 language in 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 Material design 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 material as true in the configuration options.

....
@chart = GoogleVisualr::BarChart.new(data_table, { material: true })

Listeners

For an example usage of listeners, please refer to this comment.

Besides listeners you can now also redraw charts in your JavaScript maunally by calling draw_<element id>() function. See this commit for more details.

Support

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!).

Who's Using GoogleVisualr?

I would like to collect some data about who's using this Gem. Please drop me a line.

Author

GoogleVisualr is maintained by Winston Teo.

Who is Winston Teo? You should follow Winston on Twitter, or find out more on WinstonYW.

License

Copyright Β© 2015 Winston Teo Yong Wei. Free software, released under the MIT license.