ruby-googlechart 0.6.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,21 @@
1
+ # <URL:http://code.google.com/apis/chart/#grid>
2
+ module GoogleChart
3
+ module GridLine
4
+ def self.included(klass)
5
+ klass.register!(:grid)
6
+ end
7
+
8
+ @@grid_line_styles = { :solid => [1,0], :dash => [3,2], :dot => [1,2] }
9
+ @@default_grid_step = 0
10
+
11
+ def grid=(grid)
12
+ grid[:x] ||= @@default_grid_step
13
+ grid[:y] ||= @@default_grid_step
14
+ @grid = [grid[:x], grid[:y], *@@grid_line_styles[grid[:style]]].compact.join(',')
15
+ end
16
+
17
+ def grid
18
+ "chg=#{@grid}" if @grid
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,16 @@
1
+ # <URL:http://code.google.com/apis/chart/#chdl>
2
+ module GoogleChart
3
+ module Legend
4
+ def self.included(klass)
5
+ klass.register!(:legend)
6
+ end
7
+
8
+ def legend=(legend)
9
+ @legend = [legend].flatten.map {|l| CGI::escape(l.to_s) }
10
+ end
11
+
12
+ def legend
13
+ "chdl=#{@legend.join('|')}" if @legend
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,24 @@
1
+ module GoogleChart
2
+ class LineChart < AbstractChart
3
+ @@chart_types = { :line => 'lc', :xy => 'lxy', :sparkline => 'ls' }
4
+
5
+ include Axis
6
+ include Color
7
+ include Data
8
+ include GridLine
9
+ include Legend
10
+ include LineStyle
11
+ include RangeMarker
12
+ include Title
13
+
14
+ def chart_type
15
+ @chart_type ||= @@chart_types[:line]
16
+ 'cht=' + @chart_type if @chart_type
17
+ end
18
+
19
+ def chart_type=(chart_type)
20
+ @chart_type = @@chart_types[chart_type]
21
+ end
22
+ alias_method :type=, :chart_type=
23
+ end
24
+ end
@@ -0,0 +1,36 @@
1
+ # <URL:http://code.google.com/apis/chart/#line_styles>
2
+ module GoogleChart
3
+ module LineStyle
4
+ def self.included(klass)
5
+ klass.register!(:style)
6
+ end
7
+
8
+ @@styles = { :solid => [1,1,0], :dash => [1,3,2], :dot => [1,1,2] }
9
+
10
+ @@default_style = :solid
11
+ @@default_width = 1
12
+
13
+ def style=(style)
14
+ @style = [style].flatten
15
+ end
16
+
17
+ def width=(width)
18
+ @width = [width].flatten
19
+ end
20
+
21
+ def style
22
+ @style ||= []
23
+ @width ||= []
24
+
25
+ # Pad @style and @width with defaults until they are equal in length
26
+ (@style.size - @width.size).times { @width << @@default_width }
27
+ (@width.size - @style.size).times { @style << @@default_style }
28
+
29
+ unless @style.empty?
30
+ 'chls=' + (0...@style.size).map {|i|
31
+ @@styles[@style[i]].map {|n| n * @width[i] }.join(',')
32
+ }.join('|')
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,26 @@
1
+ # <URL:http://code.google.com/apis/chart/#hor_line_marker>
2
+ module GoogleChart
3
+ module RangeMarker
4
+ def self.included(klass)
5
+ klass.register!(:ranges)
6
+ end
7
+
8
+ @@range_marker_orientations = { :h => 'r', :v => 'R' }
9
+
10
+ def ranges=(ranges)
11
+ @ranges = ranges.any? {|e| e.is_a?(Array) } ? ranges : [ranges]
12
+ end
13
+
14
+ def ranges
15
+ unless @ranges.nil? || @ranges.empty?
16
+ 'chm=' + @ranges.collect {|r|
17
+ r[0] = @@range_marker_orientations[r[0]]
18
+ r[1..-1] = ['000000cc', *r[1..-1]] if r.size == 3
19
+ # The middle parameter is required by Google Charts but ignored for range markers
20
+ r = r[0..1] + ['x'] + r[2..-1]
21
+ r.join(',')
22
+ }.join('|')
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,16 @@
1
+ # <URL:http://code.google.com/apis/chart/#chtt>
2
+ module GoogleChart
3
+ module Title
4
+ def self.included(klass)
5
+ klass.register!(:title)
6
+ end
7
+
8
+ def title=(title)
9
+ @title = CGI::escape(title)
10
+ end
11
+
12
+ def title
13
+ "chtt=#{@title}" if @title
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,90 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{ruby-googlechart}
8
+ s.version = "0.6.4"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["John Parker"]
12
+ s.date = %q{2009-11-15}
13
+ s.description = %q{ruby-googlechart is yet another wrapper around the Google Charts API}
14
+ s.email = %q{jparker@urgetopunt.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".gitignore",
21
+ "CHANGELOG",
22
+ "README.rdoc",
23
+ "Rakefile",
24
+ "VERSION",
25
+ "lib/array.rb",
26
+ "lib/google_chart.rb",
27
+ "lib/google_chart/abstract_chart.rb",
28
+ "lib/google_chart/axis.rb",
29
+ "lib/google_chart/bar_chart.rb",
30
+ "lib/google_chart/bar_style.rb",
31
+ "lib/google_chart/color.rb",
32
+ "lib/google_chart/data.rb",
33
+ "lib/google_chart/grid_line.rb",
34
+ "lib/google_chart/legend.rb",
35
+ "lib/google_chart/line_chart.rb",
36
+ "lib/google_chart/line_style.rb",
37
+ "lib/google_chart/range_marker.rb",
38
+ "lib/google_chart/title.rb",
39
+ "ruby-googlechart.gemspec",
40
+ "test/google_chart/test_abstract_chart.rb",
41
+ "test/google_chart/test_axis.rb",
42
+ "test/google_chart/test_bar_chart.rb",
43
+ "test/google_chart/test_bar_style.rb",
44
+ "test/google_chart/test_color.rb",
45
+ "test/google_chart/test_data.rb",
46
+ "test/google_chart/test_grid_line.rb",
47
+ "test/google_chart/test_legend.rb",
48
+ "test/google_chart/test_line_chart.rb",
49
+ "test/google_chart/test_line_style.rb",
50
+ "test/google_chart/test_range_marker.rb",
51
+ "test/google_chart/test_title.rb",
52
+ "test/test_google_chart.rb",
53
+ "test/test_helper.rb"
54
+ ]
55
+ s.homepage = %q{http://github.com/jparker/ruby-googlechart}
56
+ s.rdoc_options = ["--charset=UTF-8"]
57
+ s.require_paths = ["lib"]
58
+ s.rubygems_version = %q{1.3.5}
59
+ s.summary = %q{Ruby wrapper around the Google Charts API}
60
+ s.test_files = [
61
+ "test/google_chart/test_abstract_chart.rb",
62
+ "test/google_chart/test_axis.rb",
63
+ "test/google_chart/test_bar_chart.rb",
64
+ "test/google_chart/test_bar_style.rb",
65
+ "test/google_chart/test_color.rb",
66
+ "test/google_chart/test_data.rb",
67
+ "test/google_chart/test_grid_line.rb",
68
+ "test/google_chart/test_legend.rb",
69
+ "test/google_chart/test_line_chart.rb",
70
+ "test/google_chart/test_line_style.rb",
71
+ "test/google_chart/test_range_marker.rb",
72
+ "test/google_chart/test_title.rb",
73
+ "test/test_google_chart.rb",
74
+ "test/test_helper.rb"
75
+ ]
76
+
77
+ if s.respond_to? :specification_version then
78
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
79
+ s.specification_version = 3
80
+
81
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
82
+ s.add_development_dependency(%q<shoulda>, [">= 0"])
83
+ else
84
+ s.add_dependency(%q<shoulda>, [">= 0"])
85
+ end
86
+ else
87
+ s.add_dependency(%q<shoulda>, [">= 0"])
88
+ end
89
+ end
90
+
@@ -0,0 +1,29 @@
1
+ require File.dirname(__FILE__) + '/../test_helper'
2
+
3
+ class TestAbstractChart < Test::Unit::TestCase
4
+ should 'require descendent classes to define #chart_type method' do
5
+ klass = Class.new(GoogleChart::AbstractChart)
6
+ assert_raise(NoMethodError, /chart_type/) { klass.new.to_url }
7
+
8
+ klass.class_eval { def chart_type() end }
9
+ assert_nothing_raised { klass.new.to_url }
10
+ end
11
+
12
+ should 'allow descendent classes to maintain distinct parameter registries' do
13
+ klass = Class.new(GoogleChart::AbstractChart)
14
+ another_klass = Class.new(GoogleChart::AbstractChart)
15
+ assert_not_same(another_klass.registry, klass.registry)
16
+ end
17
+
18
+ should 'begin URLs with Google Charts base URL' do
19
+ assert_match(%r{\Ahttp://chart\.apis\.google\.com/chart\?}, Class.new(MockChart).new.to_url)
20
+ end
21
+
22
+ should 'have a default chart size' do
23
+ assert_match(/\bchs=600x500\b/, Class.new(MockChart).new.to_url)
24
+ end
25
+
26
+ should 'accept custom chart sizes' do
27
+ assert_match(/\bchs=800x375\b/, Class.new(MockChart).new(:size => '800x375').to_url)
28
+ end
29
+ end
@@ -0,0 +1,74 @@
1
+ require File.dirname(__FILE__) + '/../test_helper'
2
+
3
+ class TestAxis < Test::Unit::TestCase
4
+ def setup
5
+ @klass = Class.new(MockChart).class_eval { include GoogleChart::Axis }
6
+ end
7
+
8
+ should 'not display axis parameter by default' do
9
+ url = @klass.new.to_url
10
+ assert_no_match(/\bchx[lrt]=/, url)
11
+ end
12
+
13
+ context 'axis types' do
14
+ should 'display bottom x-axis' do
15
+ assert_match(/\bchxt=x\b/, @klass.new(:axes => {:x => true}).to_url)
16
+ end
17
+
18
+ should 'display top x-axis' do
19
+ assert_match(/\bchxt=t\b/, @klass.new(:axes => {:t => true}).to_url)
20
+ end
21
+
22
+ should 'display left y-axis' do
23
+ assert_match(/\bchxt=y\b/, @klass.new(:axes => {:y => true}).to_url)
24
+ end
25
+
26
+ should 'display right y-axis' do
27
+ assert_match(/\bchxt=r\b/, @klass.new(:axes => {:r => true}).to_url)
28
+ end
29
+
30
+ should 'display multiple axes' do
31
+ assert_match(/\bchxt=x,y,r\b/, @klass.new(:axes => {:x => true, :y => true, :r => true}).to_url)
32
+ end
33
+ end
34
+
35
+ context 'axis labels' do
36
+ should 'dsiplay enumerated axis labels' do
37
+ assert_match(/\bchxt=x&chxl=0:\|foo\|bar\|baz\b/, @klass.new(:axes => {:x => %w[foo bar baz]}).to_url)
38
+ end
39
+
40
+ should 'display enumerated axis labels for multiple axes' do
41
+ assert_match(/\bchxt=x,y&chxl=0:\|foo\|bar\|baz\|1:\|0\|5\|10\b/, @klass.new(:axes => {:x => %w[foo bar baz], :y => %w[0 5 10]}).to_url)
42
+ end
43
+
44
+ should 'display enumerated axis labels for subset of axes' do
45
+ assert_match(/\bchxt=x,y,r&chxl=0:\|foo\|bar\|2:\|baz\|froz\b/, @klass.new(:axes => {:x => %w[foo bar], :y => true, :r => %w[baz froz]}).to_url)
46
+ end
47
+
48
+ should 'escape axis labels for url' do
49
+ assert_match(/\bchxl=0:\|foo\+bar\|baz%7Cfroz\b/, @klass.new(:axes => {:x => ['foo bar', 'baz|froz']}).to_url)
50
+ end
51
+
52
+ should 'convert non-String labels to Strings' do
53
+ assert_match(/\bchxl=0:\|foo\|\|1\b/, @klass.new(:axes => {:x => ['foo', nil, 1]}).to_url)
54
+ end
55
+ end
56
+
57
+ context 'axis ranges' do
58
+ should 'dsiplay axis label ranges' do
59
+ assert_match(/\bchxt=x&chxr=0,8,16/, @klass.new(:axes => {:x => 8..16}).to_url)
60
+ end
61
+
62
+ should 'display multiple axis label ranges' do
63
+ assert_match(/\bchxt=x,y&chxr=0,6,14\|1,5,23\b/, @klass.new(:axes => {:x => 6..14, :y => 5..23}).to_url)
64
+ end
65
+
66
+ should 'display axis label ranges for subset of axes' do
67
+ assert_match(/\bchxr=0,0,5\|2,0,100\b/, @klass.new(:axes => {:x => 0..5, :y => true, :r => 0..100}).to_url)
68
+ end
69
+ end
70
+
71
+ should 'display multiple types of axis labels' do
72
+ assert_match(/\bchxt=x,y,r&chxl=0:\|foo\|bar\|baz&chxr=2,5,23\b/, @klass.new(:axes => {:x => %w[foo bar baz], :y => true, :r => 5..23}).to_url)
73
+ end
74
+ end
@@ -0,0 +1,126 @@
1
+ require File.dirname(__FILE__) + '/../test_helper'
2
+
3
+ class TestBarChart < Test::Unit::TestCase
4
+ should 'have a default chart type' do
5
+ assert_match(/\bcht=bvg\b/, GoogleChart::BarChart.new.to_url)
6
+ end
7
+
8
+ should 'support horizontal orientation' do
9
+ assert_match(/\bcht=bhg\b/, GoogleChart::BarChart.new(:horizontal => true).to_url)
10
+ end
11
+
12
+ should 'support grouping' do
13
+ assert_match(/\bcht=bvg\b/, GoogleChart::BarChart.new(:grouping => :group).to_url)
14
+ end
15
+
16
+ should 'support stacking' do
17
+ assert_match(/\bcht=bvs\b/, GoogleChart::BarChart.new(:grouping => :stack).to_url)
18
+ end
19
+
20
+ should 'support overlapping as alias for stacking' do
21
+ assert_match(/\bcht=bvs\b/, GoogleChart::BarChart.new(:grouping => :overlap).to_url)
22
+ end
23
+
24
+ should 'support horizontal orientation with grouping' do
25
+ assert_match(/\bcht=bhg\b/, GoogleChart::BarChart.new(:horizontal => true, :grouping => :group).to_url)
26
+ end
27
+
28
+ should 'support horizontal orientation with stacking' do
29
+ assert_match(/\bcht=bhs\b/, GoogleChart::BarChart.new(:horizontal => true, :grouping => :stack).to_url)
30
+ end
31
+
32
+ should 'support horizontal orientation with overlapping' do
33
+ assert_match(/\bcht=bhs\b/, GoogleChart::BarChart.new(:horizontal => true, :grouping => :overlap).to_url)
34
+ end
35
+
36
+ context 'data reduction and scaling' do
37
+ context 'with one data set' do
38
+ setup { @chart = GoogleChart::BarChart.new(:data => [0,2,3]) }
39
+
40
+ should 'scale normally for grouping' do
41
+ @chart.grouping = :group
42
+ assert_match(/\bchd=s:Ao9\b/, @chart.to_url)
43
+ end
44
+
45
+ should 'scale normally for stacking' do
46
+ @chart.grouping = :stack
47
+ assert_match(/\bchd=s:Ao9\b/, @chart.to_url)
48
+ end
49
+
50
+ should 'scale normally for overlapping' do
51
+ @chart.grouping = :overlap
52
+ assert_match(/\bchd=s:Ao9\b/, @chart.to_url)
53
+ end
54
+ end
55
+
56
+ context 'with two data sets' do
57
+ setup { @chart = GoogleChart::BarChart.new(:data => [[0,2,3], [0,4,6]]) }
58
+
59
+ should 'scale normally for grouping' do
60
+ @chart.grouping = :group
61
+ assert_match(/\bchd=s:AUe,Ao9\b/, @chart.to_url)
62
+ end
63
+
64
+ should 'scale based on column sums for stacking' do
65
+ @chart.grouping = :stack
66
+ assert_match(/\bchd=s:ANU,Abo\b/, @chart.to_url)
67
+ end
68
+
69
+ should 'scale based on reduced data for overlapping' do
70
+ @chart.grouping = :overlap
71
+ assert_match(/\bchd=s:AUe,AUe\b/, @chart.to_url)
72
+ end
73
+ end
74
+
75
+ context 'with three data sets' do
76
+ setup { @chart = GoogleChart::BarChart.new(:data => [[0,2,3], [0,4,6], [0,6,9]]) }
77
+
78
+ should 'scale normally for grouping' do
79
+ @chart.grouping = :group
80
+ assert_match(/\bchd=s:ANU,Abo,Ao9\b/, @chart.to_url)
81
+ end
82
+
83
+ should 'scale based on column sums for stacking' do
84
+ @chart.grouping = :stack
85
+ assert_match(/\bchd=s:AGK,ANU,AUe\b/, @chart.to_url)
86
+ end
87
+
88
+ should 'scale based on reduced data for overlapping' do
89
+ @chart.grouping = :overlap
90
+ assert_match(/\bchd=s:ANU,ANU,AAA\b/, @chart.to_url)
91
+ end
92
+ end
93
+ end
94
+
95
+ should 'include Axis module' do
96
+ assert GoogleChart::Axis === GoogleChart::BarChart.new
97
+ end
98
+
99
+ should 'include BarStyle module' do
100
+ assert GoogleChart::BarStyle === GoogleChart::BarChart.new
101
+ end
102
+
103
+ should 'include Color module' do
104
+ assert GoogleChart::Color === GoogleChart::BarChart.new
105
+ end
106
+
107
+ should 'include Data module' do
108
+ assert GoogleChart::Data === GoogleChart::BarChart.new
109
+ end
110
+
111
+ should 'include GridLine module' do
112
+ assert GoogleChart::GridLine === GoogleChart::BarChart.new
113
+ end
114
+
115
+ should 'include Legend module' do
116
+ assert GoogleChart::Legend === GoogleChart::BarChart.new
117
+ end
118
+
119
+ should 'include RangeMarker module' do
120
+ assert GoogleChart::RangeMarker === GoogleChart::BarChart.new
121
+ end
122
+
123
+ should 'include Title module' do
124
+ assert GoogleChart::Title === GoogleChart::BarChart.new
125
+ end
126
+ end