gchartrb 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/CREDITS CHANGED
@@ -1 +1,2 @@
1
- Will Fitzgerald - http://www.entish.org/willwhim
1
+ Will Fitzgerald - http://www.entish.org/willwhim
2
+ Bryan Donovan
data/History.txt CHANGED
@@ -1,3 +1,12 @@
1
+ == 0.4
2
+
3
+ * Fixed issue when all data points are 0 in Line XY Chart (Thanks Cedric)
4
+ * Proper calculation of max values for Line XY chart (Thanks Bryan Donovan)
5
+
6
+ == 0.3 / 2007-12-11
7
+
8
+ * Google changed Pie chart implementation to disable legends. Fixed in API.
9
+
1
10
  == 0.2 / 2007-12-11
2
11
 
3
12
  * Removed hoe dependency
data/Rakefile CHANGED
@@ -8,7 +8,7 @@ class Hoe
8
8
  def extra_deps; @extra_deps.reject { |x| Array(x).first == "hoe" } end
9
9
  end # copied from the Rakefile of the sup project
10
10
 
11
- Hoe.new('gchartrb', "0.3.0") do |p|
11
+ Hoe.new('gchartrb', "0.4.0") do |p|
12
12
  p.rubyforge_name = 'gchartrb'
13
13
  p.author = 'Deepak Jois'
14
14
  p.email = 'deepak.jois@gmail.com'
data/TODO CHANGED
@@ -1,13 +1,13 @@
1
1
  TODO List (Features)
2
2
  ====================
3
- * Axis labels
4
- * Grid lines
5
3
  * Markers
6
4
  * Line Styles
7
5
  * Fill Area
8
6
  * Handle Missing Values (?)
9
7
  * Other charts (Scatter)
10
8
  * Download image
9
+ * Friendly color values
10
+ * Helper methods for standard chart types like Sparklines etc.
11
11
 
12
12
  TODO List (Other)
13
13
  =================
@@ -15,9 +15,10 @@ TODO List (Other)
15
15
 
16
16
  Code Level Improvements
17
17
  =======================
18
+ * Automate upload to google code
18
19
  * Write tests that render different kinds of graphs
19
- * Remove the need for core_ext.rb file (stupid idea I was trying out earlier)
20
20
  * Use splats instead of attribute accessors to provide a neater API (?)
21
+ * Clean up encoding, especially the handling of 'missing' values
21
22
  * Improved error handling and reporting for things
22
- * Number of labels and number of postions in axis must be the same
23
+ * Number of labels and number of postions in axis must be the same (?)
23
24
  * Axis styles need to be specified in a certain way
@@ -276,11 +276,11 @@ module GoogleChart
276
276
  def encode_data(values, max_value=nil)
277
277
  case data_encoding
278
278
  when :simple
279
- simple_encode(values)
279
+ simple_encode(values, max_value)
280
280
  when :text
281
- text_encode(values)
281
+ text_encode(values, max_value)
282
282
  when :extended
283
- extended_encode(values)
283
+ extended_encode(values, max_value)
284
284
  else
285
285
  raise "Illegal Encoding Specified"
286
286
  end
@@ -291,8 +291,13 @@ module GoogleChart
291
291
  max_value = values.max unless max_value
292
292
 
293
293
  chart_data = values.collect do |val|
294
- if val.to_i >=0
294
+ if val.to_i >=0
295
+ puts max_value
296
+ if max_value == 0
297
+ SIMPLE_ENCODING[0]
298
+ else
295
299
  SIMPLE_ENCODING[(alphabet_length * val / max_value).to_i]
300
+ end
296
301
  else
297
302
  "_"
298
303
  end
@@ -304,13 +309,23 @@ module GoogleChart
304
309
  def text_encode(values, max_value=nil)
305
310
  max_value = values.max unless max_value
306
311
  values.inject("") { |sum, v|
307
- sum += ( "%.1f" % (v*100/max_value) ) + ","
312
+ if max_value == 0
313
+ sum += "0,"
314
+ else
315
+ sum += ( "%.1f" % (v*100/max_value) ) + ","
316
+ end
308
317
  }.chomp(",")
309
318
  end
310
319
 
311
320
  def extended_encode(values, max_value)
312
321
  max_value = values.max unless max_value
313
- values.collect { |v| @@complex_encoding[(v * 4095/max_value).to_i]}.join('')
322
+ values.collect { |v|
323
+ if max_value == 0
324
+ @@complex_encoding[0]
325
+ else
326
+ @@complex_encoding[(v * 4095/max_value).to_i]
327
+ end
328
+ }.join('')
314
329
  end
315
330
 
316
331
  def join_encoded_data(encoded_data)
@@ -1,73 +1,62 @@
1
1
  require File.dirname(__FILE__) + '/base'
2
2
  module GoogleChart
