mattetti-googlecharts 1.3.6 → 1.3.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/lib/gchart.rb +39 -3
  2. data/spec/gchart_spec.rb +94 -0
  3. metadata +1 -1
data/lib/gchart.rb CHANGED
@@ -3,20 +3,22 @@ require 'gchart/version'
3
3
  require 'gchart/theme'
4
4
  require "open-uri"
5
5
  require "uri"
6
+ require "cgi"
6
7
 
7
8
  class Gchart
8
9
 
9
10
  include GchartInfo
10
11
 
11
12
  @@url = "http://chart.apis.google.com/chart?"
12
- @@types = ['line', 'line_xy', 'scatter', 'bar', 'venn', 'pie', 'pie_3d', 'jstize', 'sparkline', 'meter']
13
+ @@types = ['line', 'line_xy', 'scatter', 'bar', 'venn', 'pie', 'pie_3d', 'jstize', 'sparkline', 'meter', 'map']
13
14
  @@simple_chars = ('A'..'Z').to_a + ('a'..'z').to_a + ('0'..'9').to_a
14
15
  @@chars = @@simple_chars + ['-', '.']
15
16
  @@ext_pairs = @@chars.map { |char_1| @@chars.map { |char_2| char_1 + char_2 } }.flatten
16
17
  @@file_name = 'chart.png'
17
18
 
18
19
  attr_accessor :title, :type, :width, :height, :horizontal, :grouped, :legend, :data, :encoding, :max_value, :bar_colors,
19
- :title_color, :title_size, :custom, :axis_with_labels, :axis_labels, :bar_width_and_spacing, :id, :alt, :class
20
+ :title_color, :title_size, :custom, :axis_with_labels, :axis_labels, :bar_width_and_spacing, :id, :alt, :class,
21
+ :range_markers, :geographical_area, :map_colors, :country_codes
20
22
 
21
23
  # Support for Gchart.line(:title => 'my title', :size => '400x600')
22
24
  def self.method_missing(m, options={})
@@ -28,6 +30,8 @@ class Gchart
28
30
  @@file_name = options[:filename] unless options[:filename].nil?
29
31
  options.delete(:format)
30
32
  options.delete(:filename)
33
+ #update map_colors to be bar_colors
34
+ options.update(:bar_colors => options[:map_colors]) if options.has_key?(:map_colors)
31
35
  # create the chart and return it in the format asked for
32
36
  if @@types.include?(m.to_s)
33
37
  chart = new(options.merge!({:type => m}))
@@ -191,6 +195,11 @@ class Gchart
191
195
  "chco=#{@bar_colors}"
192
196
  end
193
197
 
198
+ def set_country_codes
199
+ @country_codes = @country_codes.join() if @country_codes.is_a?(Array)
200
+ "chld=#{@country_codes}"
201
+ end
202
+
194
203
  # set bar spacing
195
204
  # chbh=
196
205
  # <bar width in pixels>,
@@ -213,6 +222,21 @@ class Gchart
213
222
  "chbh=#{width_and_spacing_values}"
214
223
  end
215
224
 
225
+ def set_range_markers
226
+ markers = case @range_markers
227
+ when Hash
228
+ set_range_marker(@range_markers)
229
+ when Array
230
+ range_markers.collect{|marker| set_range_marker(marker)}.join('|')
231
+ end
232
+ "chm=#{markers}"
233
+ end
234
+
235
+ def set_range_marker(options)
236
+ orientation = ['vertical', 'Vertical', 'V', 'v', 'R'].include?(options[:orientation]) ? 'R' : 'r'
237
+ "#{orientation},#{options[:color]},0,#{options[:start_position]},#{options[:stop_position]}#{',1' if options[:overlaid?]}"
238
+ end
239
+
216
240
  def fill_for(type=nil, color='', angle=nil)
217
241
  unless type.nil?
218
242
  case type
@@ -238,7 +262,7 @@ class Gchart
238
262
  return set_labels if @type == :pie || @type == :pie_3d || @type == :meter
239
263
 
