googlecharts 1.6.0 → 1.6.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown CHANGED
@@ -1,5 +1,6 @@
1
1
  The goal of this Gem is to make the creation of Google Charts a simple and easy task.
2
-
2
+
3
+ require 'googlecharts'
3
4
  Gchart.line( :size => '200x300',
4
5
  :title => "example title",
5
6
  :bg => 'efefef',
@@ -175,13 +176,42 @@ Multiple axis labels are available for line charts, bar charts and scatter plots
175
176
  * y = left y-axis
176
177
  * r = right y-axis
177
178
 
178
- Gchart.line(:label_axis => 'x,y,r')
179
+ Gchart.line(:axis_with_label => 'x,y,r,t')
179
180
 
180
181
  To add labels on these axis:
181
182
 
182
- Gchart.line(:axis_labels => ['Jan|July|Jan|July|Jan', '0|100', 'A|B|C', '2005|2006|2007'])
183
+ Gchart.line(:axis_with_label => 'x,y,r,t',
184
+ :axis_labels => ['Jan|July|Jan|July|Jan', '0|100', 'A|B|C', '2005|2006|2007'])
185
+
186
+ Note that each array entry could also be an array but represent the
187
+ labels for the corresponding axis.
188
+
189
+ A question which comes back often is how do I only display the y axis
190
+ label? Solution:
191
+
192
+ Gchart.line(
193
+ :data => [0,20, 40, 60, 140, 230, 60],
194
+ :axis_with_labels => 'y')
195
+
196
+ Custom axis ranges
197
+ ---------------
198
+
199
+ If you want to display a custom range for an axis, you need to set the
200
+ range as described in the Google charts documentation: min, max, step:
201
+
202
+ Gchart.line( :data => [17, 17, 11, 8, 2],
203
+ :axis_with_labels => ['x', 'y'],
204
+ :axis_labels => [['J', 'F', 'M', 'A', 'M']],
205
+ :axis_range => [nil, [2,17,5]])
183
206
 
207
+
208
+ In this case, the custom axis range is only defined for y (second
209
+ entry) with a minimum value of 2, max 17 and a step of 5.
210
+
211
+ This is also valid if you want to set a x axis and automatically define
212
+ the y labels.
184
213
 
214
+
185
215
  Data options
186
216
  -------------
187
217
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.6.0
1
+ 1.6.1
data/lib/gchart.rb CHANGED
@@ -35,7 +35,7 @@ class Gchart
35
35
 
36
36
  attr_accessor :title, :type, :width, :height, :curved, :horizontal, :grouped, :legend, :data, :encoding, :bar_colors,
37
37
  :title_color, :title_size, :custom, :axis_with_labels, :axis_labels, :bar_width_and_spacing, :id, :alt, :klass,
38
- :range_markers, :geographical_area, :map_colors, :country_codes, :axis_range, :filename, :min, :max, :colors
38
+ :range_markers, :geographical_area, :map_colors, :country_codes, :axis_range, :filename, :min, :max, :colors, :usemap
39
39
 
40
40
  attr_accessor :bg_type, :bg_color, :bg_angle, :chart_type, :chart_color, :chart_angle, :axis_range, :thickness, :new_markers, :grid_lines
41
41
 
@@ -64,7 +64,7 @@ class Gchart
64
64
  end
65
65
 
66
66
  def self.method_missing(m, options={})
67
- raise NoMethodError, "#{m} is not a supported chart format, please use one of the following: #{supported_types}."
67
+ raise NoMethodError, "#{m} is not a supported chart format. Please use one of the following: #{supported_types}."
68
68
  end
69
69
 
70
70
  def initialize(options={})
@@ -91,9 +91,8 @@ class Gchart
91
91
  end
92
92
  end
93
93
 
94
-
95
94
  def self.supported_types
96
- self.class.types.join(' ')
95
+ self.types.join(' ')
97
96
  end
98
97
 
99
98
  # Defines the Graph size using the following format:
@@ -102,10 +101,6 @@ class Gchart
102
101
  @width, @height = size.split("x").map { |dimension| dimension.to_i }
103
102
  end
104
103
 
105
- def size=(size='300x200')
106
- @width, @height = size.split("x").map { |dimension| dimension.to_i }
107
- end
108
-
109
104
  def size
110
105
  "#{width}x#{height}"
111
106
  end
@@ -249,9 +244,11 @@ class Gchart
249
244
  end
250
245
 
251
246
  def self.jstize(string)
247
+ # See http://github.com/mattetti/googlecharts/issues#issue/27
248
+ #URI.escape( string ).gsub("%7C", "|")
252
249
  # See discussion: http://github.com/mattetti/googlecharts/commit/9b5cfb93aa51aae06611057668e631cd515ec4f3#comment_51347
253
250
  string.gsub(' ', '+').gsub(/\[|\{|\}|\\|\^|\[|\]|\`|\]/) {|c| "%#{c[0].to_s.upcase}"}
254
- # string.gsub(' ', '+').gsub(/\[|\{|\}|\||\\|\^|\[|\]|\`|\]/) {|c| "%#{c[0].to_s.upcase}"}
251
+ #string.gsub(' ', '+').gsub(/\[|\{|\}|\||\\|\^|\[|\]|\`|\]/) {|c| "%#{c[0].to_s.upcase}"}
255
252
  end
256
253
  # load all the custom aliases
257
254
  require 'gchart/aliases'
@@ -283,6 +280,7 @@ class Gchart
283
280
  image += " height=\"#{height}\""
284
281
  image += " alt=\"#{alt}\""
285
282
  image += " title=\"#{title}\"" if title
283
+ image += " usemap=\"#{usemap}\"" if usemap
286
284
  image += " />"
287
285
  end
288
286
 
@@ -451,8 +449,6 @@ class Gchart
451
449
  "#{index}:|#{labels}"
452
450
  end
453
451
  end
454
- count = labels_arr.length
455
-
456
452
  "chxl=#{labels_arr.join('|')}"
457
453
  end
458
454
 
@@ -466,16 +462,14 @@ class Gchart
466
462
 
467
463
  return unless set && set.respond_to?(:each) && set.find {|o| o}.respond_to?(:each)
468
464
 
469
- # in the case of a line graph, the first axis range should 1
470
- index_increase = type.to_s == 'line' ? 1 : 0
471
465
  'chxr=' + set.enum_for(:each_with_index).map do |axis_range, index|
472
466
  next nil if axis_range.nil? # ignore this axis
473
467
  min, max, step = axis_range
474
- if axis_range.size > 3 or step && max && step > max # this is a full series
475
- max = axis_range.last
468
+ if axis_range.size > 3 || step && max && step > max # this is a full series
469
+ max = axis_range.compact.max
476
470
  step = nil
477
471
  end
478
- [(index + index_increase), (min_value || min || 0), (max_value || max), step].compact.join(',')
472
+ [index, (min_value || min || 0), (max_value || max), step].compact.join(',')
479
473
  end.compact.join("|")
480
474
  end
481
475
 
@@ -616,7 +610,7 @@ class Gchart
616
610
  #
617
611
  def text_encoding
618
612
  chds = dataset.map{|ds| "#{ds[:min_value]},#{ds[:max_value]}" }.join(",")
619
- "t" + number_visible + ":" + datasets.map{ |ds| ds.join(',') }.join('|') + "&chds=" + chds
613
+ "t" + number_visible + ":" + datasets.map{ |ds| ds.map{|e|e||'_'}.join(',') }.join('|') + "&chds=" + chds
620
614
  end
621
615
 
622
616
  # http://code.google.com/apis/chart/#extended
@@ -7,6 +7,7 @@ class Gchart
7
7
  alias_method :bar_color=, :bar_colors=
8
8
  alias_method :line_colors=, :bar_colors=
9
9
  alias_method :line_color=, :bar_colors=
10
+ alias_method :slice_colors=, :bar_colors=
10
11
  alias_method :labels=, :legend=
11
12
  alias_method :horizontal?, :horizontal
12
13
  alias_method :grouped?, :grouped
data/spec/gchart_spec.rb CHANGED
@@ -5,6 +5,16 @@ Chart::Theme.add_theme_file("#{File.dirname(__FILE__)}/fixtures/test_theme.yml")
5
5
 
6
6
  # Time to add your specs!
7
7
  # http://rspec.rubyforge.org/
8
+ describe "The Gchart class" do
9
+ it "should show supported_types on error" do
10
+ Gchart.supported_types.should match(/line/)
11
+ end
12
+
13
+ it "should return supported types" do
14
+ Gchart.types.include?('line').should be_true
15
+ end
16
+ end
17
+
8
18
  describe "generating a default Gchart" do
9
19
 
10
20
  before(:each) do
@@ -44,7 +54,7 @@ describe "generating a default Gchart" do
44
54
  it "should use the simple encoding by default with auto max value" do
45
55
  # 9 is the max value in simple encoding, 26 being our max value the 2nd encoded value should be 9
46
56
  Gchart.line(:data => [0, 26]).should include('chd=s:A9')
47
- Gchart.line(:data => [0, 26], :max_value => 26).should include('chxr=1,0,26')
57
+ Gchart.line(:data => [0, 26], :max_value => 26, :axis_with_labels => 'y').should include('chxr=0,0,26')
48
58
  end
49
59
 
50
60
  it "should support simple encoding with and without max_value" do
@@ -70,6 +80,10 @@ describe "generating a default Gchart" do
70
80
  Gchart.line(:data => [10, 5.2, 4, 45, 78], :encoding => 'text').should include('chd=t:10,5.2,4,45,78')
71
81
  end
72
82
 
83
+ it "should be able to have missing data points with text encoding" do
84
+ Gchart.line(:data => [10, 5.2, nil, 45, 78], :encoding => 'text').should include('chd=t:10,5.2,_,45,78')
85
+ end
86
+
73
87
  it "should handle max and min values with text encoding" do
74
88
  Gchart.line(:data => [10, 5.2, 4, 45, 78], :encoding => 'text').should include('chds=0,78')
75
89
  end
@@ -92,7 +106,7 @@ describe "generating a default Gchart" do
92
106
  :bar_colors => ['FD9A3B', '4BC7DC']).should include("chxr=0,-20,100")
93
107
  end
94
108
 
95
- it "should be able to have muliple set of data with text encoding" do
109
+ it "should be able to have multiple set of data with text encoding" do
96
110
  Gchart.line(:data => [[10, 5.2, 4, 45, 78], [20, 40, 70, 15, 99]], :encoding => 'text').include?(Gchart.jstize('chd=t:10,5.2,4,45,78|20,40,70,15,99')).should be_true
97
111
  end
98
112
 
@@ -113,19 +127,19 @@ describe "generating a default Gchart" do
113
127
  end
114
128
 
115
129
  def labeled_line(options = {})
116
- Gchart.line({:data => @data, :axis_with_labels => 'x,y'}.merge options)
130
+ Gchart.line({:data => @data, :axis_with_labels => 'x,y'}.merge(options))
117
131
  end
118
132
 
119
133
  it "should display ranges properly" do
120
134
  @data = [85,107,123,131,155,172,173,189,203,222,217,233,250,239,256,267,247,261,275,295,288,305,322,307,325,347,331,346,363,382,343,359,383,352,374,393,358,379,396,416,377,398,419,380,409,426,453,432,452,465,436,460,480,440,457,474,501,457,489,507,347,373,413,402,424,448,475,488,513,475,507,530,440,476,500,518,481,512,531,367,396,423,387,415,446,478,442,469,492,463,489,508,463,491,518,549,503,526,547,493,530,549,493,520,541,564,510,535,564,492,512,537,502,530,548,491,514,538,568,524,548,568,512,533,552,577,520,545,570,516,536,555,514,536,566,521,553,579,604,541,569,595,551,581,602,549,576,606,631,589,615,650,597,624,646,672,605,626,654,584,608,631,574,597,622,559,591,614,644,580,603,629,584,615,631,558,591,618,641,314,356,395,397,429,450,421,454,477,507,458,490,560,593]
121
- labeled_line(:axis_labels => [(1.upto(24).to_a << 1)]).
122
- should include('chxr=1,85,593')
135
+ labeled_line(:axis_labels => [((1..24).to_a << 1)]).
136
+ should include('chxr=0,85,672')
123
137
  end
124
138
 
125
139
  def labeled_bar(options = {})
126
140
  Gchart.bar({:data => @data,
127
141
  :axis_with_labels => 'x,y',
128
- :axis_labels => [1.upto(12).to_a],
142
+ :axis_labels => [(1..12).to_a],
129
143
  :encoding => "text"
130
144
  }.merge(options))
131
145
  end
@@ -153,12 +167,16 @@ describe "generating a default Gchart" do
153
167
  labeled_bar(
154
168
  :axis_range => [[],[0,16]]
155
169
  ).should include('chxr=0,0|1,0,16')
170
+
171
+ Gchart.line(
172
+ :data => [0,20, 40, 60, 140, 230, 60],
173
+ :axis_with_labels => 'y').should include("chxr=0,0,230")
156
174
  end
157
175
 
158
176
  it "should take in consideration the max value when creating a range" do
159
177
  data = [85,107,123,131,155,172,173,189,203,222,217,233,250,239,256,267,247,261,275,295,288,305,322,307,325,347,331,346,363,382,343,359,383,352,374,393,358,379,396,416,377,398,419,380,409,426,453,432,452,465,436,460,480,440,457,474,501,457,489,507,347,373,413,402,424,448,475,488,513,475,507,530,440,476,500,518,481,512,531,367,396,423,387,415,446,478,442,469,492,463,489,508,463,491,518,549,503,526,547,493,530,549,493,520,541,564,510,535,564,492,512,537,502,530,548,491,514,538,568,524,548,568,512,533,552,577,520,545,570,516,536,555,514,536,566,521,553,579,604,541,569,595,551,581,602,549,576,606,631,589,615,650,597,624,646,672,605,626,654,584,608,631,574,597,622,559,591,614,644,580,603,629,584,615,631,558,591,618,641,314,356,395,397,429,450,421,454,477,507,458,490,560,593]
160
- url = Gchart.line(:data => data, :axis_with_labels => 'x,y', :axis_labels => [(1.upto(24).to_a << 1)], :max_value => 700)
161
- url.should include('chxr=1,85,700')
178
+ url = Gchart.line(:data => data, :axis_with_labels => 'x,y', :axis_labels => [((1..24).to_a << 1)], :max_value => 700)
179
+ url.should include('chxr=0,85,700')
162
180
  end
163
181
 
164
182
  end
@@ -211,8 +229,8 @@ describe "generating different type of charts" do
211
229
  end
212
230
 
213
231
  it "should not support other types" do
214
- lambda{Gchart.sexy}.should raise_error
215
- # msg => "sexy is not a supported chart format, please use one of the following: #{Gchart.supported_types}."
232
+ msg = "sexy is not a supported chart format. Please use one of the following: #{Gchart.supported_types}."
233
+ lambda{Gchart.sexy}.should raise_error(NoMethodError)
216
234
  end
217
235
 
218
236
  end
@@ -239,25 +257,25 @@ describe "range markers" do
239
257
 
240
258
  describe "when setting the orientation option" do
241
259
  before(:each) do
242
- options = {:start_position => 0.59, :stop_position => 0.61, :color => 'ff0000'}
260
+ @options = {:start_position => 0.59, :stop_position => 0.61, :color => 'ff0000'}
243
261
  end
244
262
 
245
263
  it "to vertical (R) if given a valid option" do
246
- Gchart.line(:range_markers => options.merge(:orientation => 'v')).include?('chm=R').should be_true
247
- Gchart.line(:range_markers => options.merge(:orientation => 'V')).include?('chm=R').should be_true
248
- Gchart.line(:range_markers => options.merge(:orientation => 'R')).include?('chm=R').should be_true
249
- Gchart.line(:range_markers => options.merge(:orientation => 'vertical')).include?('chm=R').should be_true
250
- Gchart.line(:range_markers => options.merge(:orientation => 'Vertical')).include?('chm=R').should be_true
264
+ Gchart.line(:range_markers => @options.merge(:orientation => 'v')).include?('chm=R').should be_true
265
+ Gchart.line(:range_markers => @options.merge(:orientation => 'V')).include?('chm=R').should be_true
266
+ Gchart.line(:range_markers => @options.merge(:orientation => 'R')).include?('chm=R').should be_true
267
+ Gchart.line(:range_markers => @options.merge(:orientation => 'vertical')).include?('chm=R').should be_true
268
+ Gchart.line(:range_markers => @options.merge(:orientation => 'Vertical')).include?('chm=R').should be_true
251
269
  end
252
270
 
253
271
  it "to horizontal (r) if given a valid option (actually anything other than the vertical options)" do
254
- Gchart.line(:range_markers => options.merge(:orientation => 'horizontal')).include?('chm=r').should be_true
255
- Gchart.line(:range_markers => options.merge(:orientation => 'h')).include?('chm=r').should be_true
256
- Gchart.line(:range_markers => options.merge(:orientation => 'etc')).include?('chm=r').should be_true
272
+ Gchart.line(:range_markers => @options.merge(:orientation => 'horizontal')).include?('chm=r').should be_true
273
+ Gchart.line(:range_markers => @options.merge(:orientation => 'h')).include?('chm=r').should be_true
274
+ Gchart.line(:range_markers => @options.merge(:orientation => 'etc')).include?('chm=r').should be_true
257
275
  end
258
276
 
259
277
  it "if left blank defaults to horizontal (r)" do
260
- Gchart.line(:range_markers => options).include?('chm=r').should be_true
278
+ Gchart.line(:range_markers => @options).include?('chm=r').should be_true
261
279
  end
262
280
  end
263
281
 
data/spec/spec_helper.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  begin
2
- require 'spec'
2
+ require 'rspec'
3
3
  rescue LoadError
4
4
  require 'rubygems'
5
5
  gem 'rspec'
6
- require 'spec'
7
- end
6
+ require 'rspec'
7
+ end
data/tasks/rspec.rake CHANGED
@@ -1,11 +1,17 @@
1
1
  begin
2
- require 'spec'
2
+ require 'rspec'
3
3
  rescue LoadError
4
4
  require 'rubygems'
5
- require 'spec'
5
+ require 'rspec'
6
6
  end
7
+
7
8
  begin
8
- require 'spec/rake/spectask'
9
+ require 'rspec/core/rake_task'
10
+
11
+ desc "Run the specs under spec/models"
12
+ RSpec::Core::RakeTask.new do |t|
13
+ # t.rspec_opts = ['--options', "spec/spec.opts"]
14
+ end
9
15
  rescue LoadError
10
16
  puts <<-EOS
11
17
  To use rspec for testing you must install rspec gem:
@@ -14,8 +20,3 @@ EOS
14
20
  exit(0)
15
21
  end
16
22
 
17
- desc "Run the specs under spec/models"
18
- Spec::Rake::SpecTask.new do |t|
19
- t.spec_opts = ['--options', "spec/spec.opts"]
20
- t.spec_files = FileList['spec/*_spec.rb']
21
- end
metadata CHANGED
@@ -1,7 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: googlecharts
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.6.0
4
+ hash: 13
5
+ prerelease: false
6
+ segments:
7
+ - 1
8
+ - 6
9
+ - 1
10
+ version: 1.6.1
5
11
  platform: ruby
6
12
  authors:
7
13
  - Matt Aimonetti
@@ -9,7 +15,7 @@ autorequire:
9
15
  bindir: bin
10
16
  cert_chain: []
11
17
 
12
- date: 2010-05-06 00:00:00 -07:00
18
+ date: 2011-02-06 00:00:00 -08:00
13
19
  default_executable:
14
20
  dependencies: []
15
21
 
@@ -24,7 +30,6 @@ extra_rdoc_files:
24
30
  - README.markdown
25
31
  - README.txt
26
32
  files:
27
- - .gitignore
28
33
  - History.txt
29
34
  - License.txt
30
35
  - Manifest.txt
@@ -64,26 +69,32 @@ homepage: http://googlecharts.rubyforge.org/
64
69
  licenses: []
65
70
 
66
71
  post_install_message:
67
- rdoc_options:
68
- - --charset=UTF-8
72
+ rdoc_options: []
73
+
69
74
  require_paths:
70
75
  - lib
71
76
  required_ruby_version: !ruby/object:Gem::Requirement
77
+ none: false
72
78
  requirements:
73
79
  - - ">="
74
80
  - !ruby/object:Gem::Version
81
+ hash: 3
82
+ segments:
83
+ - 0
75
84
  version: "0"
76
- version:
77
85
  required_rubygems_version: !ruby/object:Gem::Requirement
86
+ none: false
78
87
  requirements:
79
88
  - - ">="
80
89
  - !ruby/object:Gem::Version
90
+ hash: 3
91
+ segments:
92
+ - 0
81
93
  version: "0"
82
- version:
83
94
  requirements: []
84
95
 
85
96
  rubyforge_project:
86
- rubygems_version: 1.3.5
97
+ rubygems_version: 1.3.7
87
98
  signing_key:
88
99
  specification_version: 3
89
100
  summary: Generate charts using Google API & Ruby
data/.gitignore DELETED
@@ -1,4 +0,0 @@
1
- .DS_Store
2
- log/*
3
- .manifest
4
- pkg