google_visualr 2.1.0 → 2.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile.lock +1 -1
- data/README.rdoc +17 -17
- data/lib/google_visualr/data_table.rb +14 -8
- data/lib/google_visualr/version.rb +1 -1
- data/spec/google_visualr/data_table_spec.rb +84 -76
- metadata +3 -3
data/Gemfile.lock
CHANGED
data/README.rdoc
CHANGED
@@ -8,6 +8,21 @@ Good for any Ruby on Rails setup whereby you prefer to work your logic in models
|
|
8
8
|
|
9
9
|
Please refer to the {GoogleVisualr API Reference site}[http://googlevisualr.heroku.com/] for a complete list of Google charts that you can create with GoogleVisualr.
|
10
10
|
|
11
|
+
== The Gist
|
12
|
+
|
13
|
+
* In your model or controller, write Ruby code to create your chart (e.g. Area Chart, Bar Chart, even Spark Lines etc).
|
14
|
+
* Configure your chart with any of the options as listed in Google Chart Tools' API Docs. E.g. {Area Chart's Configuration Options}[http://code.google.com/apis/chart/interactive/docs/gallery/areachart.html#Configuration_Options].
|
15
|
+
* In your view, invoke a <em>chart.to_js(div_id)</em> method and that will magically generate and insert JavaScript into the final HTML output.
|
16
|
+
* You get your awesome Google chart, and you didn't write any JavaScript!
|
17
|
+
|
18
|
+
== Limitations
|
19
|
+
|
20
|
+
GoogleVisualr is created solely for the aim of simplifying the display of a chart, and not the interactions.
|
21
|
+
|
22
|
+
Hence, do note that GoogleVisualr is not a 100% complete wrapper for Google Chart Tools.
|
23
|
+
|
24
|
+
For example, Methods and Events as described in Google Chart Tools' API Docs, for use after a chart has been rendered, are not implemented because they felt more native being written as JavaScript functions, within views or .js files.
|
25
|
+
|
11
26
|
== Differences Between Gem and Plugin
|
12
27
|
|
13
28
|
Gem
|
@@ -24,21 +39,6 @@ This gem, however, is not a drop-in replacement for the plugin, as there are num
|
|
24
39
|
|
25
40
|
Notably, you will have to interact mostly with GoogleVisualr::DataTable which was absent in the plugin.
|
26
41
|
|
27
|
-
== The Gist
|
28
|
-
|
29
|
-
* In your model or controller, write Ruby code to create your chart (e.g. Area Chart, Bar Chart, even Spark Lines etc).
|
30
|
-
* Configure your chart with any of the options as listed in Google Chart Tools' API Docs. E.g. {Area Chart's Configuration Options}[http://code.google.com/apis/chart/interactive/docs/gallery/areachart.html#Configuration_Options].
|
31
|
-
* In your view, invoke a <em>chart.to_js(div_id)</em> method and that will magically generate and insert JavaScript into the final HTML output.
|
32
|
-
* You get your awesome Google chart, and you didn't write any JavaScript!
|
33
|
-
|
34
|
-
== Limitations
|
35
|
-
|
36
|
-
GoogleVisualr is created solely for the aim of simplifying the display of a chart, and not the interactions.
|
37
|
-
|
38
|
-
Hence, do note that GoogleVisualr is not a 100% complete wrapper for Google Chart Tools.
|
39
|
-
|
40
|
-
For example, Methods and Events as described in Google Chart Tools' API Docs, for use after a chart has been rendered, are not implemented because they felt more native being written as JavaScript functions, within views or .js files.
|
41
|
-
|
42
42
|
= Install
|
43
43
|
|
44
44
|
Assuming you are on Rails 3, include the gem in your Gemfile.
|
@@ -94,8 +94,8 @@ Please submit all feedback, bugs and feature-requests to {GitHub Issues Tracker}
|
|
94
94
|
|
95
95
|
= Change Log
|
96
96
|
|
97
|
-
|
98
|
-
* Added
|
97
|
+
<em>Version 2.1.0</em>
|
98
|
+
* Added +#render_chart+ as a helper method in Rails views.
|
99
99
|
|
100
100
|
= Author
|
101
101
|
|
@@ -65,6 +65,7 @@ module GoogleVisualr
|
|
65
65
|
end
|
66
66
|
|
67
67
|
# Adds a new column to the data_table.
|
68
|
+
# Experimental support for role (and pattern): http://code.google.com/apis/chart/interactive/docs/roles.html.
|
68
69
|
#
|
69
70
|
# Parameters:
|
70
71
|
# * type [Required] The data type of the data in the column. Supports the following string values:
|
@@ -75,17 +76,20 @@ module GoogleVisualr
|
|
75
76
|
# - 'boolean' : Boolean value ('true' or 'false'). Example values: v: true
|
76
77
|
# * label [Optional] A string value that some visualizations display for this column. Example: label:'Height'
|
77
78
|
# * id [Optional] A unique (basic alphanumeric) string ID of the column. Be careful not to choose a JavaScript keyword. Example: id:'col_1'
|
78
|
-
|
79
|
-
|
79
|
+
# * role [Optional] A string value that describes the purpose of the data in that column. Example, a column might hold data describing tooltip text, data point annotations, or uncertainty indicators.
|
80
|
+
# * pattern [Optional] A number (or date) format string specifying how to display the column value; used in conjunction with role.
|
81
|
+
def new_column(type, label=nil, id =nil, role=nil, pattern=nil)
|
82
|
+
column = { :type => type, :label => label, :id => id, :role => role, :pattern => pattern }.reject { |key, value| value.nil? }
|
83
|
+
@cols << column
|
80
84
|
end
|
81
85
|
|
82
86
|
# Adds multiple columns to the data_table.
|
83
87
|
#
|
84
88
|
# Parameters:
|
85
|
-
# * columns [Required] An array of column objects {:type, :label, :id}. Calls new_column for each column object.
|
89
|
+
# * columns [Required] An array of column objects {:type, :label, :id, :role, :pattern}. Calls new_column for each column object.
|
86
90
|
def new_columns(columns)
|
87
91
|
columns.each do |column|
|
88
|
-
new_column(column[:type], column[:label], column[:id])
|
92
|
+
new_column(column[:type], column[:label], column[:id], column[:role], column[:pattern])
|
89
93
|
end
|
90
94
|
end
|
91
95
|
|
@@ -200,10 +204,12 @@ module GoogleVisualr
|
|
200
204
|
js = "var data_table = new google.visualization.DataTable();"
|
201
205
|
|
202
206
|
@cols.each do |column|
|
203
|
-
js << "data_table.addColumn(
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
+
js << "data_table.addColumn("
|
208
|
+
if column[:role].nil?
|
209
|
+
js << column.map{ |key, value| "'#{value}'" }.join(", ")
|
210
|
+
else
|
211
|
+
js << "{" + column.map{ |key, value| "#{key}: '#{value}'" }.join(", ") + "}"
|
212
|
+
end
|
207
213
|
js << ");"
|
208
214
|
end
|
209
215
|
|
@@ -2,8 +2,9 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe GoogleVisualr::DataTable do
|
4
4
|
|
5
|
-
|
5
|
+
let(:dt) { GoogleVisualr::DataTable.new }
|
6
6
|
|
7
|
+
def valid_object
|
7
8
|
@cols = [
|
8
9
|
{ :id => 'A', :label => 'NEW A' , :type => 'string' },
|
9
10
|
{ :id => 'B', :label => 'B-label', :type => 'number' },
|
@@ -15,12 +16,10 @@ describe GoogleVisualr::DataTable do
|
|
15
16
|
{ :c => [ {:v => 'c'}, {:v => 3.0, :f => 'Three'}, {:v => Date.parse('2008-04-30 00:31:26'), :f => '4/30/08 12:31 AM'} ] }
|
16
17
|
]
|
17
18
|
GoogleVisualr::DataTable.new({:cols => @cols, :rows => @rows})
|
18
|
-
|
19
19
|
end
|
20
20
|
|
21
21
|
describe "#new" do
|
22
22
|
it "initializes without params" do
|
23
|
-
dt = GoogleVisualr::DataTable.new
|
24
23
|
dt.should_not be_nil
|
25
24
|
dt.cols.should be_a_kind_of Array
|
26
25
|
dt.rows.should be_a_kind_of Array
|
@@ -46,142 +45,144 @@ describe GoogleVisualr::DataTable do
|
|
46
45
|
|
47
46
|
describe "#new_column" do
|
48
47
|
it "initializes a new column with only type param" do
|
49
|
-
dt = GoogleVisualr::DataTable.new
|
50
48
|
dt.new_column('string')
|
51
|
-
dt.cols.first.should == {:
|
49
|
+
dt.cols.first.should == {:type => 'string'}
|
52
50
|
end
|
53
51
|
|
54
52
|
it "initializes a new column with all params" do
|
55
|
-
dt = GoogleVisualr::DataTable.new
|
56
53
|
dt.new_column('string', 'A LABEL', 'col_0')
|
57
54
|
dt.cols.first.should == {:id => 'col_0', :label => 'A LABEL', :type => 'string'}
|
58
55
|
end
|
56
|
+
|
57
|
+
it "initializes a new column with experimental role param" do
|
58
|
+
dt.new_column('string', nil, nil, 'interval', 'pattern')
|
59
|
+
dt.cols.first.should == {:type => 'string', :role => 'interval', :pattern => 'pattern'}
|
60
|
+
end
|
59
61
|
end
|
60
62
|
|
61
63
|
describe "new_columns" do
|
62
|
-
it "initializes new columns" do
|
63
|
-
columns = [
|
64
|
+
it "initializes new columns (with experimental support)" do
|
65
|
+
columns = [
|
66
|
+
{:id => 'A', :label => 'NEW A', :type => 'string'},
|
67
|
+
{:id => 'B', :label => 'NEW B', :type => 'string'},
|
68
|
+
{:type => 'string', :role => 'interval', :pattern => 'pattern'}
|
69
|
+
]
|
64
70
|
|
65
|
-
dt = GoogleVisualr::DataTable.new
|
66
71
|
dt.new_columns(columns)
|
67
|
-
dt.cols.
|
68
|
-
dt.cols.
|
72
|
+
dt.cols[0].should == columns[0]
|
73
|
+
dt.cols[1].should == columns[1]
|
74
|
+
dt.cols[2].should == columns[2]
|
69
75
|
end
|
70
76
|
end
|
71
77
|
|
72
78
|
context "column values" do
|
73
79
|
before do
|
74
|
-
|
75
|
-
|
76
|
-
@dt.set_column(0, [1,2,3])
|
80
|
+
dt.new_column({:type => 'number'})
|
81
|
+
dt.set_column(0, [1,2,3])
|
77
82
|
end
|
78
83
|
|
79
84
|
describe "#set_column" do
|
80
85
|
it "sets a column of values to column #index" do
|
81
|
-
|
82
|
-
|
83
|
-
|
86
|
+
dt.rows[0][0].v.should == 1
|
87
|
+
dt.rows[1][0].v.should == 2
|
88
|
+
dt.rows[2][0].v.should == 3
|
84
89
|
end
|
85
90
|
end
|
86
91
|
|
87
92
|
describe "#get_column" do
|
88
93
|
it "retrieves values in column #index" do
|
89
|
-
|
94
|
+
dt.get_column(0).should == [1,2,3]
|
90
95
|
end
|
91
96
|
end
|
92
97
|
end
|
93
98
|
|
94
99
|
context "row values" do
|
95
100
|
before do
|
96
|
-
|
97
|
-
|
98
|
-
@dt.rows.should be_empty
|
101
|
+
dt.new_columns( [ {:type => 'number'}, {:type => 'string'} ] )
|
102
|
+
dt.rows.should be_empty
|
99
103
|
end
|
100
104
|
|
101
105
|
describe "#add_row" do
|
102
106
|
context "when param is empty" do
|
103
107
|
it "adds an empty row to the data_table" do
|
104
|
-
|
105
|
-
|
106
|
-
|
108
|
+
dt.add_row
|
109
|
+
dt.rows.size.should == 1
|
110
|
+
dt.rows[0].should be_empty
|
107
111
|
end
|
108
112
|
end
|
109
113
|
|
110
114
|
context "when param is not empty" do
|
111
115
|
it "adds the row values to the data_table" do
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
+
dt.add_row([1, 'A'])
|
117
|
+
dt.rows.size.should == 1
|
118
|
+
dt.rows[0][0].v.should == 1
|
119
|
+
dt.rows[0][1].v.should == 'A'
|
116
120
|
end
|
117
121
|
end
|
118
122
|
end
|
119
123
|
|
120
|
-
|
121
124
|
describe "#add_rows" do
|
122
125
|
context "when param is number" do
|
123
126
|
it "adds x number of empty rows to the data_table" do
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
127
|
+
dt.add_rows(2)
|
128
|
+
dt.rows.size.should == 2
|
129
|
+
dt.rows[0].should be_empty
|
130
|
+
dt.rows[1].should be_empty
|
128
131
|
end
|
129
132
|
end
|
130
133
|
|
131
134
|
context "when param is an array" do
|
132
135
|
it "adds the rows to the data_table" do
|
133
|
-
|
134
|
-
|
136
|
+
dt.add_rows( [ [1, 'A'], [2, 'B'] ] )
|
137
|
+
dt.rows.size.should == 2
|
135
138
|
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
139
|
+
dt.rows[0][0].v.should == 1
|
140
|
+
dt.rows[0][1].v.should == 'A'
|
141
|
+
dt.rows[1][0].v.should == 2
|
142
|
+
dt.rows[1][1].v.should == 'B'
|
140
143
|
end
|
141
144
|
end
|
142
145
|
end
|
143
146
|
|
144
147
|
describe "@get_row" do
|
145
148
|
it "retrieves values in row #index" do
|
146
|
-
|
147
|
-
|
149
|
+
dt.add_rows( [ [1, 'A'], [2, 'B'] ] )
|
150
|
+
dt.rows.size.should == 2
|
148
151
|
|
149
|
-
|
150
|
-
|
152
|
+
dt.get_row(0).should == [1, 'A']
|
153
|
+
dt.get_row(1).should == [2, 'B']
|
151
154
|
end
|
152
155
|
end
|
153
156
|
end
|
154
157
|
|
155
158
|
context "cell value" do
|
156
159
|
before do
|
157
|
-
|
158
|
-
|
159
|
-
@dt.add_row
|
160
|
+
dt.new_columns( [ {:type => 'string'}, {:type => 'number'}, {:type => 'boolean'}, {:type => 'datetime'}, {:type => 'date'} ] )
|
161
|
+
dt.add_row
|
160
162
|
end
|
161
163
|
|
162
|
-
|
163
164
|
describe "#set_cell" do
|
164
165
|
it "sets cell" do
|
165
|
-
|
166
|
-
|
166
|
+
dt.set_cell(0, 0, {:v => 'ABCD'})
|
167
|
+
dt.set_cell(0, 1, 1000)
|
167
168
|
|
168
|
-
|
169
|
+
dt.get_row(0).should == ['ABCD', 1000]
|
169
170
|
end
|
170
171
|
|
171
172
|
it "raises an exception if the row_index or column_index specified is out of range" do
|
172
173
|
expect {
|
173
|
-
|
174
|
+
dt.set_cell(5, 0, 1000)
|
174
175
|
}.to raise_exception(RangeError)
|
175
176
|
|
176
177
|
expect {
|
177
|
-
|
178
|
+
dt.set_cell(0, 5, 1000)
|
178
179
|
}.to raise_exception(RangeError)
|
179
180
|
end
|
180
181
|
|
181
182
|
describe "#verify_against_column_type" do
|
182
183
|
def assert_raises_exception(col, value)
|
183
184
|
expect {
|
184
|
-
|
185
|
+
dt.set_cell(0, col, value)
|
185
186
|
}.to raise_exception(ArgumentError)
|
186
187
|
end
|
187
188
|
|
@@ -208,24 +209,24 @@ describe GoogleVisualr::DataTable do
|
|
208
209
|
|
209
210
|
it "accepts 'nil' for all column types" do
|
210
211
|
expect {
|
211
|
-
|
212
|
+
dt.set_cell(0, 0, nil)
|
212
213
|
}.to_not raise_exception(ArgumentError)
|
213
214
|
end
|
214
215
|
end
|
215
216
|
|
216
217
|
describe "#get_cell" do
|
217
218
|
it "gets cell" do
|
218
|
-
|
219
|
-
|
219
|
+
dt.set_cell(0, 0, 'ABCD')
|
220
|
+
dt.get_cell(0, 0).should == 'ABCD'
|
220
221
|
end
|
221
222
|
|
222
223
|
it "raises an exception if the row_index or column_index specified is out of range" do
|
223
224
|
expect {
|
224
|
-
|
225
|
+
dt.get_cell(0, 5)
|
225
226
|
}.to raise_exception(RangeError)
|
226
227
|
|
227
228
|
expect {
|
228
|
-
|
229
|
+
dt.get_cell(5, 0)
|
229
230
|
}.to raise_exception(RangeError)
|
230
231
|
end
|
231
232
|
end
|
@@ -234,28 +235,35 @@ describe GoogleVisualr::DataTable do
|
|
234
235
|
describe "#to_js" do
|
235
236
|
context "cols" do
|
236
237
|
it "includes :id and :label when these are specified" do
|
237
|
-
|
238
|
-
|
239
|
-
data_table.add_row([1])
|
238
|
+
dt.new_column('number', 'Total', '1')
|
239
|
+
dt.add_row([1])
|
240
240
|
|
241
|
-
|
241
|
+
dt.to_js.should == "var data_table = new google.visualization.DataTable();data_table.addColumn('number', 'Total', '1');data_table.addRow([{v: 1}]);"
|
242
242
|
end
|
243
243
|
|
244
244
|
it "excludes :id and :label when these are not specified" do
|
245
|
-
|
246
|
-
|
247
|
-
|
245
|
+
dt.new_column('number')
|
246
|
+
dt.add_row([1])
|
247
|
+
|
248
|
+
dt.to_js.should == "var data_table = new google.visualization.DataTable();data_table.addColumn('number');data_table.addRow([{v: 1}]);"
|
249
|
+
end
|
248
250
|
|
249
|
-
|
251
|
+
it "includes :role and :pattern when these are specified" do
|
252
|
+
dt.new_column('string', nil, nil, 'interval', 'pattern')
|
253
|
+
dt.add_row(['interval'])
|
254
|
+
|
255
|
+
dt.to_js.should == "var data_table = new google.visualization.DataTable();data_table.addColumn({type: 'string', role: 'interval', pattern: 'pattern'});data_table.addRow([{v: 'interval'}]);"
|
250
256
|
end
|
251
257
|
end
|
252
258
|
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
+
context "valid object literal" do
|
260
|
+
it "converts object to js string" do
|
261
|
+
dt = valid_object
|
262
|
+
js = dt.to_js
|
263
|
+
js.should match /google.visualization.DataTable/i
|
264
|
+
js.should match /addColumn/i
|
265
|
+
js.should match /addRow/i
|
266
|
+
end
|
259
267
|
end
|
260
268
|
end
|
261
269
|
|
@@ -267,9 +275,9 @@ describe GoogleVisualr::DataTable do
|
|
267
275
|
end
|
268
276
|
|
269
277
|
it "initializes with a hash" do
|
270
|
-
cell = GoogleVisualr::DataTable::Cell.new( { :v => 1, :f =>
|
278
|
+
cell = GoogleVisualr::DataTable::Cell.new( { :v => 1, :f => '1.0', :p => {:style => 'border: 1px solid green;'} } )
|
271
279
|
cell.v.should == 1
|
272
|
-
cell.f.should ==
|
280
|
+
cell.f.should == '1.0'
|
273
281
|
cell.p.should == {:style => 'border: 1px solid green;'}
|
274
282
|
end
|
275
283
|
end
|
@@ -278,12 +286,12 @@ describe GoogleVisualr::DataTable do
|
|
278
286
|
context "initialized with a value" do
|
279
287
|
it "returns a json string" do
|
280
288
|
cell = GoogleVisualr::DataTable::Cell.new(1)
|
281
|
-
cell.to_js.should ==
|
289
|
+
cell.to_js.should == '{v: 1}'
|
282
290
|
end
|
283
291
|
|
284
292
|
it "returns 'null' when v is nil" do
|
285
293
|
cell = GoogleVisualr::DataTable::Cell.new(nil)
|
286
|
-
cell.to_js.should ==
|
294
|
+
cell.to_js.should == 'null'
|
287
295
|
end
|
288
296
|
end
|
289
297
|
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: google_visualr
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 2.1.
|
5
|
+
version: 2.1.1
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Winston Teo
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date:
|
13
|
+
date: 2012-01-22 00:00:00 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: bundler
|
@@ -171,7 +171,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
171
171
|
requirements: []
|
172
172
|
|
173
173
|
rubyforge_project: google_visualr
|
174
|
-
rubygems_version: 1.8.
|
174
|
+
rubygems_version: 1.8.13
|
175
175
|
signing_key:
|
176
176
|
specification_version: 3
|
177
177
|
summary: A Ruby wrapper around the Google Chart Tools that allows anyone to create the same beautiful charts with just plain Ruby.
|