gvis 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/MIT-LICENSE +20 -0
- data/README +62 -0
- data/Rakefile +23 -0
- data/lib/data_table.rb +76 -0
- data/lib/google_visualization.rb +122 -0
- data/lib/gvis.rb +2 -0
- metadata +72 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 [name of plugin creator]
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
Gvis
|
2
|
+
====
|
3
|
+
|
4
|
+
Rails plugin that provides a Ruby wrapper for easily loading the Google Visualization API, and simple generation of the javascript required to plot the graphs
|
5
|
+
|
6
|
+
version 2.X - Rails 3.X
|
7
|
+
version 1.X - Rails 2.X
|
8
|
+
|
9
|
+
|
10
|
+
Installation
|
11
|
+
============
|
12
|
+
|
13
|
+
Rails 3:
|
14
|
+
# Gemfile
|
15
|
+
gem 'gvis', '>= 2.0.1'
|
16
|
+
|
17
|
+
Rails 2.X:
|
18
|
+
# config/environment.rb
|
19
|
+
config.gem 'gvis', :version => '< 2.0.0'
|
20
|
+
|
21
|
+
# Include the GoogleVisualization module in app/helpers/application_helper.rb
|
22
|
+
module ApplicationHelper
|
23
|
+
|
24
|
+
include GoogleVisualization
|
25
|
+
|
26
|
+
end
|
27
|
+
|
28
|
+
# Load the API, and render any graphs by placing these methods inside your layout
|
29
|
+
# app/views/layouts/application.html.erb
|
30
|
+
<head>
|
31
|
+
<%= include_visualization_api %>
|
32
|
+
<%= render_visualizations %>
|
33
|
+
...
|
34
|
+
</head>
|
35
|
+
|
36
|
+
|
37
|
+
Example
|
38
|
+
=======
|
39
|
+
|
40
|
+
# Render desired graphs in the view
|
41
|
+
# index.html.erb
|
42
|
+
<% visualization "my_chart", "MotionChart", :width => 600, :height => 400, :html => {:class => "graph_chart"} do |chart| %>
|
43
|
+
<%# Add the columns that the graph will have %>
|
44
|
+
<% chart.string "Fruit" %>
|
45
|
+
<% chart.date "Date" %>
|
46
|
+
<% chart.number "Sales" %>
|
47
|
+
<% chart.number "Expenses" %>
|
48
|
+
<% chart.string "Location" %>
|
49
|
+
|
50
|
+
<%# Add the data %>
|
51
|
+
<% chart.add_rows([
|
52
|
+
["Apples", Date.new(1998,1,1), 1000,300,'East'],
|
53
|
+
["Oranges", Date.new(1998,1,1), 950,200,'West'],
|
54
|
+
["Bananas", Date.new(1998,1,1), 300,250,'West'],
|
55
|
+
["Apples", Date.new(1998,2,1), 1200,400,'East'],
|
56
|
+
["Oranges", Date.new(1998,2,1), 950,150,'West'],
|
57
|
+
["Bananas", Date.new(1998,2,1), 788,617,'West']
|
58
|
+
]) %>
|
59
|
+
<% end %>
|
60
|
+
|
61
|
+
|
62
|
+
Copyright (c) 2009 [Jeremy Olliver], released under the MIT license
|
data/Rakefile
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/testtask'
|
3
|
+
require 'rake/rdoctask'
|
4
|
+
|
5
|
+
desc 'Default: run unit tests.'
|
6
|
+
task :default => :test
|
7
|
+
|
8
|
+
desc 'Test the gvis plugin.'
|
9
|
+
Rake::TestTask.new(:test) do |t|
|
10
|
+
t.libs << 'lib'
|
11
|
+
t.libs << 'test'
|
12
|
+
t.pattern = 'test/**/*_test.rb'
|
13
|
+
t.verbose = true
|
14
|
+
end
|
15
|
+
|
16
|
+
desc 'Generate documentation for the gvis plugin.'
|
17
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
18
|
+
rdoc.rdoc_dir = 'rdoc'
|
19
|
+
rdoc.title = 'Gvis'
|
20
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
21
|
+
rdoc.rdoc_files.include('README')
|
22
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
23
|
+
end
|
data/lib/data_table.rb
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
# Converts Ruby data structures into javascript strings for constructing the data for Google Visualization API clients.
|
2
|
+
#
|
3
|
+
# This library can be used to create a google.visualization.DataTable usable by
|
4
|
+
# visualizations built on the Google Visualization API. Output formats are raw
|
5
|
+
# JSON, JSON response, and JavaScript.
|
6
|
+
#
|
7
|
+
# written by Jeremy Olliver
|
8
|
+
class DataTable
|
9
|
+
|
10
|
+
attr_accessor :data, :columns, :column_types
|
11
|
+
|
12
|
+
def initialize(data = nil, columns = [], options = {})
|
13
|
+
@columns, @column_types = [], {}
|
14
|
+
unless columns.nil? || columns.empty?
|
15
|
+
columns.each do |type, name|
|
16
|
+
register_column(type, name, options)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
@data = data || []
|
20
|
+
end
|
21
|
+
|
22
|
+
# Registers each column explicitly, with data type, and a name associated
|
23
|
+
def register_column(type, name, options = {})
|
24
|
+
@columns << name.to_s
|
25
|
+
@column_types.merge!(name.to_s => type.to_s)
|
26
|
+
end
|
27
|
+
|
28
|
+
# Adds a single row to the table
|
29
|
+
def add_row(row)
|
30
|
+
@data << row
|
31
|
+
end
|
32
|
+
|
33
|
+
# Adds multiple rows (2D array) to the table
|
34
|
+
def add_rows(rows)
|
35
|
+
@data += rows
|
36
|
+
end
|
37
|
+
|
38
|
+
# Handle the Column definition methods (#string, #number, #date, #datetime)
|
39
|
+
def method_missing(method, *arguments)
|
40
|
+
if ["string", "number", "date", "datetime"].include?(method.to_s)
|
41
|
+
options = arguments.extract_options!
|
42
|
+
register_column(method, arguments, options)
|
43
|
+
else
|
44
|
+
raise NoMethodError.new(method)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
###################################################################################
|
49
|
+
# Format the ruby arrays into JS #
|
50
|
+
# Builds up a string representing a JS Array with JS escaped and formatted values #
|
51
|
+
###################################################################################
|
52
|
+
def js_format_data
|
53
|
+
formatted_rows = []
|
54
|
+
@data.each do |row|
|
55
|
+
values = []
|
56
|
+
row.each_with_index do |entry,index|
|
57
|
+
# Format/escape individual values for javascript, checking column types, and the ruby value as a failsafe
|
58
|
+
safe_val = if @column_types[index] == "date" || entry.is_a?(Date)
|
59
|
+
# Format a date object as a javascript date
|
60
|
+
entry.is_a?(String) ? entry : "new Date (#{entry.year},#{entry.month - 1},#{entry.day})"
|
61
|
+
elsif @column_types[index] == "datetime" || entry.is_a?(Time)
|
62
|
+
# Format a Time (datetime) as a javascript date object down to seconds
|
63
|
+
entry.is_a?(String) ? entry : "new Date (#{entry.year},#{entry.month - 1},#{entry.day},#{entry.hour},#{entry.min},#{entry.sec})"
|
64
|
+
else
|
65
|
+
# Non date/time values can be JS escaped/formatted safely with # to_json
|
66
|
+
entry.to_json
|
67
|
+
end
|
68
|
+
values << safe_val
|
69
|
+
end
|
70
|
+
rowstring = "[#{values.join(",")}]"
|
71
|
+
formatted_rows << rowstring
|
72
|
+
end
|
73
|
+
"[#{formatted_rows.join(',')}]"
|
74
|
+
end
|
75
|
+
|
76
|
+
end
|
@@ -0,0 +1,122 @@
|
|
1
|
+
# Provides calls for simplifying the loading and use of the Google Visualization API
|
2
|
+
#
|
3
|
+
# For use with rails, include this Module in ApplicationHelper
|
4
|
+
# See the Readme for usage details
|
5
|
+
#
|
6
|
+
# written by Jeremy Olliver
|
7
|
+
module GoogleVisualization
|
8
|
+
|
9
|
+
attr_accessor :google_visualizations, :visualization_packages
|
10
|
+
|
11
|
+
#######################################################################
|
12
|
+
# Place these method calls inside the <head> tag in your layout file. #
|
13
|
+
#######################################################################
|
14
|
+
|
15
|
+
# Include the Visualization API code from google.
|
16
|
+
# (Omit this call if you prefer to include the API in your own js package)
|
17
|
+
def include_visualization_api
|
18
|
+
%Q(<!--Load the AJAX API--><script type="text/javascript" src="http://www.google.com/jsapi"></script>)
|
19
|
+
end
|
20
|
+
|
21
|
+
# This code actually inserts the visualization data
|
22
|
+
def render_visualizations
|
23
|
+
if @google_visualizations
|
24
|
+
package_list = []
|
25
|
+
@visualization_packages.uniq.each do |p|
|
26
|
+
package_list << "\'#{p.to_s.camelize.downcase}\'"
|
27
|
+
end
|
28
|
+
output = %Q(
|
29
|
+
<script type="text/javascript">
|
30
|
+
google.load('visualization', '1', {'packages':[#{package_list.uniq.join(',')}]});
|
31
|
+
google.setOnLoadCallback(drawCharts);
|
32
|
+
var chartData = {};
|
33
|
+
var visualizationCharts = {};
|
34
|
+
function drawCharts() { )
|
35
|
+
@google_visualizations.each do |id, vis|
|
36
|
+
output += generate_visualization(id, vis[0], vis[1], vis[2])
|
37
|
+
end
|
38
|
+
output += "} </script>"
|
39
|
+
output + "<!-- Rendered Google Visualizations /-->"
|
40
|
+
else
|
41
|
+
"<!-- No graphs on this page /-->"
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
########################################################################
|
46
|
+
# Call this method from the view to insert the visualization data here #
|
47
|
+
# Will output a div with given id, and add the chart data to be #
|
48
|
+
# rendered from the head of the page #
|
49
|
+
########################################################################
|
50
|
+
def visualization(id, chart_type, options = {}, &block)
|
51
|
+
init
|
52
|
+
chart_type = chart_type.camelize # Camelize the chart type, as the Google API follows Camel Case conventions (e.g. ColumnChart, MotionChart)
|
53
|
+
options.stringify_keys! # Ensure consistent hash access
|
54
|
+
@visualization_packages << chart_type # Add the chart type to the packages needed to be loaded
|
55
|
+
|
56
|
+
# Initialize the data table (with hashed options), and pass it the block for cleaner adding of attributes within the block
|
57
|
+
table = DataTable.new(options.delete("data"), options.delete("columns"), options)
|
58
|
+
if block_given?
|
59
|
+
yield table
|
60
|
+
end
|
61
|
+
|
62
|
+
# Extract the html options
|
63
|
+
html_options = options.delete("html") || {}
|
64
|
+
# Add our chart to the list to be rendered in the head tag
|
65
|
+
@google_visualizations.merge!(id => [chart_type, table, options])
|
66
|
+
|
67
|
+
# Output a div with given id, that our graph will be embedded into
|
68
|
+
html = ""
|
69
|
+
html_options.each do |key, value|
|
70
|
+
html += %Q(#{key}="#{value}" )
|
71
|
+
end
|
72
|
+
concat %Q(<div id="#{id}" #{html}><!-- /--></div>), block.binding
|
73
|
+
nil
|
74
|
+
end
|
75
|
+
|
76
|
+
protected
|
77
|
+
|
78
|
+
# Initialize instance variables
|
79
|
+
def init
|
80
|
+
@google_visualizations ||= {}
|
81
|
+
@visualization_packages ||=[]
|
82
|
+
end
|
83
|
+
|
84
|
+
###################################################
|
85
|
+
# Internal methods for building the script data #
|
86
|
+
###################################################
|
87
|
+
def generate_visualization(id, chart, table, options={})
|
88
|
+
# Generate the js chart data
|
89
|
+
output = "chartData['#{id}'] = new google.visualization.DataTable();"
|
90
|
+
table.columns.each do |col|
|
91
|
+
output += "chartData['#{id}'].addColumn('#{table.column_types[col]}', '#{col}');"
|
92
|
+
end
|
93
|
+
option_str = make_opts_string(options)
|
94
|
+
|
95
|
+
output += %Q(
|
96
|
+
chartData['#{id}'].addRows(#{table.js_format_data});
|
97
|
+
visualizationCharts['#{id}'] = new google.visualization.#{chart.to_s.camelize}(document.getElementById('#{id}'));
|
98
|
+
visualizationCharts['#{id}'].draw(chartData['#{id}'], {#{option_str}});
|
99
|
+
)
|
100
|
+
end
|
101
|
+
|
102
|
+
# parse options into an array of key-value pairs
|
103
|
+
#
|
104
|
+
def make_opts_string(opts)
|
105
|
+
option_str = []
|
106
|
+
opts.each do |key, val|
|
107
|
+
str = "#{key}: "
|
108
|
+
if val.kind_of? Hash
|
109
|
+
str += "{" + make_opts_string(val) + "}"
|
110
|
+
elsif val.kind_of? Array
|
111
|
+
str += "[ " + val.collect { |v| "'#{v}'" }.join(", ") + " ]"
|
112
|
+
else
|
113
|
+
str += (val.kind_of?(String) ? "'#{val}'" : val.to_s)
|
114
|
+
end
|
115
|
+
|
116
|
+
option_str << str
|
117
|
+
end
|
118
|
+
|
119
|
+
return option_str.join(',')
|
120
|
+
end
|
121
|
+
|
122
|
+
end
|
data/lib/gvis.rb
ADDED
metadata
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: gvis
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 19
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
- 2
|
10
|
+
version: 1.0.2
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Jeremy Olliver
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-03-12 00:00:00 +13:00
|
19
|
+
default_executable:
|
20
|
+
dependencies: []
|
21
|
+
|
22
|
+
description: Easily embed charts with Google Visualization API, using ruby formatted options in your view files
|
23
|
+
email: jeremy.olliver@gmail.com
|
24
|
+
executables: []
|
25
|
+
|
26
|
+
extensions: []
|
27
|
+
|
28
|
+
extra_rdoc_files: []
|
29
|
+
|
30
|
+
files:
|
31
|
+
- README
|
32
|
+
- Rakefile
|
33
|
+
- MIT-LICENSE
|
34
|
+
- lib/gvis.rb
|
35
|
+
- lib/data_table.rb
|
36
|
+
- lib/google_visualization.rb
|
37
|
+
has_rdoc: true
|
38
|
+
homepage: http://github.com/jeremyolliver/gvis
|
39
|
+
licenses: []
|
40
|
+
|
41
|
+
post_install_message:
|
42
|
+
rdoc_options: []
|
43
|
+
|
44
|
+
require_paths:
|
45
|
+
- lib
|
46
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
47
|
+
none: false
|
48
|
+
requirements:
|
49
|
+
- - ">="
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
hash: 3
|
52
|
+
segments:
|
53
|
+
- 0
|
54
|
+
version: "0"
|
55
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
56
|
+
none: false
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
hash: 3
|
61
|
+
segments:
|
62
|
+
- 0
|
63
|
+
version: "0"
|
64
|
+
requirements: []
|
65
|
+
|
66
|
+
rubyforge_project:
|
67
|
+
rubygems_version: 1.6.1
|
68
|
+
signing_key:
|
69
|
+
specification_version: 3
|
70
|
+
summary: Easily embed charts with Google Visualization API
|
71
|
+
test_files: []
|
72
|
+
|