Description

A Ruby interface to manipulate and populate data for Google Interactive Charts.

Example

require 'erb'
require 'rubygems'
require 'google_visualization'

include Google::Visualization

data_table = DataTable.new
data_table.add_column DataColumn.new(DataType::STRING, 'Task')
data_table.add_column DataColumn.new(DataType::NUMBER, 'Hours per Day')
data_table.add_row ['Work', 11]
data_table.add_row ['Eat', 2]
data_table.add_row ['Commute', 2]
data_table.add_row ['Watch TV', 2]
data_table.add_row ['Sleep', 7]

page = <<HTML
<html>
  <head>
    <!--Load the AJAX API-->
    <script type="text/javascript" src="http://www.google.com/jsapi"></script>
    <script type="text/javascript">

      // Load the Visualization API and the piechart package.
      google.load('visualization', '1', {'packages':['piechart']});

      // Set a callback to run when the Google Visualization API is loaded.
      google.setOnLoadCallback(drawChart);

      // Callback that creates and populates a data table,
      // instantiates the pie chart, passes in the data and
      // draws it.
      function drawChart() {
        // Create our data table.
        var data = new google.visualization.DataTable(<%= data_table.to_json %>);

        // Instantiate and draw our chart, passing in some options.
        var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
        chart.draw(data, {width: 400, height: 240, is3D: true, title: 'My Daily Activities'});
      }
    </script>
  </head>

  <body>
    <!--Div that will hold the pie chart-->
    <div id="chart_div"></div>
  </body>
</html>
HTML

template = ERB.new(page)
print template.result

Copyright

Copyright © 2010 Miguel Fonseca. See LICENSE for details.