google_visualr 2.1.0 → 2.1.1

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/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- google_visualr (2.1.0)
4
+ google_visualr (2.1.1)
5
5
 
6
6
  GEM
7
7
  remote: http://rubygems.org/
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
- *Version 2.1.0*
98
- * Added `#render_chart` as a helper method in Rails views.
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
- def new_column(type, label=nil, id =nil)
79
- @cols << { :type => type, :label => label, :id => id }
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
- js << "#{column[:type]}'"
205
- js << ", '#{column[:label]}'" unless column[:label].nil?
206
- js << ", '#{column[:id]}'" unless column[:id].nil?
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
 
@@ -1,3 +1,3 @@
1
1
  module GoogleVisualr
2
- VERSION = "2.1.0"
2
+ VERSION = "2.1.1"
3
3
  end
@@ -2,8 +2,9 @@ require 'spec_helper'
2
2
 
3
3
  describe GoogleVisualr::DataTable do
4
4
 
5
- def valid_object
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 == {:id => nil, :label => nil, :type => 'string'}
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 = [ {:id => 'A', :label => 'NEW A', :type => 'string'}, {:id => 'B', :label => 'NEW B', :type => 'string'} ]
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.first.should == columns.first
68
- dt.cols.last.should == columns.last
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
- @dt = GoogleVisualr::DataTable.new
75
- @dt.new_column({:type => 'number'})
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
- @dt.rows[0][0].v.should == 1
82
- @dt.rows[1][0].v.should == 2
83
- @dt.rows[2][0].v.should == 3
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
- @dt.get_column(0).should == [1,2,3]
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
- @dt = GoogleVisualr::DataTable.new
97
- @dt.new_columns( [ {:type => 'number'}, {:type => 'string'} ] )
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
- @dt.add_row
105
- @dt.rows.size.should == 1
106
- @dt.rows[0].should be_empty
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
- @dt.add_row([1, 'A'])
113
- @dt.rows.size.should == 1
114
- @dt.rows[0][0].v.should == 1
115
- @dt.rows[0][1].v.should == 'A'
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
- @dt.add_rows(2)
125
- @dt.rows.size.should == 2
126
- @dt.rows[0].should be_empty
127
- @dt.rows[1].should be_empty
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
- @dt.add_rows( [ [1, 'A'], [2, 'B'] ] )
134
- @dt.rows.size.should == 2
136
+ dt.add_rows( [ [1, 'A'], [2, 'B'] ] )
137
+ dt.rows.size.should == 2
135
138
 
136
- @dt.rows[0][0].v.should == 1
137
- @dt.rows[0][1].v.should == 'A'
138
- @dt.rows[1][0].v.should == 2
139
- @dt.rows[1][1].v.should == 'B'
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
- @dt.add_rows( [ [1, 'A'], [2, 'B'] ] )
147
- @dt.rows.size.should == 2
149
+ dt.add_rows( [ [1, 'A'], [2, 'B'] ] )
150
+ dt.rows.size.should == 2
148
151
 
149
- @dt.get_row(0).should == [1, 'A']
150
- @dt.get_row(1).should == [2, 'B']
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
- @dt = GoogleVisualr::DataTable.new
158
- @dt.new_columns( [ {:type => 'string'}, {:type => 'number'}, {:type => 'boolean'}, {:type => 'datetime'}, {:type => 'date'} ] )
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
- @dt.set_cell(0, 0, {:v => 'ABCD'})
166
- @dt.set_cell(0, 1, 1000)
166
+ dt.set_cell(0, 0, {:v => 'ABCD'})
167
+ dt.set_cell(0, 1, 1000)
167
168
 
168
- @dt.get_row(0).should == ['ABCD', 1000]
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
- @dt.set_cell(5, 0, 1000)
174
+ dt.set_cell(5, 0, 1000)
174
175
  }.to raise_exception(RangeError)
175
176
 
176
177
  expect {
177
- @dt.set_cell(0, 5, 1000)
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
- @dt.set_cell(0, col, value)
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
- @dt.set_cell(0, 0, nil)
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
- @dt.set_cell(0, 0, 'ABCD')
219
- @dt.get_cell(0, 0).should == 'ABCD'
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
- @dt.get_cell(0, 5)
225
+ dt.get_cell(0, 5)
225
226
  }.to raise_exception(RangeError)
226
227
 
227
228
  expect {
228
- @dt.get_cell(5, 0)
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
- data_table = GoogleVisualr::DataTable.new()
238
- data_table.new_column("Total", "Total", "1")
239
- data_table.add_row([1])
238
+ dt.new_column('number', 'Total', '1')
239
+ dt.add_row([1])
240
240
 
241
- data_table.to_js.should == "var data_table = new google.visualization.DataTable();data_table.addColumn('Total', 'Total', '1');data_table.addRow([{v: 1}]);"
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
- data_table = GoogleVisualr::DataTable.new()
246
- data_table.new_column("Total")
247
- data_table.add_row([1])
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
- data_table.to_js.should == "var data_table = new google.visualization.DataTable();data_table.addColumn('Total');data_table.addRow([{v: 1}]);"
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
- it "converts object to js string" do
254
- dt = valid_object
255
- js = dt.to_js
256
- js.should match /google.visualization.DataTable/i
257
- js.should match /addColumn/i
258
- js.should match /addRow/i
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 => "1.0", :p => {:style => 'border: 1px solid green;'} } )
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 == "1.0"
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 == "{v: 1}"
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 == "null"
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.0
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: 2011-08-13 00:00:00 Z
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.5
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.