gchartrb 0.5 → 0.5.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/History.txt +7 -3
- data/Manifest.txt +1 -0
- data/README.txt +0 -1
- data/Rakefile +1 -1
- data/lib/example.rb +70 -0
- data/lib/google_chart/bar_chart.rb +3 -4
- data/lib/google_chart/base.rb +30 -3
- data/lib/google_chart/line_chart.rb +3 -4
- data/lib/google_chart/pie_chart.rb +1 -1
- data/lib/google_chart/scatter_chart.rb +1 -1
- data/lib/test.rb +16 -9
- metadata +2 -1
data/History.txt
CHANGED
@@ -1,13 +1,17 @@
|
|
1
|
-
== 0.5
|
1
|
+
== 0.5.1 / 2007-12-12
|
2
|
+
|
3
|
+
* Added support for max_value method to specify maximum values to plot against
|
4
|
+
|
5
|
+
== 0.5 / 2007-12-12
|
2
6
|
|
3
7
|
* Added Scatter Chart Support
|
4
8
|
* Documentation Updates
|
5
9
|
|
6
|
-
== 0.4.1
|
10
|
+
== 0.4.1 / 2007-12-12
|
7
11
|
|
8
12
|
* Removed some spurious debug statements
|
9
13
|
|
10
|
-
== 0.4
|
14
|
+
== 0.4 / 2007-12-12
|
11
15
|
|
12
16
|
* Fixed issue when all data points are 0 in Line XY Chart (Thanks Cedric)
|
13
17
|
* Proper calculation of max values for Line XY chart (Thanks Bryan Donovan)
|
data/Manifest.txt
CHANGED
data/README.txt
CHANGED
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.5") do |p|
|
11
|
+
Hoe.new('gchartrb', "0.5.1") do |p|
|
12
12
|
p.rubyforge_name = 'gchartrb'
|
13
13
|
p.author = 'Deepak Jois'
|
14
14
|
p.email = 'deepak.jois@gmail.com'
|
data/lib/example.rb
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
require 'google_chart'
|
2
|
+
|
3
|
+
# Some examples from http://24ways.org/2007/tracking-christmas-cheer-with-google-charts
|
4
|
+
|
5
|
+
# Pie Chart
|
6
|
+
pc = GoogleChart::PieChart.new("600x300", "Food and Drinks Consumed Christmas 2007")
|
7
|
+
pc.data "Egg nog", 10, '00AF33'
|
8
|
+
pc.data "Christmas Ham", 20, '4BB74C'
|
9
|
+
pc.data "Milk (not including egg nog)", 8, 'EE2C2C'
|
10
|
+
pc.data "Cookies", 25, 'CC3232'
|
11
|
+
pc.data "Roasted Chestnuts", 5, '33FF33'
|
12
|
+
pc.data "Chocolate", 3, '66FF66'
|
13
|
+
pc.data "Various Other Beverages", 15, '9AFF9A'
|
14
|
+
pc.data "Various Other Foods", 9, 'C1FFC1'
|
15
|
+
pc.data "Snacks", 5, 'CCFFCC'
|
16
|
+
puts pc.to_url
|
17
|
+
|
18
|
+
# Line Chart
|
19
|
+
x_axis_labels = (1..31).to_a.collect do |v|
|
20
|
+
if [1,6,25,26,31].member?(v)
|
21
|
+
if v == 1
|
22
|
+
"Dec 1st"
|
23
|
+
elsif v == 31
|
24
|
+
"Dec 31st"
|
25
|
+
elsif v
|
26
|
+
"#{v}th"
|
27
|
+
end
|
28
|
+
else
|
29
|
+
nil
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
y_axis_labels = (0..10).to_a.collect do |v|
|
34
|
+
val = 10 * v
|
35
|
+
if val ==50 or val == 100
|
36
|
+
val.to_s
|
37
|
+
else
|
38
|
+
nil
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
series_1_y = [30,45,20,50,15,80,60,70,40,55,80]
|
43
|
+
series_2_y = [50,10,30,55,60]
|
44
|
+
|
45
|
+
series_1_x = [1,6,8,10,18,23,25,26,28,29,31]
|
46
|
+
series_2_x = [1,4,6,9,11]
|
47
|
+
|
48
|
+
series_1_xy = []
|
49
|
+
series_2_xy = []
|
50
|
+
|
51
|
+
series_1_x.each_with_index do |v,i|
|
52
|
+
series_1_xy[i] = [v-1, series_1_y[i] ]
|
53
|
+
end
|
54
|
+
|
55
|
+
series_2_x.each_with_index do |v,i|
|
56
|
+
series_2_xy[i] = [v-1, series_2_y[i ] ]
|
57
|
+
end
|
58
|
+
|
59
|
+
puts series_1_xy.inspect
|
60
|
+
puts series_2_xy.inspect
|
61
|
+
|
62
|
+
lcxy = GoogleChart::LineChart.new('800x300', "Projected Christmas Cheer for 2007", true)
|
63
|
+
lcxy.data "2006", series_1_xy, '458B00'
|
64
|
+
lcxy.data "2007", series_2_xy, 'CD2626'
|
65
|
+
lcxy.max_value [30,100]
|
66
|
+
lcxy.data_encoding = :text
|
67
|
+
lcxy.axis :x, :labels => x_axis_labels
|
68
|
+
lcxy.axis :y, :labels => y_axis_labels
|
69
|
+
lcxy.grid :x_step => 3.333, :y_step => 10, :length_segment => 1, :length_blank => 3
|
70
|
+
puts lcxy.to_url
|
@@ -35,13 +35,12 @@ module GoogleChart
|
|
35
35
|
end
|
36
36
|
|
37
37
|
def process_data
|
38
|
-
if @data.size > 1
|
39
|
-
max_value = @data.flatten.max
|
38
|
+
if @data.size > 1
|
40
39
|
join_encoded_data(@data.collect { |series|
|
41
|
-
encode_data(series,
|
40
|
+
encode_data(series, max_data_value)
|
42
41
|
})
|
43
42
|
else
|
44
|
-
encode_data(@data.flatten)
|
43
|
+
encode_data(@data.flatten,max_data_value)
|
45
44
|
end
|
46
45
|
end
|
47
46
|
|
data/lib/google_chart/base.rb
CHANGED
@@ -51,7 +51,7 @@ module GoogleChart
|
|
51
51
|
add_title if chart_title.to_s.length > 0
|
52
52
|
|
53
53
|
params.merge!(extras)
|
54
|
-
query_string = params.map { |k,v| "#{k}=#{URI.escape(v.to_s)}" }.join('&')
|
54
|
+
query_string = params.map { |k,v| "#{k}=#{URI.escape(v.to_s).gsub(/%20/,'+').gsub(/%7C/,'|')}" }.join('&')
|
55
55
|
BASE_URL + query_string
|
56
56
|
end
|
57
57
|
|
@@ -78,6 +78,29 @@ module GoogleChart
|
|
78
78
|
@colors << color if color
|
79
79
|
end
|
80
80
|
|
81
|
+
# Allows (optional) setting of a max value for the chart, which will be used for data encoding and axis plotting.
|
82
|
+
# The value to pass depends on the type of chart
|
83
|
+
# * For Line Chart and Bar Charts it should be a single integer or float value
|
84
|
+
# * For Scatter Charts and Line XY Charts, you MUST pass an array containing the maximum values for X and Y
|
85
|
+
#
|
86
|
+
# ==== Examples
|
87
|
+
# For bar charts
|
88
|
+
# bc.max_value 5 # 5 will be used to calculate the relative encoding values
|
89
|
+
# For scatter chart
|
90
|
+
# sc.max_value [5,6] # 5 is the max x value and 6 is the max y value
|
91
|
+
#
|
92
|
+
# Note : MAKE SURE you are passing the right values otherwise an exception will be raised
|
93
|
+
def max_value(value)
|
94
|
+
if [:lxy, :s].member?(self.chart_type) and value.is_a?(Array)
|
95
|
+
@max_x = value.first
|
96
|
+
@max_y = value.last
|
97
|
+
elsif [:lc,:bhg,:bhs,:bvg,:bvs] and (value.is_a?(Integer) or value.is_a?(Float))
|
98
|
+
@max_data = value
|
99
|
+
else
|
100
|
+
raise "Invalid max value for this chart type"
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
81
104
|
# Adds a background or chart fill. Call this option twice if you want both a background and a chart fill
|
82
105
|
# [+bg_or_c+] Can be one of <tt>:background</tt> or <tt>:chart</tt> depending on the kind of fill requested
|
83
106
|
# [+type+] Can be one of <tt>:solid</tt>, <tt>:gradient</tt> or <tt>:stripes</tt>
|
@@ -331,14 +354,18 @@ module GoogleChart
|
|
331
354
|
encoded_data.join((self.data_encoding == :simple) ? "," : "|")
|
332
355
|
end
|
333
356
|
|
357
|
+
def max_data_value
|
358
|
+
@max_data or @data.flatten.max
|
359
|
+
end
|
360
|
+
|
334
361
|
## Applicable to Line Charts and Scatter Charts only
|
335
362
|
|
336
363
|
def max_x_value
|
337
|
-
x_data.flatten.max
|
364
|
+
@max_x or x_data.flatten.max
|
338
365
|
end
|
339
366
|
|
340
367
|
def max_y_value
|
341
|
-
y_data.flatten.max
|
368
|
+
@max_y or y_data.flatten.max
|
342
369
|
end
|
343
370
|
|
344
371
|
def x_data
|
@@ -51,14 +51,13 @@ module GoogleChart
|
|
51
51
|
encoded_data << join_encoded_data([encode_data(x_data[i],max_x_value), encode_data(y_data[i],max_y_value)])
|
52
52
|
}
|
53
53
|
join_encoded_data(encoded_data)
|
54
|
-
else # Line graph multiple data series
|
55
|
-
max_value = @data.flatten.max
|
54
|
+
else # Line graph multiple data series
|
56
55
|
join_encoded_data(@data.collect { |series|
|
57
|
-
encode_data(series,
|
56
|
+
encode_data(series, max_data_value)
|
58
57
|
})
|
59
58
|
end
|
60
59
|
else
|
61
|
-
encode_data(@data.flatten)
|
60
|
+
encode_data(@data.flatten, max_data_value)
|
62
61
|
end
|
63
62
|
end
|
64
63
|
end
|
@@ -10,7 +10,7 @@ module GoogleChart
|
|
10
10
|
# puts sc.to_url
|
11
11
|
class ScatterChart < Base
|
12
12
|
|
13
|
-
# Initializes the
|
13
|
+
# Initializes the Scatter Chart with a +chart_size+ (in WIDTHxHEIGHT format) and a +chart_title+
|
14
14
|
def initialize(chart_size='300x200', chart_title=nil)
|
15
15
|
super(chart_size, chart_title)
|
16
16
|
self.chart_type = :s
|
data/lib/test.rb
CHANGED
@@ -6,7 +6,7 @@ pc.data "Apples", 40
|
|
6
6
|
pc.data "Banana", 20
|
7
7
|
pc.data "Peach", 30
|
8
8
|
pc.data "Orange", 60
|
9
|
-
puts "
|
9
|
+
puts "\nPie Chart"
|
10
10
|
puts pc.to_url
|
11
11
|
|
12
12
|
# Line Chart
|
@@ -19,7 +19,7 @@ lc.data "Trend 3", [6,5,4,3,2,1], 'ff0000'
|
|
19
19
|
lc.axis :y, :range => [0,6], :color => 'ff00ff', :font_size => 16, :alignment => :center
|
20
20
|
lc.axis :x, :range => [0,6], :color => '00ffff', :font_size => 16, :alignment => :center
|
21
21
|
lc.grid :x_step => 100.0/6.0, :y_step => 100.0/6.0, :length_segment => 1, :length_blank => 0
|
22
|
-
puts "
|
22
|
+
puts "\nLine Chart"
|
23
23
|
puts lc.to_url
|
24
24
|
|
25
25
|
# Bar Chart
|
@@ -27,14 +27,14 @@ bc = GoogleChart::BarChart.new('800x200', "Bar Chart", :vertical, false)
|
|
27
27
|
bc.data "Trend 1", [5,4,3,1,3,5], '0000ff'
|
28
28
|
bc.data "Trend 2", [1,2,3,4,5,6], 'ff0000'
|
29
29
|
bc.data "Trend 3", [6,5,4,4,5,6], '00ff00'
|
30
|
-
puts "
|
30
|
+
puts "\nBar Chart"
|
31
31
|
puts bc.to_url
|
32
32
|
|
33
33
|
# Line XY Chart
|
34
34
|
lcxy = GoogleChart::LineChart.new('320x200', "Line XY Chart", true)
|
35
35
|
lcxy.data "Trend 1", [[1,1], [2,2], [3,3], [4,4]], '0000ff'
|
36
36
|
lcxy.data "Trend 2", [[4,5], [2,2], [1,1], [3,4]], '00ff00'
|
37
|
-
puts "
|
37
|
+
puts "\nLine XY Chart"
|
38
38
|
puts lcxy.to_url
|
39
39
|
|
40
40
|
# Venn Diagram
|
@@ -49,32 +49,36 @@ vd.data "Blue", 100, '0000ff'
|
|
49
49
|
vd.data "Green", 80, '00ff00'
|
50
50
|
vd.data "Red", 60, 'ff0000'
|
51
51
|
vd.intersections 30,30,30,10
|
52
|
-
puts "
|
52
|
+
puts "\nVenn Diagram"
|
53
53
|
puts vd.to_url
|
54
54
|
|
55
55
|
# Scatter Chart
|
56
56
|
sc = GoogleChart::ScatterChart.new('320x200',"Scatter Chart")
|
57
57
|
sc.data "Scatter Set", [[1,1,], [2,2], [3,3], [4,4]]
|
58
|
-
sc.
|
59
|
-
sc.axis :
|
58
|
+
sc.max_value [5,5] # Setting the max value
|
59
|
+
sc.axis :x, :range => [0,5]
|
60
|
+
sc.axis :y, :range => [0,5], :labels => [0,1,2,3,4,5]
|
60
61
|
sc.point_sizes [10,15,30,55] # Optional
|
61
|
-
puts "
|
62
|
+
puts "\nScatter Chart"
|
62
63
|
puts sc.to_url
|
63
64
|
|
64
65
|
# Solid fill
|
65
66
|
lc.fill(:background, :solid, {:color => 'fff2cc'})
|
66
67
|
lc.fill(:chart, :solid, {:color => 'ffcccc'})
|
68
|
+
puts "\nLine Chart with Solid Fill"
|
67
69
|
puts lc.to_url
|
68
70
|
|
69
71
|
# Gradient fill
|
70
72
|
lc.fill :background, :gradient, :angle => 0, :color => [['76A4FB',1],['ffffff',0]]
|
71
73
|
lc.fill :chart, :gradient, :angle => 0, :color => [['76A4FB',1],
|
72
74
|
['ffffff',0]]
|
75
|
+
puts "\nLine Chart with Gradient Fill"
|
73
76
|
puts lc.to_url
|
74
77
|
|
75
78
|
# Stripes Fill
|
76
79
|
lc.fill :chart, :stripes, :angle => 90, :color => [['76A4FB',0.2],
|
77
80
|
['ffffff',0.2]]
|
81
|
+
puts "\nLine Chart with Stripes Fill"
|
78
82
|
puts lc.to_url
|
79
83
|
|
80
84
|
# Adding Extra params
|
@@ -82,7 +86,9 @@ lc = GoogleChart::LineChart.new('320x200', "Line Chart", false)
|
|
82
86
|
lc.data "Trend 1", [5,4,3,1,3,5,6], '0000ff'
|
83
87
|
lc.data "Trend 2", [1,2,3,4,5,6], '00ff00'
|
84
88
|
lc.data "Trend 3", [6,5,4,3,2,1], 'ff0000'
|
85
|
-
|
89
|
+
lc.max_value 10 # Setting max value for simple line chart
|
90
|
+
puts "\nLine Chart with Horizontal range marker using extra params"
|
91
|
+
puts lc.to_url({:chm => "r,000000,0,0.1,0.5"}) # Single black region as a horizontal range marker
|
86
92
|
|
87
93
|
# Bryan Error condition
|
88
94
|
lcxy = GoogleChart::LineChart.new('320x200', "Line XY Chart", true)
|
@@ -90,5 +96,6 @@ lcxy.data 'A', [[0, 32], [1, 15], [2, 23], [3, 18], [4, 41], [5, 53]],'0000ff'
|
|
90
96
|
lcxy.data 'B', [[0, 73], [1, 0], [2, 28], [3, 0], [4, 333], [5, 0]], '00ff00'
|
91
97
|
lcxy.data 'C', [[0, 22], [1, 26], [2, 14], [3, 33], [4, 17], [5, 7]], 'ff0000'
|
92
98
|
lcxy.data 'D', [[0, 4], [1, 39], [2, 0], [3, 5], [4, 11], [5, 14]], 'cc00ff'
|
99
|
+
puts "\nBryan Error Condition"
|
93
100
|
puts lcxy.to_url
|
94
101
|
|
metadata
CHANGED
@@ -3,7 +3,7 @@ rubygems_version: 0.9.4
|
|
3
3
|
specification_version: 1
|
4
4
|
name: gchartrb
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version:
|
6
|
+
version: 0.5.1
|
7
7
|
date: 2007-12-12 00:00:00 +08:00
|
8
8
|
summary: Ruby Wrapper for the Google Chart API
|
9
9
|
require_paths:
|
@@ -35,6 +35,7 @@ files:
|
|
35
35
|
- README.txt
|
36
36
|
- Rakefile
|
37
37
|
- TODO
|
38
|
+
- lib/example.rb
|
38
39
|
- lib/google_chart.rb
|
39
40
|
- lib/google_chart/bar_chart.rb
|
40
41
|
- lib/google_chart/base.rb
|