3
- # Generates a Line Chart. You can specify whether it is a standard line chart or an XY Line chart
4
- #
5
- # ==== Examples
6
- #
7
- # # Line Chart
8
- # lc = GoogleChart::LineChart.new('320x200', "Line Chart", false)
9
- # lc.data "Trend 1", [5,4,3,1,3,5,6], '0000ff'
10
- # lc.data "Trend 2", [1,2,3,4,5,6], '00ff00'
11
- # lc.data "Trend 3", [6,5,4,3,2,1], 'ff0000'
12
- # puts lc.to_url
13
- #
14
- # # Line XY Chart
15
- # lcxy = GoogleChart::LineChart.new('320x200', "Line XY Chart", true)
16
- # lcxy.data "Trend 1", [[1,1], [2,2], [3,3], [4,4]], '0000ff'
17
- # lcxy.data "Trend 2", [[4,5], [2,2], [1,1], [3,4]], '00ff00'
18
- # puts lcxy.to_url
19
- class LineChart < Base
20
- attr_accessor :is_xy
21
-
22
- # Initializes a Line Chart object with a +chart_size+ and +chart_title+. Specify +is_xy+ as +true+ to generate a Line XY chart
23
- def initialize(chart_size='300x200', chart_title=nil, is_xy=false)
24
- super(chart_size, chart_title)
25
- self.is_xy = is_xy
26
- end
27
-
28
- # If you want the Line Chart to be an XY chart, set the value to <tt>true</tt>, otherwise <tt>false</tt>.
29
- #
30
- # ==== Examples
31
- # A line chart takes data in the following format (both X and Y coordinates need to be specified)
32
- # lcxy = GoogleChart::LineChart.new('320x200', "Line XY Chart", true)
33
- # lcxy.data "Trend 1", [[1,1], [2,2], [3,3], [4,4]], '0000ff'
34
- # puts lcxy.to_url
35
- def is_xy=(value)
36
- @is_xy = value
37
- if value
38
- self.chart_type = :lxy
39
- else
40
- self.chart_type = :lc
41
- end
42
- end
43
-
44
- def process_data
45
- if self.is_xy or @data.size > 1
46
- if self.is_xy # XY Line graph data series
47
- max_value = @data.flatten.max
48
- x_data = @data.collect do |series|
49
- series.collect { |val| val.first }
50
- end
51
-
52
- y_data = @data.collect do |series|
53
- series.collect { |val| val.last }
54
- end
55
-
56
- encoded_data = []
57
- @data.size.times { |i|
58
- # Interleave X and Y co-ordinate data
59
- encoded_data << join_encoded_data([encode_data(x_data[i],max_value), encode_data(y_data[i],max_value)])
60
- }
61
- join_encoded_data(encoded_data)
62
- else # Line graph multiple data series
63
- max_value = @data.flatten.max
64
- join_encoded_data(@data.collect { |series|
65
- encode_data(series, max_value)
66
- })
67
- end
68
- else
69
- encode_data(@data.flatten)
70
- end
3
+ class LineChart < Base
4
+ attr_accessor :is_xy
5
+
6
+ def initialize(chart_size='300x200', chart_title=nil, is_xy=false)
7
+ super(chart_size, chart_title)
8
+ self.is_xy = is_xy
9
+ end
10
+
11
+ def is_xy=(value)
12
+ @is_xy = value
13
+ if value
14
+ self.chart_type = :lxy
15
+ else
16
+ self.chart_type = :lc
17
+ end
18
+ end
19
+
20
+ def max_x_value
21
+ puts "Max x value is #{x_data.flatten.max}"
22
+ x_data.flatten.max
23
+ end
24
+
25
+ def max_y_value
26
+ puts "Max y value is #{y_data.flatten.max}"
27
+ y_data.flatten.max
28
+ end
29
+
30
+ def x_data
31
+ @data.collect do |series|
32
+ series.collect { |val| val.first }
33
+ end
34
+ end
35
+
36
+ def y_data
37
+ @data.collect do |series|
38
+ series.collect { |val| val.last }
39
+ end
40
+ end
41
+
42
+ def process_data
43
+ if self.is_xy or @data.size > 1
44
+ if self.is_xy # XY Line graph data series
45
+ encoded_data = []
46
+ @data.size.times { |i|
47
+ # Interleave X and Y co-ordinate data
48
+ encoded_data << join_encoded_data([encode_data(x_data[i],max_x_value), encode_data(y_data[i],max_y_value)])
49
+ }
50
+ join_encoded_data(encoded_data)
51
+ else # Line graph multiple data series
52
+ max_value = @data.flatten.max
53
+ join_encoded_data(@data.collect { |series|
54
+ encode_data(series, max_value)
55
+ })
71
56
  end
57
+ else
58
+ encode_data(@data.flatten)
59
+ end
72
60
  end
73
- end
61
+ end
62
+ end
data/lib/test.rb CHANGED
@@ -70,3 +70,11 @@ lc.data "Trend 2", [1,2,3,4,5,6], '00ff00'
70
70
  lc.data "Trend 3", [6,5,4,3,2,1], 'ff0000'
71
71
  puts lc.to_url({:chm => "r,000000,0,0.1,0.5"}) # Single black line as a horizontal marker
72
72
 
73
+ # Bryan Error condition
74
+ lcxy = GoogleChart::LineChart.new('320x200', "Line XY Chart", true)
75
+ lcxy.data 'A', [[0, 32], [1, 15], [2, 23], [3, 18], [4, 41], [5, 53]],'0000ff'
76
+ lcxy.data 'B', [[0, 73], [1, 0], [2, 28], [3, 0], [4, 333], [5, 0]], '00ff00'
77
+ lcxy.data 'C', [[0, 22], [1, 26], [2, 14], [3, 33], [4, 17], [5, 7]], 'ff0000'
78
+ lcxy.data 'D', [[0, 4], [1, 39], [2, 0], [3, 5], [4, 11], [5, 14]], 'cc00ff'
79
+ puts lcxy.to_url
80
+
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.4
3
3
  specification_version: 1
4
4
  name: gchartrb
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.3.0
7
- date: 2007-12-11 00:00:00 +08:00
6
+ version: 0.4.0
7
+ date: 2007-12-12 00:00:00 +08:00
8
8
  summary: Ruby Wrapper for the Google Chart API
9
9
  require_paths:
10
10
  - lib