mattetti-googlecharts 1.3.7 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/gchart.rb CHANGED
@@ -4,6 +4,7 @@ require 'gchart/theme'
4
4
  require "open-uri"
5
5
  require "uri"
6
6
  require "cgi"
7
+ require 'enumerator'
7
8
 
8
9
  class Gchart
9
10
 
@@ -16,9 +17,9 @@ class Gchart
16
17
  @@ext_pairs = @@chars.map { |char_1| @@chars.map { |char_2| char_1 + char_2 } }.flatten
17
18
  @@file_name = 'chart.png'
18
19
 
19
- attr_accessor :title, :type, :width, :height, :horizontal, :grouped, :legend, :data, :encoding, :max_value, :bar_colors,
20
+ attr_accessor :title, :type, :width, :height, :horizontal, :grouped, :legend, :data, :encoding, :min_value, :max_value, :bar_colors,
20
21
  :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
22
+ :range_markers, :geographical_area, :map_colors, :country_codes, :axis_range
22
23
 
23
24
  # Support for Gchart.line(:title => 'my title', :size => '400x600')
24
25
  def self.method_missing(m, options={})
@@ -113,6 +114,26 @@ class Gchart
113
114
  end
114
115
  end
115
116
 
117
+ # returns the full data range as an array
118
+ # it also sets the data range if not defined
119
+ def full_data_range(ds)
120
+ return [@min, @max] unless (@min.nil? || @max.nil?)
121
+ @max = (max_value.nil? || max_value == 'auto') ? ds.compact.map{|mds| mds.compact.max}.max : max_value
122
+ if (min_value.nil? || min_value == 'auto')
123
+ min_ds_value = ds.compact.map{|mds| mds.compact.min}.min || 0
124
+ @min = (min_ds_value < 0) ? min_ds_value : 0
125
+ else
126
+ @min = min_value
127
+ end
128
+ @axis_range = [[@min,@max]]
129
+ end
130
+
131
+ def dataset
132
+ @dataset ||= prepare_dataset(data)
133
+ full_data_range(@dataset) unless @axis_range
134
+ @dataset
135
+ end
136
+
116
137
  def self.jstize(string)