240
264
  if @legend.is_a?(Array)
241
- "chdl=#{@legend.map{|label| "#{label}"}.join('|')}"
265
+ "chdl=#{@legend.map{|label| "#{CGI::escape(label)}"}.join('|')}"
242
266
  else
243
267
  "chdl=#{@legend}"
244
268
  end
@@ -270,6 +294,10 @@ class Gchart
270
294
  "chxl=#{labels_arr.join('|')}"
271
295
  end
272
296
 
297
+ def set_geographical_area
298
+ "chtm=#{@geographical_area}"
299
+ end
300
+
273
301
  def set_type
274
302
  case @type
275
303
  when :line
@@ -290,6 +318,8 @@ class Gchart
290
318
  "cht=ls"
291
319
  when :meter
292
320
  "cht=gom"
321
+ when :map
322
+ "cht=t"
293
323
  end
294
324
  end
295
325
 
@@ -398,6 +428,12 @@ class Gchart
398
428
  set_axis_with_labels
399
429
  when '@axis_labels'
400
430
  set_axis_labels
431
+ when '@range_markers'
432
+ set_range_markers
433
+ when '@geographical_area'
434
+ set_geographical_area
435
+ when '@country_codes'
436
+ set_country_codes
401
437
  when '@custom'
402
438
  @custom
403
439
  end
data/spec/gchart_spec.rb CHANGED
@@ -81,6 +81,7 @@ describe "generating a default Gchart" do
81
81
  Gchart.line(:axis_labels => [['Jan','July','Jan','July','Jan']]).include?(Gchart.jstize('chxl=0:|Jan|July|Jan|July|Jan')).should be_true
82
82
  Gchart.line(:axis_labels => [['Jan','July','Jan','July','Jan'], ['0','100'], ['A','B','C'], ['2005','2006','2007']]).include?(Gchart.jstize('chxl=0:|Jan|July|Jan|July|Jan|1:|0|100|2:|A|B|C|3:|2005|2006|2007')).should be_true
83
83
  end
84
+
84
85
 
85
86
  end
86
87
 
@@ -126,12 +127,64 @@ describe "generating different type of charts" do
126
127
  Gchart.meter.include?('cht=gom').should be_true
127
128
  end
128
129
 
130
+ it "should be able to generate a map chart" do
131
+ Gchart.map.should be_an_instance_of(String)
132
+ Gchart.map.include?('cht=t').should be_true
133
+ end
134
+
129
135
  it "should not support other types" do
130
136
  Gchart.sexy.should == "sexy is not a supported chart format, please use one of the following: #{Gchart.supported_types}."
131
137
  end
132
138
 
133
139
  end
134
140
 
