GithubHelp home page GithubHelp logo

runt18 / google_visualr Goto Github PK

View Code? Open in Web Editor NEW

This project forked from winston/google_visualr

0.0 3.0 0.0 781 KB

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

Home Page: http://googlevisualr.heroku.com

License: MIT License

Ruby 74.73% HTML 9.26% JavaScript 16.01%

google_visualr's Introduction

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.

google_visualr's People

Contributors

andycroll avatar bernardomenezes avatar ghiculescu avatar greinacker avatar hartator avatar juanitofatas avatar k1w1 avatar kandadaboggu avatar lawso017 avatar ledermann avatar lzap avatar raphaelcm avatar sigmike avatar winston avatar yatsyk avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.