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>\n  <head>\n    <!--Load the AJAX API-->\n    <script type=\"text/javascript\" src=\"http://www.google.com/jsapi\"></script>\n    <script type=\"text/javascript\">\n\n      // Load the Visualization API and the piechart package.\n      google.load('visualization', '1', {'packages':['piechart']});\n\n      // Set a callback to run when the Google Visualization API is loaded.\n      google.setOnLoadCallback(drawChart);\n\n      // Callback that creates and populates a data table,\n      // instantiates the pie chart, passes in the data and\n      // draws it.\n      function drawChart() {\n        // Create our data table.\n        var data = new google.visualization.DataTable(<%= data_table.to_json %>);\n\n        // Instantiate and draw our chart, passing in some options.\n        var chart = new google.visualization.PieChart(document.getElementById('chart_div'));\n        chart.draw(data, {width: 400, height: 240, is3D: true, title: 'My Daily Activities'});\n      }\n    </script>\n  </head>\n\n  <body>\n    <!--Div that will hold the pie chart-->\n    <div id=\"chart_div\"></div>\n  </body>\n</html>\n"

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

Copyright

Copyright © 2010 Miguel Fonseca. See LICENSE for details.