gchartrb 0.5.5 → 0.6
Sign up to get free protection for your applications and to get access to all the features.
- data/CREDITS +2 -1
- data/History.txt +6 -0
- data/Manifest.txt +1 -0
- data/Rakefile +1 -1
- data/TODO +6 -5
- data/lib/google_chart.rb +2 -1
- data/lib/google_chart/bar_chart.rb +83 -69
- data/lib/google_chart/base.rb +16 -0
- data/lib/google_chart/financial_line_chart.rb +31 -0
- data/lib/google_chart/line_chart.rb +16 -2
- data/lib/test.rb +19 -0
- metadata +3 -2
data/CREDITS
CHANGED
data/History.txt
CHANGED
@@ -1,3 +1,9 @@
|
|
1
|
+
== 0.6 / 2008-01-27
|
2
|
+
|
3
|
+
* Added experimental support for undocumented Financial Line Chart (lfi)
|
4
|
+
* Added line style support (thanks diomedes)
|
5
|
+
* Added support for customisable width and spacing in bar charts
|
6
|
+
|
1
7
|
== 0.5.5 / 2008-01-23
|
2
8
|
|
3
9
|
* Fixed a bug related to multiple datasets using extended encoding
|
data/Manifest.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.
|
11
|
+
Hoe.new('gchartrb', "0.6") 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,11 +1,12 @@
|
|
1
1
|
TODO List (Features)
|
2
2
|
====================
|
3
|
-
* Line Styles
|
3
|
+
* Line Styles DONE
|
4
|
+
* Customisable Width for bar charts DONE
|
4
5
|
* Fill Area
|
5
|
-
*
|
6
|
-
|
7
|
-
* Friendly
|
8
|
-
*
|
6
|
+
* Download Image
|
7
|
+
|
8
|
+
* Friendly Color Values
|
9
|
+
* Easy calculations for Stuff that is needed
|
9
10
|
|
10
11
|
TODO List (Other)
|
11
12
|
=================
|
data/lib/google_chart.rb
CHANGED
@@ -1,76 +1,90 @@
|
|
1
1
|
require File.dirname(__FILE__) + '/base'
|
2
2
|
module GoogleChart
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
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) # :yield: self
|
17
|
+
super(chart_size, chart_title)
|
18
|
+
@alignment = alignment
|
19
|
+
@stacked = stacked
|
20
|
+
set_chart_type
|
21
|
+
self.show_legend = true
|
22
|
+
yield self if block_given?
|
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
36
|
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
end
|
51
|
-
end
|
37
|
+
# Defines options for bar width, spacing between bars and between groups of bars. Applicable for bar charts.
|
38
|
+
# [+options+] : Options for the style, specifying things like line thickness and lengths of the line segment and blank portions
|
39
|
+
#
|
40
|
+
# ==== Options
|
41
|
+
# * <tt>:bar_width</tt>, Bar width in pixels
|
42
|
+
# * <tt>:bar_spacing</tt> (optional), space between bars in a group
|
43
|
+
# * <tt>:group_spacing</tt> (optional), space between groups
|
44
|
+
def width_spacing_options(options={})
|
45
|
+
options_str = "#{options[:bar_width]}"
|
46
|
+
options_str += ",#{options[:bar_spacing]}" if options[:bar_spacing]
|
47
|
+
options_str += ",#{options[:group_spacing]}" if options[:bar_spacing] and options[:group_spacing]
|
48
|
+
@bar_width_spacing_options = options_str
|
49
|
+
end
|
52
50
|
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
51
|
+
def process_data
|
52
|
+
if @stacked # Special handling of max value for stacked
|
53
|
+
unless @max_data # Unless max_data is explicitly set
|
54
|
+
@max_data = @data.inject([]) do |sum_arr, series|
|
55
|
+
series.each_with_index do |v,i|
|
56
|
+
if sum_arr[i] == nil
|
57
|
+
sum_arr[i] = v
|
58
|
+
else
|
59
|
+
sum_arr[i] += v
|
60
|
+
end
|
61
|
+
end
|
62
|
+
sum_arr
|
63
|
+
end.max
|
64
|
+
end
|
60
65
|
end
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
self.chart_type = :bvs
|
69
|
-
elsif alignment == :horizontal and stacked == false
|
70
|
-
self.chart_type = :bhg
|
71
|
-
elsif alignment == :horizontal and stacked == true
|
72
|
-
self.chart_type = :bhs
|
73
|
-
end
|
66
|
+
|
67
|
+
if @data.size > 1
|
68
|
+
join_encoded_data(@data.collect { |series|
|
69
|
+
encode_data(series, max_data_value)
|
70
|
+
})
|
71
|
+
else
|
72
|
+
encode_data(@data.flatten,max_data_value)
|
74
73
|
end
|
75
74
|
end
|
76
|
-
|
75
|
+
|
76
|
+
private
|
77
|
+
def set_chart_type
|
78
|
+
# Set chart type
|
79
|
+
if alignment == :vertical and stacked == false
|
80
|
+
self.chart_type = :bvg
|
81
|
+
elsif alignment == :vertical and stacked == true
|
82
|
+
self.chart_type = :bvs
|
83
|
+
elsif alignment == :horizontal and stacked == false
|
84
|
+
self.chart_type = :bhg
|
85
|
+
elsif alignment == :horizontal and stacked == true
|
86
|
+
self.chart_type = :bhs
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
data/lib/google_chart/base.rb
CHANGED
@@ -24,6 +24,8 @@ module GoogleChart
|
|
24
24
|
:x => "x"
|
25
25
|
}
|
26
26
|
|
27
|
+
DEFAULT_LINE_STYLE = '1'
|
28
|
+
|
27
29
|
# Size of the chart in WIDTHxHEIGHT format
|
28
30
|
attr_accessor :chart_size
|
29
31
|
|
@@ -55,6 +57,7 @@ module GoogleChart
|
|
55
57
|
@colors = []
|
56
58
|
@axis = []
|
57
59
|
@markers = []
|
60
|
+
@line_styles = []
|
58
61
|
self.chart_size = chart_size
|
59
62
|
self.chart_title = chart_title
|
60
63
|
self.data_encoding = :simple
|
@@ -79,6 +82,8 @@ module GoogleChart
|
|
79
82
|
add_axis unless @axis.empty?
|
80
83
|
add_grid
|
81
84
|
add_data
|
85
|
+
add_line_styles unless @line_styles.empty?
|
86
|
+
set_bar_width_spacing_options if @bar_width_spacing_options
|
82
87
|
add_markers unless @markers.empty?
|
83
88
|
add_labels(@labels) if [:p, :p3].member?(self.chart_type)
|
84
89
|
add_legend(@labels) if show_legend
|
@@ -355,6 +360,17 @@ module GoogleChart
|
|
355
360
|
def add_grid
|
356
361
|
params.merge!({ :chg => @grid_str }) if @grid_str
|
357
362
|
end
|
363
|
+
|
364
|
+
def add_line_styles
|
365
|
+
0.upto(@line_styles.length - 1) { |i|
|
366
|
+
@line_styles[i] = DEFAULT_LINE_STYLE unless @line_styles[i]
|
367
|
+
}
|
368
|
+
params.merge!({:chls => @line_styles.join("|")})
|
369
|
+
end
|
370
|
+
|
371
|
+
def set_bar_width_spacing_options
|
372
|
+
params.merge!({:chbh => @bar_width_spacing_options})
|
373
|
+
end
|
358
374
|
|
359
375
|
def add_markers
|
360
376
|
params.merge!({:chm => @markers.join("|")})
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/base'
|
2
|
+
module GoogleChart
|
3
|
+
|
4
|
+
# Generates a Financial Line Chart. This feature is UNDOCUMENTED and EXPERIMENTAL.
|
5
|
+
# For a sample usage, visit (right at the bottom) http://24ways.org/2007/tracking-christmas-cheer-with-google-charts
|
6
|
+
#
|
7
|
+
# ==== Examples
|
8
|
+
# flc = GoogleChart::FinancialLineChart.new do |chart|
|
9
|
+
# chart.data "", [3,10,20,37,40,25,68,75,89,99], "ff0000"
|
10
|
+
# end
|
11
|
+
# puts flc.to_url
|
12
|
+
#
|
13
|
+
class FinancialLineChart < Base
|
14
|
+
|
15
|
+
# Specify the
|
16
|
+
# * +chart_size+ in WIDTHxHEIGHT format
|
17
|
+
# * +chart_title+ as a string
|
18
|
+
def initialize(chart_size='100x15', chart_title=nil) # :yield: self
|
19
|
+
super(chart_size, chart_title)
|
20
|
+
self.chart_type = :lfi
|
21
|
+
self.show_legend = false
|
22
|
+
yield self if block_given?
|
23
|
+
end
|
24
|
+
|
25
|
+
def process_data
|
26
|
+
join_encoded_data(@data.collect { |series|
|
27
|
+
encode_data(series, max_data_value)
|
28
|
+
})
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -28,6 +28,7 @@ module GoogleChart
|
|
28
28
|
def initialize(chart_size='300x200', chart_title=nil, is_xy=false) # :yield: self
|
29
29
|
super(chart_size, chart_title)
|
30
30
|
self.is_xy = is_xy
|
31
|
+
@line_styles = []
|
31
32
|
yield self if block_given?
|
32
33
|
end
|
33
34
|
|
@@ -43,6 +44,19 @@ module GoogleChart
|
|
43
44
|
end
|
44
45
|
end
|
45
46
|
|
47
|
+
# Defines a line style. Applicable for line charts.
|
48
|
+
# [+data_set_index+] Can be one of <tt>:background</tt> or <tt>:chart</tt> depending on the kind of fill requested
|
49
|
+
# [+options+] : Options for the style, specifying things like line thickness and lengths of the line segment and blank portions
|
50
|
+
#
|
51
|
+
# ==== Options
|
52
|
+
# * <tt>:line_thickness</tt> (mandatory) option which specifies the thickness of the line segment in pixels
|
53
|
+
# * <tt>:length_segment</tt>, which specifies the length of the line segment
|
54
|
+
# * <tt>:length_blank</tt>, which specifies the lenght of the blank segment
|
55
|
+
def line_style(data_set_index, options={})
|
56
|
+
@line_styles[data_set_index] = "#{options[:line_thickness]}"
|
57
|
+
@line_styles[data_set_index] += ",#{options[:length_segment]},#{options[:length_blank]}" if options[:length_segment]
|
58
|
+
end
|
59
|
+
|
46
60
|
def process_data
|
47
61
|
if self.is_xy or @data.size > 1
|
48
62
|
if self.is_xy # XY Line graph data series
|
@@ -54,8 +68,8 @@ module GoogleChart
|
|
54
68
|
join_encoded_data(encoded_data)
|
55
69
|
else # Line graph multiple data series
|
56
70
|
join_encoded_data(@data.collect { |series|
|
57
|
-
|
58
|
-
|
71
|
+
encode_data(series, max_data_value)
|
72
|
+
})
|
59
73
|
end
|
60
74
|
else
|
61
75
|
encode_data(@data.flatten, max_data_value)
|
data/lib/test.rb
CHANGED
@@ -34,6 +34,7 @@ GoogleChart::BarChart.new('800x200', "Bar Chart", :vertical, false) do |bc|
|
|
34
34
|
bc.data "Trend 1", [5,4,3,1,3,5], '0000ff'
|
35
35
|
bc.data "Trend 2", [1,2,3,4,5,6], 'ff0000'
|
36
36
|
bc.data "Trend 3", [6,5,4,4,5,6], '00ff00'
|
37
|
+
bc.width_spacing_options :bar_width => 5, :bar_spacing => 2, :group_spacing => 10
|
37
38
|
puts "\nBar Chart"
|
38
39
|
puts bc.to_url
|
39
40
|
end
|
@@ -155,3 +156,21 @@ end
|
|
155
156
|
puts "\nBar chart encoding error test"
|
156
157
|
puts bc.to_url
|
157
158
|
|
159
|
+
# Financial Line Chart (Experimental)
|
160
|
+
flc = GoogleChart::FinancialLineChart.new do |chart|
|
161
|
+
chart.data "", [3,10,20,37,40,25,68,75,89,99], "ff0000"
|
162
|
+
end
|
163
|
+
puts "\nFinancial Line Chart or Sparklines (EXPERIMENTAL)"
|
164
|
+
puts flc.to_url
|
165
|
+
|
166
|
+
# Line Style
|
167
|
+
lc = GoogleChart::LineChart.new('320x200', "Line Chart", false) do |lc|
|
168
|
+
lc.data "Trend 1", [5,4,3,1,3,5], '0000ff'
|
169
|
+
lc.data "Trend 2", [1,2,3,4,5,6], '00ff00'
|
170
|
+
lc.data "Trend 3", [6,5,4,3,2,1], 'ff0000'
|
171
|
+
lc.line_style 0, :length_segment => 3, :length_blank => 2, :line_thickness => 3
|
172
|
+
lc.line_style 1, :length_segment => 1, :length_blank => 2, :line_thickness => 1
|
173
|
+
lc.line_style 2, :length_segment => 2, :length_blank => 1, :line_thickness => 5
|
174
|
+
end
|
175
|
+
puts "\nLine Styles"
|
176
|
+
puts lc.to_url
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: gchartrb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: "0.6"
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Deepak Jois
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2008-01-
|
12
|
+
date: 2008-01-27 00:00:00 +08:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
@@ -38,6 +38,7 @@ files:
|
|
38
38
|
- lib/google_chart/pie_chart.rb
|
39
39
|
- lib/google_chart/scatter_chart.rb
|
40
40
|
- lib/google_chart/venn_diagram.rb
|
41
|
+
- lib/google_chart/financial_line_chart.rb
|
41
42
|
- lib/test.rb
|
42
43
|
has_rdoc: true
|
43
44
|
homepage: " by Deepak Jois"
|