gchartrb 0.1.0

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/CREDITS ADDED
@@ -0,0 +1 @@
1
+ Will Fitzgerald - http://www.entish.org/willwhim
@@ -0,0 +1,5 @@
1
+ == 0.1 / 2007-12-11
2
+
3
+ * Initial Release
4
+ * RDoc documentation
5
+
@@ -0,0 +1,13 @@
1
+ CREDITS
2
+ History.txt
3
+ Manifest.txt
4
+ README.txt
5
+ Rakefile
6
+ TODO
7
+ lib/google_chart.rb
8
+ lib/google_chart/bar_chart.rb
9
+ lib/google_chart/base.rb
10
+ lib/google_chart/line_chart.rb
11
+ lib/google_chart/pie_chart.rb
12
+ lib/google_chart/venn_diagram.rb
13
+ lib/test.rb
@@ -0,0 +1,138 @@
1
+ gchartrb
2
+ by Deepak Jois
3
+ http://code.google.com/p/gchartrb
4
+
5
+ == DESCRIPTION:
6
+
7
+ gchartrb is a Ruby wrapper around the Google Chart API, located at http://code.google.com/apis/chart/
8
+
9
+ Visit http://code.google.com/p/gchartrb to track development regarding gchartrb.
10
+
11
+ == FEATURES:
12
+
13
+ * Provides an object oriented interface in Ruby to create Google Chart URLs for charts.
14
+
15
+ == INSTALL:
16
+
17
+ === Ruby Gem:
18
+
19
+ sudo gem install gchartrb
20
+
21
+ === Source Code:
22
+
23
+ Download the latest release from http://code.google.com/p/gchartrb/downloads/list
24
+
25
+ === Subversion
26
+
27
+ svn checkout http://gchartrb.googlecode.com/svn/trunk/ gchartrb-read-only
28
+
29
+ == Problems/TODO
30
+ The following features are pending :
31
+
32
+ * Scatter Charts
33
+ * Horizontal and Vertical range Markers
34
+ * Line Styles
35
+ * Shape Markers
36
+ * Fill Area
37
+
38
+ However, you can still make use of the API to insert arbitrary parameters
39
+
40
+ # Adding Extra params
41
+ lc = GoogleChart::LineChart.new('320x200', "Line Chart", false)
42
+ lc.data "Trend 1", [5,4,3,1,3,5,6], '0000ff'
43
+ lc.data "Trend 2", [1,2,3,4,5,6], '00ff00'
44
+ lc.data "Trend 3", [6,5,4,3,2,1], 'ff0000'
45
+ puts lc.to_url({:chm => "r,000000,0,0.1,0.5"}) # Single black line as a horizontal marker by inserting arbitrary param
46
+
47
+ == SYNOPSIS:
48
+
49
+ require 'rubygems'
50
+ require 'google_chart'
51
+
52
+ # Pie Chart
53
+ pc = GoogleChart::PieChart.new('320x200', "Pie Chart",false)
54
+ pc.data "Apples", 40
55
+ pc.data "Banana", 20
56
+ pc.data "Peach", 30
57
+ pc.data "Orange", 60
58
+ puts pc.to_url
59
+
60
+ # Line Chart
61
+ lc = GoogleChart::LineChart.new('320x200', "Line Chart", false)
62
+ lc.data "Trend 1", [5,4,3,1,3,5,6], '0000ff'
63
+ lc.show_legend = true
64
+ lc.show_labels = false
65
+ lc.data "Trend 2", [1,2,3,4,5,6], '00ff00'
66
+ lc.data "Trend 3", [6,5,4,3,2,1], 'ff0000'
67
+ lc.axis :y, :range => [0,6], :color => 'ff00ff', :font_size => 16, :alignment => :center
68
+ lc.axis :x, :range => [0,6], :color => '00ffff', :font_size => 16, :alignment => :center
69
+ lc.grid :x_step => 100.0/6.0, :y_step => 100.0/6.0, :length_segment => 1, :length_blank => 0
70
+ puts lc.to_url
71
+
72
+ # Bar Chart
73
+ bc = GoogleChart::BarChart.new('800x200', "Bar Chart", :vertical, false)
74
+ bc.data "Trend 1", [5,4,3,1,3,5], '0000ff'
75
+ bc.data "Trend 2", [1,2,3,4,5,6], 'ff0000'
76
+ bc.data "Trend 3", [6,5,4,4,5,6], '00ff00'
77
+ puts bc.to_url
78
+
79
+ # Line XY Chart
80
+ lcxy = GoogleChart::LineChart.new('320x200', "Line XY Chart", true)
81
+ lcxy.data "Trend 1", [[1,1], [2,2], [3,3], [4,4]], '0000ff'
82
+ lcxy.data "Trend 2", [[4,5], [2,2], [1,1], [3,4]], '00ff00'
83
+ puts lcxy.to_url
84
+
85
+ # Venn Diagram
86
+ # Supply three vd.data statements of label, size, color for circles A, B, C
87
+ # Then, an :intersections with four values:
88
+ # the first value specifies the area of A intersecting B
89
+ # the second value specifies the area of B intersecting C
90
+ # the third value specifies the area of C intersecting A
91
+ # the fourth value specifies the area of A intersecting B intersecting C
92
+ vd = GoogleChart::VennDiagram.new("320x200", 'Venn Diagram')
93
+ vd.data "Blue", 100, '0000ff'
94
+ vd.data "Green", 80, '00ff00'
95
+ vd.data "Red", 60, 'ff0000'
96
+ vd.intersections 30,30,30,10
97
+ puts vd.to_url
98
+
99
+ # Solid fill
100
+ lc.fill(:background, :solid, {:color => 'fff2cc'})
101
+ lc.fill(:chart, :solid, {:color => 'ffcccc'})
102
+ puts lc.to_url
103
+
104
+ # Gradient fill
105
+ lc.fill :background, :gradient, :angle => 0, :color => [['76A4FB',1],['ffffff',0]]
106
+ lc.fill :chart, :gradient, :angle => 0, :color => [['76A4FB',1],
107
+ ['ffffff',0]]
108
+ puts lc.to_url
109
+
110
+ # Stripes Fill
111
+ lc.fill :chart, :stripes, :angle => 90, :color => [['76A4FB',0.2],
112
+ ['ffffff',0.2]]
113
+ puts lc.to_url
114
+
115
+ == LICENSE:
116
+
117
+ (The MIT License)
118
+
119
+ Copyright (c) 2007 Deepak Jois
120
+
121
+ Permission is hereby granted, free of charge, to any person obtaining
122
+ a copy of this software and associated documentation files (the
123
+ 'Software'), to deal in the Software without restriction, including
124
+ without limitation the rights to use, copy, modify, merge, publish,
125
+ distribute, sublicense, and/or sell copies of the Software, and to
126
+ permit persons to whom the Software is furnished to do so, subject to
127
+ the following conditions:
128
+
129
+ The above copyright notice and this permission notice shall be
130
+ included in all copies or substantial portions of the Software.
131
+
132
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
133
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
134
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
135
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
136
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
137
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
138
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,18 @@
1
+ # -*- ruby -*-
2
+
3
+ require 'rubygems'
4
+ require 'hoe'
5
+ require './lib/google_chart.rb'
6
+
7
+ Hoe.new('gchartrb', "0.1.0") do |p|
8
+ p.rubyforge_name = 'gchartrb'
9
+ p.author = 'Deepak Jois'
10
+ p.email = 'deepak.jois@gmail.com'
11
+ p.summary = 'Ruby Wrapper for the Google Chart API'
12
+ p.description = p.paragraphs_of('README.txt', 2..5).join("\n\n")
13
+ p.url = p.paragraphs_of('README.txt', 0).first.split(/\n/)[1..-1]
14
+ p.changes = p.paragraphs_of('History.txt', 0..1).join("\n\n")
15
+ p.remote_rdoc_dir = ''
16
+ end
17
+
18
+ # vim: syntax=Ruby
data/TODO ADDED
@@ -0,0 +1,23 @@
1
+ TODO List (Features)
2
+ ====================
3
+ * Axis labels
4
+ * Grid lines
5
+ * Markers
6
+ * Line Styles
7
+ * Fill Area
8
+ * Handle Missing Values (?)
9
+ * Other charts (Scatter)
10
+ * Download image
11
+
12
+ TODO List (Other)
13
+ =================
14
+ * Create an alternate git repository
15
+
16
+ Code Level Improvements
17
+ =======================
18
+ * 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
+ * Use splats instead of attribute accessors to provide a neater API (?)
21
+ * Improved error handling and reporting for things
22
+ * Number of labels and number of postions in axis must be the same
23
+ * Axis styles need to be specified in a certain way
@@ -0,0 +1,9 @@
1
+ %w(
2
+ base
3
+ pie_chart
4
+ line_chart
5
+ bar_chart
6
+ venn_diagram
7
+ ).each do |filename|
8
+ require File.dirname(__FILE__) + "/google_chart/#{filename}"
9
+ end
@@ -0,0 +1,62 @@
1
+ require File.dirname(__FILE__) + '/base'
2
+ module GoogleChart
3
+ # Generates a Bar Chart. You can specify the alignment(horizontal or vertical) and whether you want the bars to be grouped or stacked
4
+ # ==== Examples
5
+ # bc = GoogleChart::BarChart.new('800x200', "Bar Chart", :vertical, false)
6
+ # bc.data "Trend 1", [5,4,3,1,3,5], '0000ff'
7
+ class BarChart < Base
8
+
9
+ attr_accessor :alignment, :stacked
10
+
11
+ # Specify the
12
+ # * +chart_size+ in WIDTHxHEIGHT format
13
+ # * +chart_title+ as a string
14
+ # * +alignment+ as either <tt>:vertical</tt> or <tt>:horizontal</tt>
15
+ # * +stacked+ should be +true+ if you want the bars to be stacked, false otherwise
16
+ def initialize(chart_size='300x200', chart_title=nil, alignment=:vertical, stacked=false)
17
+ super(chart_size, chart_title)
18
+ @alignment = alignment
19
+ @stacked = stacked
20
+ set_chart_type
21
+ self.show_labels = false
22
+ self.show_legend = true
23
+ end
24
+
25
+ # Set the alignment to either <tt>:vertical</tt> or <tt>:horizontal</tt>
26
+ def alignment=(value)
27
+ @alignment = value
28
+ set_chart_type
29
+ end
30
+
31
+ # If you want the bar chart to be stacked, set the value to <tt>true</tt>, otherwise set the value to <tt>false</tt> to group it.
32
+ def stacked=(stacked)
33
+ @stacked = value
34
+ set_chart_type
35
+ end
36
+
37
+ def process_data
38
+ if @data.size > 1
39
+ max_value = @data.flatten.max
40
+ join_encoded_data(@data.collect { |series|
41
+ encode_data(series, max_value)
42
+ })
43
+ else
44
+ encode_data(@data.flatten)
45
+ end
46
+ end
47
+
48
+ private
49
+ def set_chart_type
50
+ # Set chart type
51
+ if alignment == :vertical and stacked == false
52
+ self.chart_type = :bvg
53
+ elsif alignment == :vertical and stacked == true
54
+ self.chart_type = :bvs
55
+ elsif alignment == :horizontal and stacked == false
56
+ self.chart_type = :bhg
57
+ elsif alignment == :horizontal and stacked == true
58
+ self.chart_type = :bhs
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,304 @@
1
+ require 'uri'
2
+
3
+ module GoogleChart
4
+ class Base
5
+ BASE_URL = "http://chart.apis.google.com/chart?"
6
+
7
+ SIMPLE_ENCODING = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'.split('');
8
+ COMPLEX_ENCODING_ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-.'.split('');
9
+ @@complex_encoding = []
10
+ COMPLEX_ENCODING_ALPHABET.each_with_index do |outer,index_outer|
11
+ COMPLEX_ENCODING_ALPHABET.each_with_index do |inner, index_inner|
12
+ @@complex_encoding[index_outer * 64 + index_inner] = outer + inner
13
+ end
14
+ end
15
+
16
+ attr_accessor :chart_size, :chart_type, :chart_title, :data_encoding, :params, :show_legend, :show_labels
17
+
18
+ def initialize(chart_size, chart_title)
19
+ self.params = Hash.new
20
+ @labels = []
21
+ @data = []
22
+ @colors = []
23
+ @axis = []
24
+ self.chart_size = chart_size
25
+ self.chart_title = chart_title
26
+ self.data_encoding = :simple
27
+ self.show_legend = true
28
+ self.show_labels = false
29
+ end
30
+
31
+ # Generates the URL string that can be used to retrieve the graph image in PNG format.
32
+ # Use this after assigning all the properties to the graph
33
+ # You can pass in additional params as a hash for features that may not have been implemented
34
+ # For e.g
35
+ # lc = GoogleChart::LineChart.new('320x200', "Line Chart", false)
36
+ # lc.data "Trend 1", [5,4,3,1,3,5,6], '0000ff'
37
+ # lc.data "Trend 2", [1,2,3,4,5,6], '00ff00'
38
+ # lc.data "Trend 3", [6,5,4,3,2,1], 'ff0000'
39
+ # puts lc.to_url({:chm => "000000,0,0.1,0.11"}) # Single black line as a horizontal marker
40
+ def to_url(extras={})
41
+ params.clear
42
+ set_size
43
+ set_type
44
+ set_colors
45
+ set_fill_options
46
+ add_axis
47
+ add_grid
48
+ add_data
49
+ add_labels(@labels) if show_labels
50
+ add_legend(@labels) if show_legend
51
+ add_title if chart_title.to_s.length > 0
52
+
53
+ params.merge!(extras)
54
+ query_string = params.map { |k,v| "#{k}=#{URI.escape(v.to_s)}" }.join('&')
55
+ BASE_URL + query_string
56
+ end
57
+
58
+ # Adds the data to the chart, according to the type of the graph being generated.
59
+ #
60
+ # [+name+] is a string containing a label for the data.
61
+ # [+value+] is either a number or an array of numbers containing the data. Pie Charts and Venn Diagrams take a single number, but other graphs require an array of numbers
62
+ # [+color+ (optional)] is a hexadecimal RGB value for the color to represent the data
63
+ #
64
+ # ==== Examples
65
+ #
66
+ # for GoogleChart::LineChart (normal)
67
+ # lc.data "Trend 1", [1,2,3,4,5], 'ff00ff'
68
+ #
69
+ # for GoogleChart::LineChart (XY chart)
70
+ # lc.data "Trend 2", [[4,5], [2,2], [1,1], [3,4]], 'ff00ff'
71
+ #
72
+ # for GoogleChart::PieChart
73
+ # lc.data "Apples", 5, 'ff00ff'
74
+ # lc.data "Oranges", 7, '00ffff'
75
+ def data(name, value, color=nil)
76
+ @data << value
77
+ @labels << name
78
+ @colors << color if color
79
+ end
80
+
81
+ def fill(bg_or_c, type, options = {})
82
+ case bg_or_c
83
+ when :background
84
+ @background_fill = "bg," + process_fill_options(type, options)
85
+ when :chart
86
+ @chart_fill = "c," + process_fill_options(type, options)
87
+ end
88
+ end
89
+
90
+ # Adds an axis to the graph. Not applicable for Pie Chart (GoogleChart::PieChart) or Venn Diagram (GoogleChart::VennDiagram)
91
+ #
92
+ # [+type+] is a symbol which can be one of <tt>:x</tt>, <tt>:y</tt>, <tt>:right</tt>, <tt>:top</tt>
93
+ # [+options+] is a hash containing the options (see below)
94
+ #
95
+ # ==== Options
96
+ # Not all the options are mandatory.
97
+ # [<tt>:labels</tt>] An array containing the labels for the axis
98
+ # [<tt>:position</tt>] An Array containing the positions for the labels
99
+ # [<tt>:range</tt>] An array containing 2 elements, the start value and end value
100
+ #
101
+ # axis styling options have to be specified as follows
102
+ # [<tt>:color</tt>] Hexadecimal RGB value for the color to represent the data for the axis labels
103
+ # [<tt>:font_size</tt>] Font size of the labels in pixels
104
+ # [<tt>:alignment</tt>] can be one of <tt>:left</tt>, <tt>:center</tt> or <tt>:right</tt>
105
+ #
106
+ # ==== Examples
107
+ # lc.axis :y, :range => [0,6], :color => 'ff00ff', :font_size => 16, :alignment => :center
108
+ #
109
+ def axis(type, options = {})
110
+ raise "Illegal axis type" unless [:x, :y, :right, :top].member?(type)
111
+ @axis << [type, options]
112
+ end
113
+
114
+ # Adds a grid to the graph. Applicable only for Line Chart (GoogleChart::LineChart) and Scatter Chart (available soon)
115
+ #
116
+ # [+options+] is a hash containing the options (see below)
117
+ #
118
+ # === Options
119
+ # [<tt>:xstep</tt>] X axis step size
120
+ # [<tt>:ystep</tt>] Y axis step size
121
+ # [<tt>:length_segment</tt> (optional)] Length of the line segement. Useful with the :length_blank value to have dashed lines
122
+ # [<tt>:length_blank</tt> (optional)] Length of the blank segment. use 0 if you want a solid grid
123
+ #
124
+ # === Examples
125
+ # lc.grid :x_step => 5, :y_step => 5, :length_segment => 1, :length_blank => 0
126
+ #
127
+
128
+ def grid(options={})
129
+ @grid_str = "#{options[:x_step].to_f},#{options[:y_step].to_f}"
130
+ if options[:length_segment] or options[:length_blank]
131
+ @grid_str += ",#{options[:length_segment].to_f},#{options[:length_blank].to_f}"
132
+ end
133
+ end
134
+
135
+ protected
136
+
137
+ def process_fill_options(type, options)
138
+ case type
139
+ when :solid
140
+ "s,#{options[:color]}"
141
+ when :gradient
142
+ "lg,#{options[:angle]}," + options[:color].collect { |o| "#{o.first},#{o.last}" }.join(",")
143
+ when :stripes
144
+ "ls,#{options[:angle]}," + options[:color].collect { |o| "#{o.first},#{o.last}" }.join(",")
145
+ end
146
+
147
+ end
148
+
149
+ def set_type
150
+ params.merge!({:cht => chart_type})
151
+ end
152
+
153
+ def set_size
154
+ params.merge!({:chs => chart_size})
155
+ end
156
+
157
+ def set_colors
158
+ params.merge!({:chco => @colors.collect{|c| c.downcase}.join(",") }) if @colors.size > 0
159
+ end
160
+
161
+ def set_fill_options
162
+ fill_opt = [@background_fill, @chart_fill].compact.join("|")
163
+ params.merge!({:chf => fill_opt}) if fill_opt.length > 0
164
+ end
165
+
166
+ def add_labels(labels)
167
+ params.merge!({:chl => labels.collect{|l| l.to_s}.join("|") })
168
+ end
169
+
170
+ def add_legend(labels)
171
+ params.merge!({:chdl => labels.collect{ |l| l.to_s}.join("|")})
172
+ end
173
+
174
+ def add_title
175
+ params.merge!({:chtt => chart_title})
176
+ end
177
+
178
+ def add_axis
179
+ chxt = []
180
+ chxl = []
181
+ chxp = []
182
+ chxr = []
183
+ chxs = []
184
+ # Process params
185
+ @axis.each_with_index do |axis, idx|
186
+ # Find axis type
187
+ case axis.first
188
+ when :x
189
+ chxt << "x"
190
+ when :y
191
+ chxt << "y"
192
+ when :top
193
+ chxt << "r"
194
+ when :right
195
+ chxt << "t"
196
+ end
197
+
198
+ # Axis labels
199
+ axis_opts = axis.last
200
+
201
+ if axis_opts[:labels]
202
+ chxl[idx] = "#{idx}:|" + axis_opts[:labels].join("|")
203
+ end
204
+
205
+ # Axis positions
206
+ if axis_opts[:positions]
207
+ chxp[idx] = "#{idx}," + axis_opts[:positions].join(",")
208
+ end
209
+
210
+ # Axis range
211
+ if axis_opts[:range]
212
+ chxr[idx] = "#{idx},#{axis_opts[:range].first},#{axis_opts[:range].last}"
213
+ end
214
+
215
+ # Axis Styles
216
+ if axis_opts[:color] or axis_opts[:font_size] or axis_opts[:alignment]
217
+ if axis_opts[:alignment]
218
+ alignment = case axis_opts[:alignment]
219
+ when :center
220
+ 0
221
+ when :left
222
+ -1
223
+ when :right
224
+ 1
225
+ else
226
+ nil
227
+ end
228
+ end
229
+ chxs[idx] = "#{idx}," + [axis_opts[:color], axis_opts[:font_size], alignment].compact.join(",")
230
+ end
231
+ end
232
+
233
+ # Add to params hash
234
+ params.merge!({ :chxt => chxt.join(",") }) unless chxt.empty?
235
+ params.merge!({ :chxl => chxl.compact.join("|") }) unless chxl.compact.empty?
236
+ params.merge!({ :chxp => chxp.compact.join("|") }) unless chxp.compact.empty?
237
+ params.merge!({ :chxr => chxr.compact.join("|") }) unless chxr.compact.empty?
238
+ params.merge!({ :chxs => chxs.compact.join("|") }) unless chxs.compact.empty?
239
+ end
240
+
241
+ def add_grid
242
+ params.merge!({ :chg => @grid_str }) if @grid_str
243
+ end
244
+
245
+ def add_data
246
+ converted_data = process_data
247
+ case data_encoding
248
+ when :simple
249
+ converted_data = "s:" + converted_data
250
+ when :text
251
+ converted_data = "t:" + converted_data
252
+ when :extended
253
+ converted_data = "e:" + converted_data
254
+ else
255
+ raise "Illegal Encoding Specified"
256
+ end
257
+ params.merge!({:chd => converted_data})
258
+ end
259
+
260
+ def encode_data(values, max_value=nil)
261
+ case data_encoding
262
+ when :simple
263
+ simple_encode(values)
264
+ when :text
265
+ text_encode(values)
266
+ when :extended
267
+ extended_encode(values)
268
+ else
269
+ raise "Illegal Encoding Specified"
270
+ end
271
+ end
272
+
273
+ def simple_encode(values, max_value=nil)
274
+ alphabet_length = 61
275
+ max_value = values.max unless max_value
276
+
277
+ chart_data = values.collect do |val|
278
+ if val.to_i >=0
279
+ SIMPLE_ENCODING[(alphabet_length * val / max_value).to_i]
280
+ else
281
+ "_"
282
+ end
283
+ end
284
+
285
+ return chart_data.join('')
286
+ end
287
+
288
+ def text_encode(values, max_value=nil)
289
+ max_value = values.max unless max_value
290
+ values.inject("") { |sum, v|
291
+ sum += ( "%.1f" % (v*100/max_value) ) + ","
292
+ }.chomp(",")
293
+ end
294
+
295
+ def extended_encode(values, max_value)
296
+ max_value = values.max unless max_value
297
+ values.collect { |v| @@complex_encoding[(v * 4095/max_value).to_i]}.join('')
298
+ end
299
+
300
+ def join_encoded_data(encoded_data)
301
+ encoded_data.join((self.data_encoding == :simple) ? "," : "|")
302
+ end
303
+ end
304
+ end
@@ -0,0 +1,73 @@
1
+ require File.dirname(__FILE__) + '/base'
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
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,26 @@
1
+ require File.dirname(__FILE__) + '/base'
2
+ module GoogleChart
3
+ class PieChart < Base
4
+
5
+ attr_accessor :is_3d
6
+
7
+ # Initializes a Pie Chart object with a +chart_size+ and +chart_title+. Specify <tt>is_3d</tt> as +true+ to generate a 3D Pie chart
8
+ def initialize(chart_size='300x200', chart_title=nil, is_3d = false)
9
+ super(chart_size, chart_title)
10
+ self.is_3d = is_3d
11
+ end
12
+
13
+ # Set this value to <tt>true</tt> if you want the Pie Chart to be rendered as a 3D image
14
+ def is_3d=(value)
15
+ if value
16
+ self.chart_type = :p3
17
+ else
18
+ self.chart_type = :p
19
+ end
20
+ end
21
+
22
+ def process_data
23
+ encode_data(@data)
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,35 @@
1
+ require File.dirname(__FILE__) + '/base'
2
+ module GoogleChart
3
+ # Generates a Venn Diagram.
4
+ #
5
+ # Supply three vd.data statements of label, size, color for circles A, B, C. Then, intersections with four values:
6
+ # * the first value specifies the area of A intersecting B
7
+ # * the second value specifies the area of B intersecting C
8
+ # * the third value specifies the area of C intersecting A
9
+ # * the fourth value specifies the area of A intersecting B intersecting C
10
+ #
11
+ # vd = GoogleChart::VennDiagram.new("320x200", 'Venn Diagram')
12
+ # vd.data "Blue", 100, '0000ff'
13
+ # vd.data "Green", 80, '00ff00'
14
+ # vd.data "Red", 60, 'ff0000'
15
+ # vd.intersections 30,30,30,10
16
+ # puts vd.to_url
17
+ class VennDiagram < Base
18
+
19
+ # Initializes the Venn Diagram with a +chart_size+ and a +chart_title+
20
+ def initialize(chart_size='300x200', chart_title=nil)
21
+ super(chart_size, chart_title)
22
+ self.chart_type = :v
23
+ @intersections = []
24
+ end
25
+
26
+ def process_data
27
+ encode_data(@data + @intersections)
28
+ end
29
+
30
+ # Specify the intersections of the circles in the Venn Diagram. See the Rdoc for class for sample
31
+ def intersections(*values)
32
+ @intersections = values
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,72 @@
1
+ require 'google_chart'
2
+
3
+ # Pie Chart
4
+ pc = GoogleChart::PieChart.new('320x200', "Pie Chart",false)
5
+ pc.data "Apples", 40
6
+ pc.data "Banana", 20
7
+ pc.data "Peach", 30
8
+ pc.data "Orange", 60
9
+ puts pc.to_url
10
+
11
+ # Line Chart
12
+ lc = GoogleChart::LineChart.new('320x200', "Line Chart", false)
13
+ lc.data "Trend 1", [5,4,3,1,3,5,6], '0000ff'
14
+ lc.show_legend = true
15
+ lc.show_labels = false
16
+ lc.data "Trend 2", [1,2,3,4,5,6], '00ff00'
17
+ lc.data "Trend 3", [6,5,4,3,2,1], 'ff0000'
18
+ lc.axis :y, :range => [0,6], :color => 'ff00ff', :font_size => 16, :alignment => :center
19
+ lc.axis :x, :range => [0,6], :color => '00ffff', :font_size => 16, :alignment => :center
20
+ lc.grid :x_step => 100.0/6.0, :y_step => 100.0/6.0, :length_segment => 1, :length_blank => 0
21
+ puts lc.to_url
22
+
23
+ # Bar Chart
24
+ bc = GoogleChart::BarChart.new('800x200', "Bar Chart", :vertical, false)
25
+ bc.data "Trend 1", [5,4,3,1,3,5], '0000ff'
26
+ bc.data "Trend 2", [1,2,3,4,5,6], 'ff0000'
27
+ bc.data "Trend 3", [6,5,4,4,5,6], '00ff00'
28
+ puts bc.to_url
29
+
30
+ # Line XY Chart
31
+ lcxy = GoogleChart::LineChart.new('320x200', "Line XY Chart", true)
32
+ lcxy.data "Trend 1", [[1,1], [2,2], [3,3], [4,4]], '0000ff'
33
+ lcxy.data "Trend 2", [[4,5], [2,2], [1,1], [3,4]], '00ff00'
34
+ puts lcxy.to_url
35
+
36
+ # Venn Diagram
37
+ # Supply three vd.data statements of label, size, color for circles A, B, C
38
+ # Then, an :intersections with four values:
39
+ # the first value specifies the area of A intersecting B
40
+ # the second value specifies the area of B intersecting C
41
+ # the third value specifies the area of C intersecting A
42
+ # the fourth value specifies the area of A intersecting B intersecting C
43
+ vd = GoogleChart::VennDiagram.new("320x200", 'Venn Diagram')
44
+ vd.data "Blue", 100, '0000ff'
45
+ vd.data "Green", 80, '00ff00'
46
+ vd.data "Red", 60, 'ff0000'
47
+ vd.intersections 30,30,30,10
48
+ puts vd.to_url
49
+
50
+ # Solid fill
51
+ lc.fill(:background, :solid, {:color => 'fff2cc'})
52
+ lc.fill(:chart, :solid, {:color => 'ffcccc'})
53
+ puts lc.to_url
54
+
55
+ # Gradient fill
56
+ lc.fill :background, :gradient, :angle => 0, :color => [['76A4FB',1],['ffffff',0]]
57
+ lc.fill :chart, :gradient, :angle => 0, :color => [['76A4FB',1],
58
+ ['ffffff',0]]
59
+ puts lc.to_url
60
+
61
+ # Stripes Fill
62
+ lc.fill :chart, :stripes, :angle => 90, :color => [['76A4FB',0.2],
63
+ ['ffffff',0.2]]
64
+ puts lc.to_url
65
+
66
+ # Adding Extra params
67
+ lc = GoogleChart::LineChart.new('320x200', "Line Chart", false)
68
+ lc.data "Trend 1", [5,4,3,1,3,5,6], '0000ff'
69
+ lc.data "Trend 2", [1,2,3,4,5,6], '00ff00'
70
+ lc.data "Trend 3", [6,5,4,3,2,1], 'ff0000'
71
+ puts lc.to_url({:chm => "r,000000,0,0.1,0.5"}) # Single black line as a horizontal marker
72
+
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.9.4
3
+ specification_version: 1
4
+ name: gchartrb
5
+ version: !ruby/object:Gem::Version
6
+ version: 0.1.0
7
+ date: 2007-12-11 00:00:00 +08:00
8
+ summary: Ruby Wrapper for the Google Chart API
9
+ require_paths:
10
+ - lib
11
+ email: deepak.jois@gmail.com
12
+ homepage: " by Deepak Jois"
13
+ rubyforge_project: gchartrb
14
+ description: "Visit http://code.google.com/p/gchartrb to track development regarding gchartrb. == FEATURES: * Provides an object oriented interface in Ruby to create Google Chart URLs for charts. == INSTALL: === Ruby Gem:"
15
+ autorequire:
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: true
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ - - ">"
22
+ - !ruby/object:Gem::Version
23
+ version: 0.0.0
24
+ version:
25
+ platform: ruby
26
+ signing_key:
27
+ cert_chain:
28
+ post_install_message:
29
+ authors:
30
+ - Deepak Jois
31
+ files:
32
+ - CREDITS
33
+ - History.txt
34
+ - Manifest.txt
35
+ - README.txt
36
+ - Rakefile
37
+ - TODO
38
+ - lib/google_chart.rb
39
+ - lib/google_chart/bar_chart.rb
40
+ - lib/google_chart/base.rb
41
+ - lib/google_chart/line_chart.rb
42
+ - lib/google_chart/pie_chart.rb
43
+ - lib/google_chart/venn_diagram.rb
44
+ - lib/test.rb
45
+ test_files: []
46
+
47
+ rdoc_options:
48
+ - --main
49
+ - README.txt
50
+ extra_rdoc_files:
51
+ - History.txt
52
+ - Manifest.txt
53
+ - README.txt
54
+ executables: []
55
+
56
+ extensions: []
57
+
58
+ requirements: []
59
+
60
+ dependencies:
61
+ - !ruby/object:Gem::Dependency
62
+ name: hoe
63
+ version_requirement:
64
+ version_requirements: !ruby/object:Gem::Version::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: 1.3.0
69
+ version: