Plugin details

Google Chart 4 Rails - Ruby wrapper for Google Charts API.

Websitehttp://code.google.com/p/gc4r/ Repositoryhttp://gc4r.googlecode.com/svn/trunk Author Iulian Costan Tags Graphs, Charts, Google LicenseMIT

Documentation

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

Example
=======

1. Hello World

    @chart = GoogleChart.new




2. ok, make a line chart with size 300x200

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




3. add some random 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




4. 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




5. wow, title is too big let's wrap it

    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


Notice: title is now an array instead of string

6. ok now, let's add more data

    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




7. hmmmm, let's put some 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




8. better but not good enough, which one is java which one is ruby, let's put the 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




9. now, it will be nice to have 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




10. and two more, right and second 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




11. so far so good, now i want a bar chart

    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 = GoogleBarChart.new :width => 300, :height => 200, :title => ['Java vs. Ruby', 'Montly Job Opportunities']
    @chart.data = data
    @chart.axis = axis




12. or nice 3d pie chart

    dataset1 = GoogleChartDataset.new :data => 50, :color => 'FF0000', :title => 'Java'
    dataset2 = GoogleChartDataset.new :data => 90, :color => '0000FF', :title => 'Ruby'
    data = GoogleChartData.new :datasets => [dataset1, dataset2]
    @chart = GooglePieChart.new :width => 400, :height => 200, :title => ['Java vs. Ruby', 'Montly Job Opportunities'],
      :chart_type  => GooglePieChart::PIE_3D
    @chart.data = data




13. if you like it, then

Install the plugin and put the following in your controller

class AnalysisController < ApplicationController
  use_google_charts
end



in view:

<%= image_tag @chart.to_url %>

Further Documentation

There is currently no advanced documentation for this plugin.

New documentation

Edit plugin | (0 older versions) | Last edited by: Guest, about 1 month ago