google_visualr 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +0 -0
- data/MIT-LICENSE +21 -0
- data/README.rdoc +92 -0
- data/Rakefile +33 -0
- data/VERSION +1 -0
- data/init.rb +17 -0
- data/install.rb +1 -0
- data/lib/annotated_time_line.rb +50 -0
- data/lib/area_chart.rb +54 -0
- data/lib/bar_chart.rb +53 -0
- data/lib/base_chart.rb +285 -0
- data/lib/column_chart.rb +53 -0
- data/lib/formatters.rb +184 -0
- data/lib/gauge.rb +36 -0
- data/lib/geo_map.rb +32 -0
- data/lib/image_spark_line.rb +36 -0
- data/lib/intensity_map.rb +29 -0
- data/lib/line_chart.rb +54 -0
- data/lib/map.rb +31 -0
- data/lib/motion_chart.rb +36 -0
- data/lib/org_chart.rb +29 -0
- data/lib/pie_chart.rb +44 -0
- data/lib/scatter_chart.rb +52 -0
- data/lib/table.rb +39 -0
- data/uninstall.rb +1 -0
- metadata +79 -0
data/CHANGELOG
ADDED
File without changes
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2010 Winston Teo Yong Wei
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
= Overview
|
2
|
+
|
3
|
+
This Ruby library, GoogleVisualr, is a wrapper around the {Google Visualization API}[http://code.google.com/apis/visualization/documentation/] and allows you to create the same visualizations with just pure Ruby;
|
4
|
+
you don't have to write any JavaScript at all.
|
5
|
+
|
6
|
+
Good for any Ruby on Rails setup whereby you prefer to work your logic in models or controllers.
|
7
|
+
|
8
|
+
Please refer to the {GoogleVisualr reference site}[http://googlevisualr.heroku.com/] for a complete list of visualizations that you can create with GoogleVisualr.
|
9
|
+
|
10
|
+
== The Gist
|
11
|
+
|
12
|
+
* In your model or controller, write Ruby code to create your visualization (e.g. Area Chart, Bar Chart, even Spark Lines etc).
|
13
|
+
* Configure your visualization with all the options as listed in Google Visualization API Docs. E.g. {Area Chart's configuration options}[http://code.google.com/apis/visualization/documentation/gallery/areachart.html#Configuration_Options].
|
14
|
+
* In your view, just call a <em>visualization.render(div_id)</em> method that will magically generate and insert JavaScript into the final HTML output.
|
15
|
+
* You get your visualization, and you didn't write any JavaScript!
|
16
|
+
|
17
|
+
== Limitations
|
18
|
+
|
19
|
+
Do note that GoogleVisualr is not a 100% complete wrapper for the Google Visualization API. These are not implemented for sake of simplicity:
|
20
|
+
* JavaScript Methods for use after a visualization has been rendered in a view. E.g. {Area Chart's methods}[http://code.google.com/apis/visualization/documentation/gallery/areachart.html#Methods].
|
21
|
+
* JavaScript Events for use after a visualization has been rendered in a view. E.g. {Area Chart's events}[http://code.google.com/apis/visualization/documentation/gallery/areachart.html#Events].
|
22
|
+
* Visualizations not created by Google, and a few image-only charts.
|
23
|
+
|
24
|
+
= Install
|
25
|
+
|
26
|
+
Just install the GitHub repository into your app/vendor/plugin folder.
|
27
|
+
|
28
|
+
> rails my_app; cd my_app;
|
29
|
+
> script/plugin install git://github.com/winston/google_visualr.git
|
30
|
+
|
31
|
+
= Basics
|
32
|
+
|
33
|
+
This section describes a basic implementation of the GoogleVisualr::AreaChart class.
|
34
|
+
|
35
|
+
Please review the {Docs}[http://googlevisualr.heroku.com/docs] for detailed documentation and advanced implementation of constructors and methods.
|
36
|
+
---
|
37
|
+
|
38
|
+
In your Rails layout, load Google Ajax API in the head tag, at the very top.
|
39
|
+
|
40
|
+
<script src='http://www.google.com/jsapi'></script>
|
41
|
+
|
42
|
+
In your Rails controller, initialize a visualization (area chart) with an empty constructor.
|
43
|
+
|
44
|
+
@chart = GoogleVisualr::AreaChart.new
|
45
|
+
|
46
|
+
Populate visualization with column headers, and row values.
|
47
|
+
|
48
|
+
# Add Column Headers
|
49
|
+
@chart.add_column('string', 'Year' )
|
50
|
+
@chart.add_column('number', 'Sales')
|
51
|
+
@chart.add_column('number', 'Expenses')
|
52
|
+
|
53
|
+
# Add Rows and Values
|
54
|
+
@chart.add_rows(4)
|
55
|
+
@chart.set_value(0, 0, '2004')
|
56
|
+
@chart.set_value(0, 1, 1000)
|
57
|
+
@chart.set_value(0, 2, 400)
|
58
|
+
@chart.set_value(1, 0, '2005')
|
59
|
+
@chart.set_value(1, 1, 1170)
|
60
|
+
@chart.set_value(1, 2, 460)
|
61
|
+
@chart.set_value(2, 0, '2006')
|
62
|
+
@chart.set_value(2, 1, 1500)
|
63
|
+
@chart.set_value(2, 2, 660)
|
64
|
+
@chart.set_value(3, 0, '2007')
|
65
|
+
@chart.set_value(3, 1, 1030)
|
66
|
+
@chart.set_value(3, 2, 540)
|
67
|
+
|
68
|
+
Configure visualization with options.
|
69
|
+
|
70
|
+
@chart.width = 400
|
71
|
+
@chart.height = 240
|
72
|
+
|
73
|
+
In your Rails view, render visualization.
|
74
|
+
|
75
|
+
<div id='chart'></div>
|
76
|
+
<%= @chart.render('chart') %>
|
77
|
+
|
78
|
+
= Support
|
79
|
+
|
80
|
+
Please submit all feedback, bugs and feature-requests to {GitHub Issues Tracker}[http://github.com/winston/google_visualr/issues].
|
81
|
+
|
82
|
+
Please also feel free to clone/fork the project, and improve it further!
|
83
|
+
|
84
|
+
= Author
|
85
|
+
|
86
|
+
GoogleVisualr is maintained by {Winston Teo}[mailto:winstonyw+googlevisualr@gmail.com], and his Ninja Penguin.
|
87
|
+
|
88
|
+
Who is Winston Teo? Find out more on {WinstonYW}[http://www.winstonyw.com], {LinkedIn}[http://sg.linkedin.com/in/winstonyw], or {follow Winston on Twitter}[http://www.twitter.com/winstonyw].
|
89
|
+
|
90
|
+
= License
|
91
|
+
|
92
|
+
Copyright © 2010 Winston Teo Yong Wei. Free software, released under the MIT license.
|
data/Rakefile
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
# Rake Hook
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
#Gem::manage_gems
|
5
|
+
require 'rake/gempackagetask'
|
6
|
+
|
7
|
+
PKG_FILES = FileList[
|
8
|
+
'[a-zA-Z]*',
|
9
|
+
'generators/**/*',
|
10
|
+
'lib/**/*',
|
11
|
+
'rails/**/*',
|
12
|
+
'tasks/**/*',
|
13
|
+
'test/**/*'
|
14
|
+
]
|
15
|
+
|
16
|
+
spec = Gem::Specification.new do |s|
|
17
|
+
s.name = "google_visualr"
|
18
|
+
s.version = "0.0.1"
|
19
|
+
s.author = "Winston Teo"
|
20
|
+
s.email = "winston.yongwei+spam at gmail.com"
|
21
|
+
s.homepage = "https://github.com/winston/google_visualr"
|
22
|
+
s.platform = Gem::Platform::RUBY
|
23
|
+
s.summary = "Wrapper around the Google Visualization API."
|
24
|
+
s.files = PKG_FILES.to_a
|
25
|
+
s.require_path = "lib"
|
26
|
+
s.has_rdoc = false
|
27
|
+
s.extra_rdoc_files = ["README.rdoc"]
|
28
|
+
end
|
29
|
+
|
30
|
+
desc 'Turn this plugin into a gem.'
|
31
|
+
Rake::GemPackageTask.new(spec) do |pkg|
|
32
|
+
pkg.gem_spec = spec
|
33
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.6
|
data/init.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/lib/base_chart'
|
2
|
+
require File.dirname(__FILE__) + '/lib/annotated_time_line'
|
3
|
+
require File.dirname(__FILE__) + '/lib/area_chart'
|
4
|
+
require File.dirname(__FILE__) + '/lib/bar_chart'
|
5
|
+
require File.dirname(__FILE__) + '/lib/column_chart'
|
6
|
+
require File.dirname(__FILE__) + '/lib/gauge'
|
7
|
+
require File.dirname(__FILE__) + '/lib/geo_map'
|
8
|
+
require File.dirname(__FILE__) + '/lib/image_spark_line'
|
9
|
+
require File.dirname(__FILE__) + '/lib/intensity_map'
|
10
|
+
require File.dirname(__FILE__) + '/lib/line_chart'
|
11
|
+
require File.dirname(__FILE__) + '/lib/map'
|
12
|
+
require File.dirname(__FILE__) + '/lib/motion_chart'
|
13
|
+
require File.dirname(__FILE__) + '/lib/org_chart'
|
14
|
+
require File.dirname(__FILE__) + '/lib/pie_chart'
|
15
|
+
require File.dirname(__FILE__) + '/lib/scatter_chart'
|
16
|
+
require File.dirname(__FILE__) + '/lib/table'
|
17
|
+
require File.dirname(__FILE__) + '/lib/formatters'
|
data/install.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
# Install Hook
|
@@ -0,0 +1,50 @@
|
|
1
|
+
module GoogleVisualr
|
2
|
+
|
3
|
+
# http://code.google.com/apis/visualization/documentation/gallery/annotatedtimeline.html
|
4
|
+
class AnnotatedTimeLine < BaseChart
|
5
|
+
|
6
|
+
attr_accessor :element_id
|
7
|
+
|
8
|
+
# http://code.google.com/apis/visualization/documentation/gallery/annotatedtimeline.html#Configuration_Options
|
9
|
+
attr_accessor :allowHtml
|
10
|
+
attr_accessor :allowRedraw
|
11
|
+
attr_accessor :allValuesSuffix
|
12
|
+
attr_accessor :annotationsWidth
|
13
|
+
attr_accessor :colors
|
14
|
+
attr_accessor :dateFormat
|
15
|
+
attr_accessor :displayAnnotations
|
16
|
+
attr_accessor :displayAnnotationsFilter
|
17
|
+
attr_accessor :displayDateBarSeparator
|
18
|
+
attr_accessor :displayExactValues
|
19
|
+
attr_accessor :displayLegendDots
|
20
|
+
attr_accessor :displayLegendValues
|
21
|
+
attr_accessor :displayRangeSelector
|
22
|
+
attr_accessor :displayZoomButtons
|
23
|
+
attr_accessor :fill
|
24
|
+
attr_accessor :highlightDot
|
25
|
+
attr_accessor :legendPosition
|
26
|
+
attr_accessor :max
|
27
|
+
attr_accessor :min
|
28
|
+
attr_accessor :numberFormats
|
29
|
+
attr_accessor :scaleColumns
|
30
|
+
attr_accessor :scaleType
|
31
|
+
attr_accessor :thickness
|
32
|
+
attr_accessor :wmode
|
33
|
+
attr_accessor :zoomEndTime
|
34
|
+
attr_accessor :zoomStartTime
|
35
|
+
|
36
|
+
def render (element_id)
|
37
|
+
|
38
|
+
options = Hash.new
|
39
|
+
|
40
|
+
options[:package] = self.class.to_s.split('::').last
|
41
|
+
options[:element_id] = element_id
|
42
|
+
options[:chart_style] = collect_parameters
|
43
|
+
|
44
|
+
super(options)
|
45
|
+
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
data/lib/area_chart.rb
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
module GoogleVisualr
|
2
|
+
|
3
|
+
# http://code.google.com/apis/visualization/documentation/gallery/areachart.html
|
4
|
+
class AreaChart < BaseChart
|
5
|
+
|
6
|
+
attr_accessor :element_id
|
7
|
+
|
8
|
+
# http://code.google.com/apis/visualization/documentation/gallery/areachart.html#Configuration_Options
|
9
|
+
attr_accessor :axisColor
|
10
|
+
attr_accessor :axisBackgroundColor
|
11
|
+
attr_accessor :axisFontSize
|
12
|
+
attr_accessor :backgroundColor
|
13
|
+
attr_accessor :borderColor
|
14
|
+
attr_accessor :colors
|
15
|
+
attr_accessor :enableTooltip
|
16
|
+
attr_accessor :focusBorderColor
|
17
|
+
attr_accessor :height
|
18
|
+
attr_accessor :isStacked
|
19
|
+
attr_accessor :legend
|
20
|
+
attr_accessor :legendBackgroundColor
|
21
|
+
attr_accessor :legendFontSize
|
22
|
+
attr_accessor :legendTextColor
|
23
|
+
attr_accessor :lineSize
|
24
|
+
attr_accessor :logScale
|
25
|
+
attr_accessor :max
|
26
|
+
attr_accessor :min
|
27
|
+
attr_accessor :pointSize
|
28
|
+
attr_accessor :reverseAxis
|
29
|
+
attr_accessor :showCategories
|
30
|
+
attr_accessor :title
|
31
|
+
attr_accessor :titleX
|
32
|
+
attr_accessor :titleY
|
33
|
+
attr_accessor :titleColor
|
34
|
+
attr_accessor :titleFontSize
|
35
|
+
attr_accessor :tooltipFontSize
|
36
|
+
attr_accessor :tooltipHeight
|
37
|
+
attr_accessor :tooltipWidth
|
38
|
+
attr_accessor :width
|
39
|
+
|
40
|
+
def render (element_id)
|
41
|
+
|
42
|
+
options = Hash.new
|
43
|
+
|
44
|
+
options[:package] = self.class.to_s.split('::').last
|
45
|
+
options[:element_id] = element_id
|
46
|
+
options[:chart_style] = collect_parameters
|
47
|
+
|
48
|
+
super(options)
|
49
|
+
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
data/lib/bar_chart.rb
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
module GoogleVisualr
|
2
|
+
|
3
|
+
# http://code.google.com/apis/visualization/documentation/gallery/barchart.html
|
4
|
+
class BarChart < BaseChart
|
5
|
+
|
6
|
+
attr_accessor :element_id
|
7
|
+
|
8
|
+
# http://code.google.com/apis/visualization/documentation/gallery/barchart.html#Configuration_Options
|
9
|
+
attr_accessor :axisColor
|
10
|
+
attr_accessor :axisBackgroundColor
|
11
|
+
attr_accessor :axisFontSize
|
12
|
+
attr_accessor :backgroundColor
|
13
|
+
attr_accessor :borderColor
|
14
|
+
attr_accessor :colors
|
15
|
+
attr_accessor :enableTooltip
|
16
|
+
attr_accessor :focusBorderColor
|
17
|
+
attr_accessor :height
|
18
|
+
attr_accessor :is3D
|
19
|
+
attr_accessor :isStacked
|
20
|
+
attr_accessor :legend
|
21
|
+
attr_accessor :legendBackgroundColor
|
22
|
+
attr_accessor :legendFontSize
|
23
|
+
attr_accessor :legendTextColor
|
24
|
+
attr_accessor :logScale
|
25
|
+
attr_accessor :max
|
26
|
+
attr_accessor :min
|
27
|
+
attr_accessor :reverseAxis
|
28
|
+
attr_accessor :showCategories
|
29
|
+
attr_accessor :title
|
30
|
+
attr_accessor :titleX
|
31
|
+
attr_accessor :titleY
|
32
|
+
attr_accessor :titleColor
|
33
|
+
attr_accessor :titleFontSize
|
34
|
+
attr_accessor :tooltipFontSize
|
35
|
+
attr_accessor :tooltipHeight
|
36
|
+
attr_accessor :tooltipWidth
|
37
|
+
attr_accessor :width
|
38
|
+
|
39
|
+
def render (element_id)
|
40
|
+
|
41
|
+
options = Hash.new
|
42
|
+
|
43
|
+
options[:package] = self.class.to_s.split('::').last
|
44
|
+
options[:element_id] = element_id
|
45
|
+
options[:chart_style] = collect_parameters
|
46
|
+
|
47
|
+
super(options)
|
48
|
+
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
data/lib/base_chart.rb
ADDED
@@ -0,0 +1,285 @@
|
|
1
|
+
module GoogleVisualr
|
2
|
+
|
3
|
+
class BaseChart
|
4
|
+
|
5
|
+
attr_accessor :chart_data
|
6
|
+
attr_accessor :formatters
|
7
|
+
|
8
|
+
##############################
|
9
|
+
# Constructors
|
10
|
+
##############################
|
11
|
+
#
|
12
|
+
# GoogleVisualr::visualization.new:
|
13
|
+
# Creates an empty visualization instance. Use add_columns, add_rows and set_value or set_cell methods to populate the visualization.
|
14
|
+
#
|
15
|
+
# GoogleVisualr::visualization.new(data object):
|
16
|
+
# reates a visualization by passing a JavaScript-string-literal like data object into the data parameter. This object can contain formatting options.
|
17
|
+
#
|
18
|
+
##############################
|
19
|
+
# Syntax Description of Data Object
|
20
|
+
##############################
|
21
|
+
#
|
22
|
+
# The data object consists of two required top-level properties, cols and rows.
|
23
|
+
#
|
24
|
+
# * cols property
|
25
|
+
#
|
26
|
+
# cols is an array of objects describing the ID and type of each column. Each property is an object with the following properties (case-sensitive):
|
27
|
+
#
|
28
|
+
# * type [Required] The data type of the data in the column. Supports the following string values:
|
29
|
+
# - 'string' : String value. Example values: v:'foo', :v:'bar'
|
30
|
+
# - 'number' : Number value. Example values: v:7, v:3.14, v:-55
|
31
|
+
# - 'boolean' : Boolean value ('true' or 'false'). Example values: v:true, v:false
|
32
|
+
# - 'date' : Date object, with the time truncated. Example value: v:Date.parse('2010-01-01')
|
33
|
+
# - 'datetime' : DateTime/Time object, time inclusive. Example value: v:DateTime.parse('2010-01-01 14:20:25')
|
34
|
+
# - 'timeofday' : Array of 3 numbers or 4 numbers, [Hour,Minute,Second,(Optional) Milliseconds]. Example value: v:[8, 15, 0]
|
35
|
+
# * label [Optional] A string value that some visualizations display for this column. Example: label:'Height'
|
36
|
+
# * id [Optional] A unique (basic alphanumeric) string ID of the column. Be careful not to choose a JavaScript keyword. Example: id:'col_1'
|
37
|
+
#
|
38
|
+
# * rows property
|
39
|
+
#
|
40
|
+
# The rows property holds an array of row objects. Each row object has one required property called c, which is an array of cells in that row.
|
41
|
+
#
|
42
|
+
# Each cell in the table is described by an object with the following properties:
|
43
|
+
#
|
44
|
+
# * v [Optional] The cell value. The data type should match the column data type.
|
45
|
+
# * f [Optional] A string version of the v value, formatted strictly for display only. If omitted, a string version of v will be used.
|
46
|
+
#
|
47
|
+
# Cells in the row array should be in the same order as their column descriptions in cols.
|
48
|
+
#
|
49
|
+
# To indicate a null cell, you can either specify null, or set empty string for a cell in an array, or omit trailing array members.
|
50
|
+
# So, to indicate a row with null for the first two cells, you could specify [ '', '', {cell_val}] or [null, null, {cell_val}].
|
51
|
+
def initialize(options={})
|
52
|
+
|
53
|
+
@chart_data = "var chart_data = new google.visualization.DataTable();"
|
54
|
+
|
55
|
+
if !options.empty?
|
56
|
+
|
57
|
+
cols = options[:cols]
|
58
|
+
add_columns(cols)
|
59
|
+
|
60
|
+
rows = options[:rows]
|
61
|
+
rows.each do |row|
|
62
|
+
add_row(row[:c])
|
63
|
+
end
|
64
|
+
|
65
|
+
end
|
66
|
+
|
67
|
+
end
|
68
|
+
|
69
|
+
# Adds a new column to the visualization.
|
70
|
+
#
|
71
|
+
# Parameters:
|
72
|
+
# * type [Required] The data type of the data in the column. Supports the following string values:
|
73
|
+
# - 'string' : String value. Example values: v:'hello'
|
74
|
+
# - 'number' : Number value. Example values: v:7 , v:3.14, v:-55
|
75
|
+
# - 'date' : Date object, with the time truncated. Example values: v:Date.parse('2010-01-01')
|
76
|
+
# - 'datetime' : Date object including the time. Example values: v:Date.parse('2010-01-01 14:20:25')
|
77
|
+
# - 'boolean' : Boolean value ('true' or 'false'). Example values: v: true
|
78
|
+
# * label [Optional] A string value that some visualizations display for this column. Example: label:'Height'
|
79
|
+
# * id [Optional] A unique (basic alphanumeric) string ID of the column. Be careful not to choose a JavaScript keyword. Example: id:'col_1'
|
80
|
+
def add_column (type, label="", id="")
|
81
|
+
@chart_data << "chart_data.addColumn('#{type}', '#{label}', '#{id}');"
|
82
|
+
end
|
83
|
+
|
84
|
+
# Adds multiple columns to the visualization.
|
85
|
+
#
|
86
|
+
# Parameters:
|
87
|
+
# * columns [Required] An array of column objects {:type, :label, :id}. Calls add_column for each column object.
|
88
|
+
def add_columns(columns)
|
89
|
+
|
90
|
+
columns.each do |column|
|
91
|
+
add_column(column[:type], column[:label], column[:id])
|
92
|
+
end
|
93
|
+
|
94
|
+
end
|
95
|
+
|
96
|
+
# Adds a new row to the visualization.
|
97
|
+
# Call method without any parameters to add an empty row, otherwise, call method with a row object.
|
98
|
+
#
|
99
|
+
# Parameters:
|
100
|
+
# * row [Optional] An array of cell values specifying the data for the new row.
|
101
|
+
# - You can specify a value for a cell (e.g. 'hi') or specify a formatted value using cell objects (e.g. {v:55, f:'Fifty-five'}) as described in the constructor section.
|
102
|
+
# - You can mix simple values and cell objects in the same method call.
|
103
|
+
# - To create an empty cell, use nil or empty string.
|
104
|
+
def add_row(row)
|
105
|
+
|
106
|
+
if row.empty?
|
107
|
+
@chart_data << "chart_data.addRow();" # Empty Row
|
108
|
+
else
|
109
|
+
|
110
|
+
attributes = Array.new
|
111
|
+
row.each do |cell|
|
112
|
+
attributes << add_row_cell(cell)
|
113
|
+
end
|
114
|
+
|
115
|
+
@chart_data << "chart_data.addRow( [" + attributes.join(",") + "] );"
|
116
|
+
|
117
|
+
end
|
118
|
+
|
119
|
+
end
|
120
|
+
|
121
|
+
# Adds multiple rows to the visualization. You can call this method with data to populate a set of new rows or create new empty rows.
|
122
|
+
#
|
123
|
+
# Parameters:
|
124
|
+
# * array_or_num [Required] Either an array or a number.
|
125
|
+
# - Array: An array of row objects used to populate a set of new rows. Each row is an object as described in add_row().
|
126
|
+
# - Number: A number specifying the number of new empty rows to create.
|
127
|
+
def add_rows(array_or_num)
|
128
|
+
|
129
|
+
if array_or_num.is_a?(Array)
|
130
|
+
array_or_num.each do |row|
|
131
|
+
add_row(row)
|
132
|
+
end
|
133
|
+
else
|
134
|
+
@chart_data << "chart_data.addRows(#{array_or_num});"
|
135
|
+
end
|
136
|
+
|
137
|
+
end
|
138
|
+
|
139
|
+
# Sets the value and/or formatted value of a cell.
|
140
|
+
#
|
141
|
+
# Parameters:
|
142
|
+
# * row_index [Required] A number greater than or equal to zero, but smaller than the total number of rows.
|
143
|
+
# * column_index [Required] A number greater than or equal to zero, but smaller than the total number of columns.
|
144
|
+
# * value [Required] The cell value. The data type should match the column data type.
|
145
|
+
# * formatted_value [Optional] A string version of value, formatted strictly for display only. If omitted, a string version of value will be used.
|
146
|
+
def set_cell (row_index, column_index, value, formatted_value=nil, properties=nil)
|
147
|
+
|
148
|
+
@chart_data << "chart_data.setCell("
|
149
|
+
@chart_data << "#{row_index}, #{column_index}, #{typecast(value)}"
|
150
|
+
@chart_data << ", '#{formatted_value}'" unless formatted_value.blank?
|
151
|
+
@chart_data << ", '#{properties}'" unless properties.blank?
|
152
|
+
@chart_data << ");"
|
153
|
+
|
154
|
+
end
|
155
|
+
|
156
|
+
# Sets the value of a cell. Overwrites any existing cell value, and clear out any formatted value for the cell.
|
157
|
+
#
|
158
|
+
# Parameters:
|
159
|
+
# * row_index [Required] A number greater than or equal to zero, but smaller than the total number of rows.
|
160
|
+
# * column_index [Required] A number greater than or equal to zero, but smaller than the total number of columns.
|
161
|
+
# * value [Required] The cell value. The data type should match the column data type.
|
162
|
+
def set_value (row_index, column_index, value)
|
163
|
+
|
164
|
+
@chart_data << "chart_data.setCell(#{row_index}, #{column_index}, #{typecast(value)});"
|
165
|
+
|
166
|
+
end
|
167
|
+
|
168
|
+
# Applies one or more formatters to the visualization to format the columns as specified by the formatter/s.
|
169
|
+
#
|
170
|
+
# Parameters:
|
171
|
+
# * formatter/s [Required] One, or an array of formatters.
|
172
|
+
def format(*formatters)
|
173
|
+
|
174
|
+
@formatters ||= Array.new
|
175
|
+
@formatters += formatters
|
176
|
+
|
177
|
+
end
|
178
|
+
|
179
|
+
# Sets chart configuration options with a hash.
|
180
|
+
#
|
181
|
+
# Parameters:
|
182
|
+
# *options [Required] A hash of configuration options.
|
183
|
+
def set_options(options)
|
184
|
+
|
185
|
+
options.each_pair do | key, value |
|
186
|
+
send "#{key}=", value
|
187
|
+
end
|
188
|
+
|
189
|
+
end
|
190
|
+
|
191
|
+
# Generates JavaScript and renders the visualization in the final HTML output.
|
192
|
+
#
|
193
|
+
# Parameters:
|
194
|
+
# *div_id [Required] The ID of the DIV element that the visualization should be rendered in.
|
195
|
+
#
|
196
|
+
# Note: This is the super method.
|
197
|
+
def render(options)
|
198
|
+
|
199
|
+
script = "\n<script type='text/javascript'>"
|
200
|
+
script << "\n google.load('visualization','1', {packages: ['#{options[:package].downcase}'], callback: function() {"
|
201
|
+
script << "\n #{@chart_data}"
|
202
|
+
if @formatters
|
203
|
+
@formatters.each do |formatter|
|
204
|
+
script << formatter.script
|
205
|
+
end
|
206
|
+
end
|
207
|
+
script << "\n var chart = new google.visualization.#{options[:package]}(document.getElementById('#{options[:element_id]}'));"
|
208
|
+
script << "\n chart.draw(chart_data, #{options[:chart_style]});"
|
209
|
+
script << "\n }});"
|
210
|
+
script << "\n</script>"
|
211
|
+
|
212
|
+
return script
|
213
|
+
|
214
|
+
end
|
215
|
+
|
216
|
+
private
|
217
|
+
|
218
|
+
def add_row_cell(cell)
|
219
|
+
|
220
|
+
if cell.is_a?(Hash)
|
221
|
+
|
222
|
+
attributes = Array.new
|
223
|
+
cell.each_pair do |key, value|
|
224
|
+
attributes << "#{key}: #{typecast(value)}"
|
225
|
+
end
|
226
|
+
|
227
|
+
return "{" + attributes.join(",") + "}"
|
228
|
+
|
229
|
+
else
|
230
|
+
return "#{typecast(cell)}"
|
231
|
+
end
|
232
|
+
|
233
|
+
end
|
234
|
+
|
235
|
+
# If the column type is 'string' , the value should be a string.
|
236
|
+
# If the column type is 'number' , the value should be a number.
|
237
|
+
# If the column type is 'boolean' , the value should be a boolean.
|
238
|
+
# If the column type is 'date' , the value should be a Date object.
|
239
|
+
# If the column type is 'datetime' , the value should be a DateTime or Time object.
|
240
|
+
# If the column type is 'timeofday' , the value should be an array of three or four numbers: [hour, minute, second, optional milliseconds].
|
241
|
+
def typecast(value)
|
242
|
+
|
243
|
+
case
|
244
|
+
when value.is_a?(String)
|
245
|
+
return "'#{escape_single_quotes(value)}'"
|
246
|
+
when value.is_a?(Integer) || value.is_a?(Float)
|
247
|
+
return value
|
248
|
+
when value.is_a?(TrueClass) || value.is_a?(FalseClass)
|
249
|
+
return "#{value}"
|
250
|
+
when value.is_a?(Date)
|
251
|
+
return "new Date(#{value.year}, #{value.month-1}, #{value.day})"
|
252
|
+
when value.is_a?(DateTime) || value.is_a?(Time)
|
253
|
+
return "new Date(#{value.year}, #{value.month-1}, #{value.day}, #{value.hour}, #{value.min}, #{value.sec})"
|
254
|
+
when value.is_a?(Array)
|
255
|
+
return "[" + value.collect { |item| typecast(item) }.join(",") + "]"
|
256
|
+
else
|
257
|
+
return value
|
258
|
+
end
|
259
|
+
|
260
|
+
end
|
261
|
+
|
262
|
+
def collect_parameters
|
263
|
+
|
264
|
+
attributes = Array.new
|
265
|
+
instance_variable_names.each do |instance_variable|
|
266
|
+
next if instance_variable == "@chart_data" || instance_variable == "@formatters"
|
267
|
+
key = instance_variable.gsub("@", "")
|
268
|
+
value = instance_variable_get(instance_variable)
|
269
|
+
attribute = "#{key}:#{typecast(value)}"
|
270
|
+
attributes << attribute
|
271
|
+
end
|
272
|
+
|
273
|
+
return "{" + attributes.join(",") + "}"
|
274
|
+
|
275
|
+
end
|
276
|
+
|
277
|
+
def escape_single_quotes(str)
|
278
|
+
|
279
|
+
str.gsub(/[']/, '\\\\\'')
|
280
|
+
|
281
|
+
end
|
282
|
+
|
283
|
+
end
|
284
|
+
|
285
|
+
end
|