141
+
142
+ describe "range markers" do
143
+
144
+ it "should be able to generate given a hash of range-marker options" do
145
+ Gchart.line(:range_markers => {:start_position => 0.59, :stop_position => 0.61, :color => 'ff0000'}).include?('chm=r,ff0000,0,0.59,0.61').should be_true
146
+ end
147
+
148
+ it "should be able to generate given an array of range-marker hash options" do
149
+ Gchart.line(:range_markers => [
150
+ {:start_position => 0.59, :stop_position => 0.61, :color => 'ff0000'},
151
+ {:start_position => 0, :stop_position => 0.6, :color => '666666'},
152
+ {:color => 'cccccc', :start_position => 0.6, :stop_position => 1}
153
+ ]).include?(Gchart.jstize('r,ff0000,0,0.59,0.61|r,666666,0,0,0.6|r,cccccc,0,0.6,1')).should be_true
154
+ end
155
+
156
+ it "should allow a :overlaid? to be set" do
157
+ Gchart.line(:range_markers => {:start_position => 0.59, :stop_position => 0.61, :color => 'ffffff', :overlaid? => true}).include?('chm=r,ffffff,0,0.59,0.61,1').should be_true
158
+ Gchart.line(:range_markers => {:start_position => 0.59, :stop_position => 0.61, :color => 'ffffff', :overlaid? => false}).include?('chm=r,ffffff,0,0.59,0.61').should be_true
159
+ end
160
+
161
+ describe "when setting the orientation option" do
162
+ before(:each) do
163
+ options = {:start_position => 0.59, :stop_position => 0.61, :color => 'ff0000'}
164
+ end
165
+
166
+ it "to vertical (R) if given a valid option" do
167
+ Gchart.line(:range_markers => options.merge(:orientation => 'v')).include?('chm=R').should be_true
168
+ Gchart.line(:range_markers => options.merge(:orientation => 'V')).include?('chm=R').should be_true
169
+ Gchart.line(:range_markers => options.merge(:orientation => 'R')).include?('chm=R').should be_true
170
+ Gchart.line(:range_markers => options.merge(:orientation => 'vertical')).include?('chm=R').should be_true
171
+ Gchart.line(:range_markers => options.merge(:orientation => 'Vertical')).include?('chm=R').should be_true
172
+ end
173
+
174
+ it "to horizontal (r) if given a valid option (actually anything other than the vertical options)" do
175
+ Gchart.line(:range_markers => options.merge(:orientation => 'horizontal')).include?('chm=r').should be_true
176
+ Gchart.line(:range_markers => options.merge(:orientation => 'h')).include?('chm=r').should be_true
177
+ Gchart.line(:range_markers => options.merge(:orientation => 'etc')).include?('chm=r').should be_true
178
+ end
179
+
180
+ it "if left blank defaults to horizontal (r)" do
181
+ Gchart.line(:range_markers => options).include?('chm=r').should be_true
182
+ end
183
+ end
184
+
185
+ end
186
+
187
+
135
188
  describe "a bar graph" do
136
189
 
137
190
  it "should have a default vertical orientation" do
@@ -227,6 +280,13 @@ describe "a line chart" do
227
280
  @chart.include?(Gchart.jstize("chdl=first+data+set+label|n+data+set+label")).should be_true
228
281
  end
229
282
 
283
+ it "should escape text values in url" do
284
+ title = 'Chart & Title'
285
+ legend = ['first data & set label', 'n data set label']
286
+ chart = Gchart.line(:title => title, :legend => legend)
287
+ chart.include?(Gchart.jstize("chdl=first+data+%26+set+label|n+data+set+label")).should be_true
288
+ end
289
+
230
290
  it "should be able to have one legend" do
231
291
  chart = Gchart.line(:legend => 'legend label')
232
292
  chart.include?("chdl=legend+label").should be_true
@@ -378,6 +438,40 @@ describe "a google-o-meter" do
378
438
 
379
439
  end
380
440
 
441
+ describe "a map chart" do
442
+
443
+ before(:each) do
444
+ @data = [0,100,50,32]
445
+ @geographical_area = 'usa'
446
+ @map_colors = ['FFFFFF', 'FF0000', 'FFFF00', '00FF00']
447
+ @country_codes = ['MT', 'WY', "ID", 'SD']
448
+ @chart = Gchart.map(:data => @data, :encoding => 'text', :size => '400x300',
449
+ :geographical_area => @geographical_area, :map_colors => @map_colors,
450
+ :country_codes => @country_codes)
451
+ end
452
+
453
+ it "should create a map" do
454
+ @chart.include?('cht=t').should be_true
455
+ end
456
+
457
+ it "should set the geographical area" do
458
+ @chart.include?('chtm=usa').should be_true
459
+ end
460
+
461
+ it "should set the map colors" do
462
+ @chart.include?('chco=FFFFFF,FF0000,FFFF00,00FF00').should be_true
463
+ end
464
+
465
+ it "should set the country/state codes" do
466
+ @chart.include?('chld=MTWYIDSD').should be_true
467
+ end
468
+
469
+ it "should set the chart data" do
470
+ @chart.include?('chd=t:0,100,50,32').should be_true
471
+ end
472
+
473
+ end
474
+
381
475
  describe 'exporting a chart' do
382
476
 
383
477
  it "should be available in the url format by default" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mattetti-googlecharts
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.6
4
+ version: 1.3.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matt Aimonetti