117
138
  string.gsub(' ', '+').gsub(/\[|\{|\}|\||\\|\^|\[|\]|\`|\]/) {|c| "%#{c[0].to_s(16).upcase}"}
118
139
  end
@@ -159,7 +180,7 @@ class Gchart
159
180
  #
160
181
  def jstize(string)
161
182
  Gchart.jstize(string)
162
- end
183
+ end
163
184
 
164
185
  private
165
186
 
@@ -178,7 +199,7 @@ class Gchart
178
199
  end
179
200
 
180
201
  def set_data
181
- data = send("#{@encoding}_encoding", @data)
202
+ data = send("#{@encoding}_encoding")
182
203
  "chd=#{data}"
183
204
  end
184
205
 
@@ -294,6 +315,20 @@ class Gchart
294
315
  "chxl=#{labels_arr.join('|')}"
295
316
  end
296
317
 
318
+ # http://code.google.com/apis/chart/labels.html#axis_range
319
+ # Specify a range for axis labels
320
+ def set_axis_range
321
+ # a passed axis_range should look like:
322
+ # [[10,100]] or [[10,100,4]] or [[10,100], [20,300]]
323
+ # in the second example, 4 is the interval
324
+ dataset # just making sure we processed the data before
325
+ if axis_range && axis_range.respond_to?(:each) && axis_range.first.respond_to?(:each)
326
+ 'chxr=' + axis_range.enum_for(:each_with_index).map{|range, index| [index, range[0], range[1], range[2]].compact.join(',')}.join("|")
327
+ else
328
+ nil
329
+ end
330
+ end
331
+
297
332
  def set_geographical_area
298
333
  "chtm=#{@geographical_area}"
299
334
  end
@@ -353,8 +388,7 @@ class Gchart
353
388
  # Simple encoding has a resolution of 62 different values.
354
389
  # Allowing five pixels per data point, this is sufficient for line and bar charts up
355
390
  # to about 300 pixels. Simple encoding is suitable for all other types of chart regardless of size.
356
- def simple_encoding(dataset=[])
357
- dataset = prepare_dataset(dataset)
391
+ def simple_encoding
358
392
  @max_value = dataset.compact.map{|ds| ds.compact.max}.max if @max_value == 'auto'
359
393
 
360
394
  if @max_value == false || @max_value == 'false' || @max_value == :false || @max_value == 0
@@ -366,14 +400,19 @@ class Gchart
366
400
  end
367
401
 
368
402
  # http://code.google.com/apis/chart/#text
369
- # Text encoding has a resolution of 1,000 different values,
370
- # using floating point numbers between 0.0 and 100.0. Allowing five pixels per data point,
371
- # integers (1.0, 2.0, and so on) are sufficient for line and bar charts up to about 500 pixels.
372
- # Include a single decimal place (35.7 for example) if you require higher resolution.
373
- # Text encoding is suitable for all other types of chart regardless of size.
374
- def text_encoding(dataset=[])
375
- dataset = prepare_dataset(dataset)
376
- "t:" + dataset.map{ |ds| ds.join(',') }.join('|')
403
+ # Text encoding with data scaling lets you specify arbitrary positive or
404
+ # negative floating point numbers, in combination with a scaling parameter
405
+ # that lets you specify a custom range for your chart. This chart is useful
406
+ # when you don't want to worry about limiting your data to a specific range,
407
+ # or do the calculations to scale your data down or up to fit nicely inside
408
+ # a chart.
409
+ #
410
+ # Valid values range from (+/-)9.999e(+/-)100, and only four non-zero digits are supported (that is, 123400, 1234, 12.34, and 0.1234 are valid, but 12345, 123.45 and 123400.5 are not).
411
+ #
412
+ # This encoding is not available for maps.
413
+ #
414
+ def text_encoding
415
+ "t:" + dataset.map{ |ds| ds.join(',') }.join('|') + "&chds=#{@min},#{@max}"
377
416
  end
378
417
 
379
418
  def convert_to_extended_value(number)
@@ -384,13 +423,12 @@ class Gchart
384
423
  value.nil? ? "__" : value
385
424
  end
386
425
  end
426
+
387
427
 
388
428
  # http://code.google.com/apis/chart/#extended
389
429
  # Extended encoding has a resolution of 4,096 different values
390
430
  # and is best used for large charts where a large data range is required.
391
- def extended_encoding(dataset=[])
392
-
393
- dataset = prepare_dataset(dataset)
431
+ def extended_encoding
394
432
  @max_value = dataset.compact.map{|ds| ds.compact.max}.max if @max_value == 'auto'
395
433
 
396
434
  if @max_value == false || @max_value == 'false' || @max_value == :false
@@ -403,8 +441,11 @@ class Gchart
403
441
 
404
442
 
405
443
  def query_builder(options="")
444
+ dataset
406
445
  query_params = instance_variables.map do |var|
407
446
  case var
447
+ when '@data'
448
+ set_data unless @data == []
408
449
  # Set the graph size
409
450
  when '@width'
410
451
  set_size unless @width.nil? || @height.nil?
@@ -418,14 +459,14 @@ class Gchart
418
459
  set_colors
419
460
  when '@chart_color'
420
461
  set_colors if @bg_color.nil?
421
- when '@data'
422
- set_data unless @data == []
423
462
  when '@bar_colors'
424
463
  set_bar_colors
425
464
  when '@bar_width_and_spacing'
426
465
  set_bar_width_and_spacing
427
466
  when '@axis_with_labels'
428
467
  set_axis_with_labels
468
+ when '@axis_range'
469
+ set_axis_range if dataset
429
470
  when '@axis_labels'
430
471
  set_axis_labels
431
472
  when '@range_markers'
@@ -1,8 +1,8 @@
1
1
  module GchartInfo #:nodoc:
2
2
  module VERSION #:nodoc:
3
3
  MAJOR = 1
4
- MINOR = 3
5
- TINY = 6
4
+ MINOR = 4
5
+ TINY = 0
6
6
 
7
7
  STRING = [MAJOR, MINOR, TINY].join('.')
8
8
  end
data/spec/gchart_spec.rb CHANGED
@@ -60,6 +60,28 @@ describe "generating a default Gchart" do
60
60
 
61
61
  it "should be able to have data with text encoding" do
62
62
  Gchart.line(:data => [10, 5.2, 4, 45, 78], :encoding => 'text').include?('chd=t:10,5.2,4,45,78').should be_true
63
+ end
64
+
65
+ it "should handle max and min values with text encoding" do
66
+ Gchart.line(:data => [10, 5.2, 4, 45, 78], :encoding => 'text').include?('chds=0,78').should be_true
67
+ end
68
+
69
+ it "should automatically handle negative values with proper max/min limits when using text encoding" do
70
+ Gchart.line(:data => [-10, 5.2, 4, 45, 78], :encoding => 'text').include?('chds=-10,78').should be_true
71
+ end
72
+
73
+ it "should handle negative values with manual max/min limits when using text encoding" do
74
+ Gchart.line(:data => [-10, 5.2, 4, 45, 78], :encoding => 'text', :min_value => -20, :max_value => 100).include?('chds=-20,100').should be_true
75
+ end
76
+
77
+ it "should set the proper axis values when using text encoding and negative values" do
78
+ Gchart.bar( :data => [[-10], [100]],
79
+ :encoding => 'text',
80
+ :horizontal => true,
81
+ :min_value => -20,
82
+ :max_value => 100,
83
+ :axis_with_labels => 'x',
84
+ :bar_colors => ['FD9A3B', '4BC7DC']).should include("chxr=0,-20,100")
63
85
  end
64
86
 
65
87
  it "should be able to have muliple set of data with text encoding" 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.7
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matt Aimonetti