jparker-ruby-googlechart 0.5.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/History.txt +6 -0
- data/Manifest.txt +52 -0
- data/README.txt +65 -0
- data/Rakefile +25 -0
- data/lib/google_chart/axes.rb +40 -0
- data/lib/google_chart/bar.rb +27 -0
- data/lib/google_chart/bar_styles.rb +25 -0
- data/lib/google_chart/base.rb +42 -0
- data/lib/google_chart/colors.rb +14 -0
- data/lib/google_chart/data.rb +91 -0
- data/lib/google_chart/grid_lines.rb +21 -0
- data/lib/google_chart/legends.rb +14 -0
- data/lib/google_chart/line.rb +24 -0
- data/lib/google_chart/line_styles.rb +35 -0
- data/lib/google_chart/range_markers.rb +26 -0
- data/lib/google_chart/sizes.rb +16 -0
- data/lib/google_chart/titles.rb +16 -0
- data/lib/google_chart.rb +29 -0
- data/test/google_chart/test_axes.rb +70 -0
- data/test/google_chart/test_bar.rb +55 -0
- data/test/google_chart/test_bar_styles.rb +23 -0
- data/test/google_chart/test_base.rb +21 -0
- data/test/google_chart/test_colors.rb +21 -0
- data/test/google_chart/test_data.rb +81 -0
- data/test/google_chart/test_grid_lines.rb +35 -0
- data/test/google_chart/test_legends.rb +23 -0
- data/test/google_chart/test_line.rb +55 -0
- data/test/google_chart/test_line_styles.rb +47 -0
- data/test/google_chart/test_range_markers.rb +27 -0
- data/test/google_chart/test_sizes.rb +15 -0
- data/test/google_chart/test_titles.rb +15 -0
- data/test/test_google_chart.rb +97 -0
- data/test/test_helper.rb +11 -0
- metadata +113 -0
data/History.txt
ADDED
data/Manifest.txt
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
History.txt
|
2
|
+
License.txt
|
3
|
+
Manifest.txt
|
4
|
+
PostInstall.txt
|
5
|
+
README.txt
|
6
|
+
Rakefile
|
7
|
+
TODO.txt
|
8
|
+
config/hoe.rb
|
9
|
+
config/requirements.rb
|
10
|
+
lib/google_chart.rb
|
11
|
+
lib/google_chart/axes.rb
|
12
|
+
lib/google_chart/bar.rb
|
13
|
+
lib/google_chart/bar_styles.rb
|
14
|
+
lib/google_chart/base.rb
|
15
|
+
lib/google_chart/colors.rb
|
16
|
+
lib/google_chart/data.rb
|
17
|
+
lib/google_chart/grid_lines.rb
|
18
|
+
lib/google_chart/legends.rb
|
19
|
+
lib/google_chart/line.rb
|
20
|
+
lib/google_chart/line_styles.rb
|
21
|
+
lib/google_chart/range_markers.rb
|
22
|
+
lib/google_chart/sizes.rb
|
23
|
+
lib/google_chart/titles.rb
|
24
|
+
ruby-googlechart.gemspec
|
25
|
+
script/console
|
26
|
+
script/destroy
|
27
|
+
script/generate
|
28
|
+
script/txt2html
|
29
|
+
setup.rb
|
30
|
+
tasks/deployment.rake
|
31
|
+
tasks/environment.rake
|
32
|
+
tasks/website.rake
|
33
|
+
test/google_chart/test_axes.rb
|
34
|
+
test/google_chart/test_bar.rb
|
35
|
+
test/google_chart/test_bar_styles.rb
|
36
|
+
test/google_chart/test_base.rb
|
37
|
+
test/google_chart/test_colors.rb
|
38
|
+
test/google_chart/test_data.rb
|
39
|
+
test/google_chart/test_grid_lines.rb
|
40
|
+
test/google_chart/test_legends.rb
|
41
|
+
test/google_chart/test_line.rb
|
42
|
+
test/google_chart/test_line_styles.rb
|
43
|
+
test/google_chart/test_range_markers.rb
|
44
|
+
test/google_chart/test_sizes.rb
|
45
|
+
test/google_chart/test_titles.rb
|
46
|
+
test/test_google_chart.rb
|
47
|
+
test/test_helper.rb
|
48
|
+
website/index.html
|
49
|
+
website/index.txt
|
50
|
+
website/javascripts/rounded_corners_lite.inc.js
|
51
|
+
website/stylesheets/screen.css
|
52
|
+
website/template.html.erb
|
data/README.txt
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
= ruby-googlechart
|
2
|
+
|
3
|
+
* http://github.com/jparker/ruby-googlechart
|
4
|
+
|
5
|
+
== DESCRIPTION:
|
6
|
+
|
7
|
+
ruby-googlechart is a ruby library which provides object-oriented
|
8
|
+
access to the Google Charts API.
|
9
|
+
|
10
|
+
This library has largely been done as an exercise. These other ruby
|
11
|
+
libraries are more mature and may be better suited to your needs:
|
12
|
+
|
13
|
+
* http://googlecharts.rubyforge.org/
|
14
|
+
* http://code.google.com/p/gchartrb/
|
15
|
+
|
16
|
+
== FEATURES/PROBLEMS:
|
17
|
+
|
18
|
+
* Limited support for Line and Bar charts
|
19
|
+
|
20
|
+
== SYNOPSIS:
|
21
|
+
|
22
|
+
url = GoogleChart.Line(:data => [1, nil, 2, 8, 1])
|
23
|
+
|
24
|
+
url = GoogleChart.Line do |c|
|
25
|
+
c.type = :line
|
26
|
+
c.size = '800x375'
|
27
|
+
c.data = [335, 285, 240, 220, 160, 175, 200, 205]
|
28
|
+
c.scale = 0..400
|
29
|
+
c.encoding = :extended
|
30
|
+
c.title = 'Chart Title'
|
31
|
+
end
|
32
|
+
|
33
|
+
== REQUIREMENTS:
|
34
|
+
|
35
|
+
* newgem >= 1.0.3
|
36
|
+
* hoe >= 1.8.0
|
37
|
+
|
38
|
+
== INSTALL:
|
39
|
+
|
40
|
+
* sudo gem install jparker-ruby-googlechart -s http://gems.github.com/
|
41
|
+
|
42
|
+
== LICENSE:
|
43
|
+
|
44
|
+
(The MIT License)
|
45
|
+
|
46
|
+
Copyright (c) 2008 John Parker
|
47
|
+
|
48
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
49
|
+
a copy of this software and associated documentation files (the
|
50
|
+
'Software'), to deal in the Software without restriction, including
|
51
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
52
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
53
|
+
permit persons to whom the Software is furnished to do so, subject to
|
54
|
+
the following conditions:
|
55
|
+
|
56
|
+
The above copyright notice and this permission notice shall be
|
57
|
+
included in all copies or substantial portions of the Software.
|
58
|
+
|
59
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
60
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
61
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
62
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
63
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
64
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
65
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
%w[rubygems rake rake/clean fileutils newgem rubigen].each { |f| require f }
|
2
|
+
require File.dirname(__FILE__) + '/lib/google_chart'
|
3
|
+
|
4
|
+
# Generate all the Rake tasks
|
5
|
+
# Run 'rake -T' to see list of generated tasks (from gem root directory)
|
6
|
+
$hoe = Hoe.new('ruby-googlechart', GoogleChart::VERSION) do |p|
|
7
|
+
p.developer('John Parker', 'jparker@urgetopunt.com')
|
8
|
+
p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n")
|
9
|
+
p.post_install_message = 'PostInstall.txt' # TODO remove if post-install message not required
|
10
|
+
p.rubyforge_name = p.name # TODO this is default value
|
11
|
+
# p.extra_deps = [
|
12
|
+
# ['activesupport','>= 2.0.2'],
|
13
|
+
# ]
|
14
|
+
p.extra_dev_deps = [
|
15
|
+
['newgem', ">= #{::Newgem::VERSION}"]
|
16
|
+
]
|
17
|
+
|
18
|
+
p.clean_globs |= %w[**/.DS_Store tmp *.log]
|
19
|
+
path = (p.rubyforge_name == p.name) ? p.rubyforge_name : "\#{p.rubyforge_name}/\#{p.name}"
|
20
|
+
p.remote_rdoc_dir = File.join(path.gsub(/^#{p.rubyforge_name}\/?/,''), 'rdoc')
|
21
|
+
p.rsync_args = '-av --delete --ignore-errors'
|
22
|
+
end
|
23
|
+
|
24
|
+
require 'newgem/tasks' # load /tasks/*.rake
|
25
|
+
|
@@ -0,0 +1,40 @@
|
|
1
|
+
# <URL:http://code.google.com/apis/chart/#multiple_axes_labels>
|
2
|
+
module GoogleChart
|
3
|
+
module Axes
|
4
|
+
def self.included(klass)
|
5
|
+
klass.register!(:axes)
|
6
|
+
end
|
7
|
+
|
8
|
+
# TODO: Add support for axis label positions/styles, support for multiple label sets per axis
|
9
|
+
|
10
|
+
def axes
|
11
|
+
unless @axes.nil? || @axes.empty?
|
12
|
+
[
|
13
|
+
'chxt=' + @axes.join(','),
|
14
|
+
@axis_labels.empty? ? nil : 'chxl=' + @axis_labels.join('|'),
|
15
|
+
@axis_ranges.empty? ? nil : 'chxr=' + @axis_ranges.join('|')
|
16
|
+
].compact.join('&')
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def axes=(axes)
|
21
|
+
idx = 0
|
22
|
+
@axes, @axis_labels, @axis_ranges = [], [], []
|
23
|
+
[:x, :y, :r, :t].each do |axis|
|
24
|
+
case axes[axis]
|
25
|
+
when Array
|
26
|
+
@axis_labels << ("#{idx}:|" + axes[axis].collect {|l| CGI::escape(l) }.join('|'))
|
27
|
+
@axes << axis
|
28
|
+
idx += 1
|
29
|
+
when Range
|
30
|
+
@axis_ranges << ("#{idx},#{axes[axis].first},#{axes[axis].last}")
|
31
|
+
@axes << axis
|
32
|
+
idx += 1
|
33
|
+
when true
|
34
|
+
@axes << axis
|
35
|
+
idx += 1
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module GoogleChart
|
2
|
+
class Bar < Base
|
3
|
+
include Axes
|
4
|
+
include BarStyles
|
5
|
+
include Colors
|
6
|
+
include Data
|
7
|
+
include GridLines
|
8
|
+
include Legends
|
9
|
+
include RangeMarkers
|
10
|
+
include Sizes
|
11
|
+
include Titles
|
12
|
+
|
13
|
+
attr_writer :horizontal, :grouped
|
14
|
+
|
15
|
+
def chart_type
|
16
|
+
'cht=b' + (horizontal? ? 'h' : 'v') + (grouped? ? 'g' : 's')
|
17
|
+
end
|
18
|
+
|
19
|
+
def horizontal?
|
20
|
+
@horizontal
|
21
|
+
end
|
22
|
+
|
23
|
+
def grouped?
|
24
|
+
@grouped
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# <URL:http://code.google.com/apis/chart/#bar_width>
|
2
|
+
module GoogleChart
|
3
|
+
module BarStyles
|
4
|
+
def self.included(klass)
|
5
|
+
klass.register!(:style)
|
6
|
+
end
|
7
|
+
|
8
|
+
attr_writer :width
|
9
|
+
|
10
|
+
def style
|
11
|
+
"chbh=#{[@width, *@spacing].compact.join(',')}" if @width
|
12
|
+
end
|
13
|
+
|
14
|
+
# Spacing can be given as a single number for space between bars within a
|
15
|
+
# group or as an array of numbers with the first element as the space
|
16
|
+
# between bars within a group and the second element as the space between
|
17
|
+
# groups. If not provided, the default spacing between groups is 8 pixels.
|
18
|
+
#
|
19
|
+
# TODO: Should we raise an error if bar spacing is greater than group
|
20
|
+
# spacing? This would visually group bars from different groups.
|
21
|
+
def spacing=(spacing)
|
22
|
+
@spacing = [spacing].flatten
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
module GoogleChart
|
2
|
+
class Base
|
3
|
+
@@base_url = 'http://chart.apis.google.com/chart'
|
4
|
+
|
5
|
+
def initialize(options = {})
|
6
|
+
options.each {|key, value| send("#{key}=", value) }
|
7
|
+
yield self if block_given?
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.inherited(klass)
|
11
|
+
klass.class_eval do
|
12
|
+
class_variable_set(:@@parameters, [])
|
13
|
+
register!(:chart_type)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.register!(*parameters)
|
18
|
+
parameters.each do |parameter|
|
19
|
+
registry.push(parameter) unless registered?(parameter)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.registered?(parameter)
|
24
|
+
registry.include?(parameter)
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.registry
|
28
|
+
class_variable_get :@@parameters
|
29
|
+
end
|
30
|
+
|
31
|
+
# Collect all of the registered chart parameters and join them together
|
32
|
+
# to form the URL for the chart to be generated.
|
33
|
+
def to_url
|
34
|
+
"#{@@base_url}?#{query_string}"
|
35
|
+
end
|
36
|
+
|
37
|
+
private
|
38
|
+
def query_string
|
39
|
+
self.class.registry.map {|m| send(m) }.compact.join('&')
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
# <URL:http://code.google.com/apis/chart/#line_bar_pie_colors>
|
2
|
+
module GoogleChart
|
3
|
+
module Colors
|
4
|
+
def self.included(klass)
|
5
|
+
klass.register!(:color)
|
6
|
+
end
|
7
|
+
|
8
|
+
attr_writer :color
|
9
|
+
|
10
|
+
def color
|
11
|
+
'chco=' + [@color].flatten.join(',') if @color
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,91 @@
|
|
1
|
+
# <URL:http://code.google.com/apis/chart/#chart_data>
|
2
|
+
module GoogleChart
|
3
|
+
module Data
|
4
|
+
def self.included(klass)
|
5
|
+
klass.register!(:data)
|
6
|
+
end
|
7
|
+
|
8
|
+
attr_writer :encoding, :scale
|
9
|
+
|
10
|
+
@@simple_encoding = ('A'..'Z').to_a + ('a'..'z').to_a + ('0'..'9').to_a
|
11
|
+
@@extended_alphabet = ('A'..'Z').to_a + ('a'..'z').to_a + ('0'..'9').to_a + %w[- .]
|
12
|
+
@@extended_encoding = @@extended_alphabet.collect {|a|
|
13
|
+
@@extended_alphabet.collect {|b| a + b }
|
14
|
+
}.flatten
|
15
|
+
|
16
|
+
def data
|
17
|
+
'chd=' + send(:"#{encoding}_encode", @data) if @data
|
18
|
+
end
|
19
|
+
|
20
|
+
def data=(data)
|
21
|
+
@data = data.any? {|e| e.is_a?(Array) } ? data : [data]
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
def encoding
|
26
|
+
@encoding || :simple
|
27
|
+
end
|
28
|
+
|
29
|
+
def scale?
|
30
|
+
@scale
|
31
|
+
end
|
32
|
+
|
33
|
+
def scale_set(set, range)
|
34
|
+
if scale?
|
35
|
+
min, max = set.min, set.max
|
36
|
+
if min < 0
|
37
|
+
set.collect! {|n| n - min }
|
38
|
+
max -= min
|
39
|
+
end
|
40
|
+
set.collect {|n| (n * range).to_f / max if n }
|
41
|
+
else
|
42
|
+
set
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def simple_encode(data)
|
47
|
+
's:' + data.collect {|set|
|
48
|
+
scale_set(set, @@simple_encoding.size - 1).collect {|n|
|
49
|
+
case
|
50
|
+
when n.nil? then '_'
|
51
|
+
when n <= 0 then @@simple_encoding[0]
|
52
|
+
when n >= @@simple_encoding.size then @@simple_encoding[-1]
|
53
|
+
else @@simple_encoding[n.round]
|
54
|
+
end
|
55
|
+
}.join
|
56
|
+
}.join(',')
|
57
|
+
end
|
58
|
+
|
59
|
+
def extended_encode(data)
|
60
|
+
'e:' + data.collect {|set|
|
61
|
+
scale_set(set, @@extended_encoding.size - 1).collect {|n|
|
62
|
+
case
|
63
|
+
when n.nil? then '__'
|
64
|
+
when n <= 0 then @@extended_encoding[0]
|
65
|
+
when n >= @@extended_encoding.size then @@extended_encoding[-1]
|
66
|
+
else @@extended_encoding[n.round]
|
67
|
+
end
|
68
|
+
}.join
|
69
|
+
}.join(',')
|
70
|
+
end
|
71
|
+
|
72
|
+
def text_encode(data)
|
73
|
+
't:' + data.collect {|set|
|
74
|
+
scale_set(set, 100).collect {|n|
|
75
|
+
case
|
76
|
+
when n.nil? then -1
|
77
|
+
when n < 0 then 0
|
78
|
+
when n > 100 then 100
|
79
|
+
else
|
80
|
+
# More than one decimal place yields negligible resolution at the
|
81
|
+
# potentially great cost of one extra character in the URL. Avoid
|
82
|
+
# this by rounding to the nearest tenth. Further round down to an
|
83
|
+
# integer when possible to save the extra two characters (".0").
|
84
|
+
m = (n * 10).round
|
85
|
+
m / (m % 10 == 0 ? 10 : 10.0)
|
86
|
+
end
|
87
|
+
}.join(',')
|
88
|
+
}.join('|')
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# <URL:http://code.google.com/apis/chart/#grid>
|
2
|
+
module GoogleChart
|
3
|
+
module GridLines
|
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
|
12
|
+
'chg=' + @grid if @grid
|
13
|
+
end
|
14
|
+
|
15
|
+
def grid=(grid)
|
16
|
+
grid[:x] ||= @@default_grid_step
|
17
|
+
grid[:y] ||= @@default_grid_step
|
18
|
+
@grid = [grid[:x], grid[:y], *@@grid_line_styles[grid[:style]]].compact.join(',')
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
# <URL:http://code.google.com/apis/chart/#chdl>
|
2
|
+
module GoogleChart
|
3
|
+
module Legends
|
4
|
+
def self.included(klass)
|
5
|
+
klass.register!(:legend)
|
6
|
+
end
|
7
|
+
|
8
|
+
attr_writer :legend
|
9
|
+
|
10
|
+
def legend
|
11
|
+
'chdl=' + [@legend].flatten.collect {|l| CGI::escape(l) }.join('|') if @legend
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module GoogleChart
|
2
|
+
class Line < Base
|
3
|
+
@@chart_types = { :line => 'lc', :xy => 'lxy', :sparkline => 'ls' }
|
4
|
+
|
5
|
+
include Axes
|
6
|
+
include Colors
|
7
|
+
include Data
|
8
|
+
include GridLines
|
9
|
+
include Legends
|
10
|
+
include LineStyles
|
11
|
+
include RangeMarkers
|
12
|
+
include Sizes
|
13
|
+
include Titles
|
14
|
+
|
15
|
+
def chart_type
|
16
|
+
@chart_type ||= @@chart_types[:line]
|
17
|
+
'cht=' + @chart_type if @chart_type
|
18
|
+
end
|
19
|
+
|
20
|
+
def type=(chart_type)
|
21
|
+
@chart_type = @@chart_types[chart_type]
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# <URL:http://code.google.com/apis/chart/#line_styles>
|
2
|
+
module GoogleChart
|
3
|
+
module LineStyles
|
4
|
+
def self.included(klass)
|
5
|
+
klass.register!(:style)
|
6
|
+
end
|
7
|
+
|
8
|
+
@@line_styles = { :solid => [1,1,0], :dash => [1,3,2], :dot => [1,1,2] }
|
9
|
+
@@default_line_style = :solid
|
10
|
+
@@default_line_width = 1
|
11
|
+
|
12
|
+
def style
|
13
|
+
@style ||= []
|
14
|
+
@width ||= []
|
15
|
+
|
16
|
+
# Pad @style and @width with defaults until they are equal in length
|
17
|
+
(@style.size - @width.size).times { @width << @@default_line_width }
|
18
|
+
(@width.size - @style.size).times { @style << @@default_line_style }
|
19
|
+
|
20
|
+
unless @style.empty?
|
21
|
+
'chls=' + (0...@style.size).collect {|i|
|
22
|
+
@@line_styles[@style[i]].collect {|n| n * @width[i] }.join(',')
|
23
|
+
}.join('|')
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def style=(style)
|
28
|
+
@style = [style].flatten
|
29
|
+
end
|
30
|
+
|
31
|
+
def width=(width)
|
32
|
+
@width = [width].flatten
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# <URL:http://code.google.com/apis/chart/#hor_line_marker>
|
2
|
+
module GoogleChart
|
3
|
+
module RangeMarkers
|
4
|
+
def self.included(klass)
|
5
|
+
klass.register!(:ranges)
|
6
|
+
end
|
7
|
+
|
8
|
+
@@range_marker_orientations = { :h => 'r', :v => 'R' }
|
9
|
+
|
10
|
+
def ranges
|
11
|
+
unless @ranges.nil? || @ranges.empty?
|
12
|
+
'chm=' + @ranges.collect {|r|
|
13
|
+
r[0] = @@range_marker_orientations[r[0]]
|
14
|
+
r[1..-1] = ['000000cc', *r[1..-1]] if r.size == 3
|
15
|
+
# The middle parameter is required by Google Charts but ignored for range markers
|
16
|
+
r = r[0..1] + ['x'] + r[2..-1]
|
17
|
+
r.join(',')
|
18
|
+
}.join('|')
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def ranges=(ranges)
|
23
|
+
@ranges = ranges.any? {|e| e.is_a?(Array) } ? ranges : [ranges]
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# <URL:http://code.google.com/apis/chart/#chart_size>
|
2
|
+
module GoogleChart
|
3
|
+
module Sizes
|
4
|
+
def self.included(klass)
|
5
|
+
klass.register!(:size)
|
6
|
+
end
|
7
|
+
|
8
|
+
attr_writer :size
|
9
|
+
|
10
|
+
@@default_size = '600x500'
|
11
|
+
|
12
|
+
def size
|
13
|
+
'chs=' + (@size ? @size : @@default_size)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'cgi'
|
2
|
+
|
3
|
+
# <URL:http://code.google.com/apis/chart/#chtt>
|
4
|
+
module GoogleChart
|
5
|
+
module Titles
|
6
|
+
def self.included(klass)
|
7
|
+
klass.register!(:title)
|
8
|
+
end
|
9
|
+
|
10
|
+
attr_writer :title
|
11
|
+
|
12
|
+
def title
|
13
|
+
'chtt=' + CGI::escape(@title) if @title
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
data/lib/google_chart.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
$:.unshift(File.dirname(__FILE__)) unless
|
2
|
+
$:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
|
3
|
+
|
4
|
+
require 'google_chart/axes'
|
5
|
+
require 'google_chart/bar_styles'
|
6
|
+
require 'google_chart/colors'
|
7
|
+
require 'google_chart/data'
|
8
|
+
require 'google_chart/grid_lines'
|
9
|
+
require 'google_chart/legends'
|
10
|
+
require 'google_chart/line_styles'
|
11
|
+
require 'google_chart/range_markers'
|
12
|
+
require 'google_chart/sizes'
|
13
|
+
require 'google_chart/titles'
|
14
|
+
|
15
|
+
require 'google_chart/base'
|
16
|
+
require 'google_chart/bar'
|
17
|
+
require 'google_chart/line'
|
18
|
+
|
19
|
+
module GoogleChart
|
20
|
+
VERSION = '0.5.0'
|
21
|
+
|
22
|
+
def self.Line(options = {}, &block)
|
23
|
+
GoogleChart::Line.new(options, &block).to_url
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.Bar(options = {}, &block)
|
27
|
+
GoogleChart::Bar.new(options, &block).to_url
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../test_helper'
|
2
|
+
|
3
|
+
class TestAxes < Test::Unit::TestCase
|
4
|
+
def setup
|
5
|
+
@klass = Class.new(MockChart).class_eval { include GoogleChart::Axes }
|
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
|
+
end
|
52
|
+
|
53
|
+
context 'axis ranges' do
|
54
|
+
should 'dsiplay axis label ranges' do
|
55
|
+
assert_match(/\bchxt=x&chxr=0,8,16/, @klass.new(:axes => {:x => 8..16}).to_url)
|
56
|
+
end
|
57
|
+
|
58
|
+
should 'display multiple axis label ranges' do
|
59
|
+
assert_match(/\bchxt=x,y&chxr=0,6,14\|1,5,23\b/, @klass.new(:axes => {:x => 6..14, :y => 5..23}).to_url)
|
60
|
+
end
|
61
|
+
|
62
|
+
should 'display axis label ranges for subset of axes' do
|
63
|
+
assert_match(/\bchxr=0,0,5\|2,0,100\b/, @klass.new(:axes => {:x => 0..5, :y => true, :r => 0..100}).to_url)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
should 'display multiple types of axis labels' do
|
68
|
+
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)
|
69
|
+
end
|
70
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../test_helper'
|
2
|
+
|
3
|
+
class TestBar < Test::Unit::TestCase
|
4
|
+
should 'have a default chart type' do
|
5
|
+
assert_match(/\bcht=bvs\b/, GoogleChart::Bar.new.to_url)
|
6
|
+
end
|
7
|
+
|
8
|
+
should 'support horizontal orientation' do
|
9
|
+
assert_match(/\bcht=bhs\b/, GoogleChart::Bar.new(:horizontal => true).to_url)
|
10
|
+
end
|
11
|
+
|
12
|
+
should 'support grouped grouping' do
|
13
|
+
assert_match(/\bcht=bvg\b/, GoogleChart::Bar.new(:grouped => true).to_url)
|
14
|
+
end
|
15
|
+
|
16
|
+
should 'support horizontal orientation and grouped grouping' do
|
17
|
+
assert_match(/\bcht=bhg\b/, GoogleChart::Bar.new(:horizontal => true, :grouped => true).to_url)
|
18
|
+
end
|
19
|
+
|
20
|
+
should 'include Axes module' do
|
21
|
+
assert GoogleChart::Axes === GoogleChart::Bar.new
|
22
|
+
end
|
23
|
+
|
24
|
+
should 'include BarStyles module' do
|
25
|
+
assert GoogleChart::BarStyles === GoogleChart::Bar.new
|
26
|
+
end
|
27
|
+
|
28
|
+
should 'include Colors module' do
|
29
|
+
assert GoogleChart::Colors === GoogleChart::Bar.new
|
30
|
+
end
|
31
|
+
|
32
|
+
should 'include Data module' do
|
33
|
+
assert GoogleChart::Data === GoogleChart::Bar.new
|
34
|
+
end
|
35
|
+
|
36
|
+
should 'include GridLines module' do
|
37
|
+
assert GoogleChart::GridLines === GoogleChart::Bar.new
|
38
|
+
end
|
39
|
+
|
40
|
+
should 'include Legends module' do
|
41
|
+
assert GoogleChart::Legends === GoogleChart::Bar.new
|
42
|
+
end
|
43
|
+
|
44
|
+
should 'include RangeMarkers module' do
|
45
|
+
assert GoogleChart::RangeMarkers === GoogleChart::Bar.new
|
46
|
+
end
|
47
|
+
|
48
|
+
should 'include Sizes module' do
|
49
|
+
assert GoogleChart::Sizes === GoogleChart::Bar.new
|
50
|
+
end
|
51
|
+
|
52
|
+
should 'include Titles module' do
|
53
|
+
assert GoogleChart::Titles === GoogleChart::Bar.new
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../test_helper'
|
2
|
+
|
3
|
+
class TestBarStyles < Test::Unit::TestCase
|
4
|
+
def setup
|
5
|
+
@klass = Class.new(MockChart).class_eval { include GoogleChart::BarStyles }
|
6
|
+
end
|
7
|
+
|
8
|
+
should 'not display bar style parameter by default' do
|
9
|
+
assert_no_match(/\bchbh=/, @klass.new.to_url)
|
10
|
+
end
|
11
|
+
|
12
|
+
should 'display with custom bar width' do
|
13
|
+
assert_match(/\bchbh=10\b/, @klass.new(:width => 10).to_url)
|
14
|
+
end
|
15
|
+
|
16
|
+
should 'display with custom bar width and spacing' do
|
17
|
+
assert_match(/\bchbh=8,6\b/, @klass.new(:width => 8, :spacing => 6).to_url)
|
18
|
+
end
|
19
|
+
|
20
|
+
should 'display with custom bar width, spacing and group spacing' do
|
21
|
+
assert_match(/\bchbh=10,4,12\b/, @klass.new(:width => 10, :spacing => [4,12]).to_url)
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../test_helper'
|
2
|
+
|
3
|
+
class TestBase < Test::Unit::TestCase
|
4
|
+
should 'require descendent classes to define #chart_type method' do
|
5
|
+
klass = Class.new(GoogleChart::Base)
|
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::Base)
|
14
|
+
another_klass = Class.new(GoogleChart::Base)
|
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
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../test_helper'
|
2
|
+
|
3
|
+
class TestColors < Test::Unit::TestCase
|
4
|
+
def setup
|
5
|
+
@klass = Class.new(MockChart).class_eval { include GoogleChart::Colors }
|
6
|
+
end
|
7
|
+
|
8
|
+
should 'not display color parameter by default' do
|
9
|
+
assert_no_match(/\bchco=/, @klass.new.to_url)
|
10
|
+
end
|
11
|
+
|
12
|
+
should 'display single color parameter' do
|
13
|
+
assert_match(/\bchco=ff0000\b/, @klass.new(:color => 'ff0000').to_url)
|
14
|
+
end
|
15
|
+
|
16
|
+
should 'display multiple color parameters' do
|
17
|
+
assert_match(/\bchco=ff0000,00ff00,0000ff\b/, @klass.new(:color => %w[ff0000 00ff00 0000ff]).to_url)
|
18
|
+
end
|
19
|
+
|
20
|
+
should 'convert 3-digit colors to 6-digit colors'
|
21
|
+
end
|
@@ -0,0 +1,81 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../test_helper'
|
2
|
+
|
3
|
+
class TestData < Test::Unit::TestCase
|
4
|
+
def setup
|
5
|
+
@klass = Class.new(MockChart).class_eval { include GoogleChart::Data }
|
6
|
+
end
|
7
|
+
|
8
|
+
should 'use simple encoding by default' do
|
9
|
+
assert_match(/\bchd=s:AB89\b/, @klass.new(:data => [0,1,60,61]).to_url)
|
10
|
+
end
|
11
|
+
|
12
|
+
context 'simple encoding' do
|
13
|
+
should 'encode single dataset' do
|
14
|
+
assert_match(/\bchd=s:Af9\b/, @klass.new(:data => [0, 31, 61], :encoding => :simple).to_url)
|
15
|
+
end
|
16
|
+
|
17
|
+
should 'encode multiple datasets' do
|
18
|
+
assert_match(/\bchd=s:JP,YH\b/, @klass.new(:data => [[9, 15], [24, 7]], :encoding => :simple).to_url)
|
19
|
+
end
|
20
|
+
|
21
|
+
should 'encode missing data' do
|
22
|
+
assert_match(/\bchd=s:y_h\b/, @klass.new(:data => [50, nil, 33], :encoding => :simple).to_url)
|
23
|
+
end
|
24
|
+
|
25
|
+
should 'encode floating point data' do
|
26
|
+
assert_match(/\bchd=s:yh\b/, @klass.new(:data => [49.5, 33.4], :encoding => :simple).to_url)
|
27
|
+
end
|
28
|
+
|
29
|
+
should 'scale data based on maximum value by default'
|
30
|
+
|
31
|
+
should 'scale data using custom scale'
|
32
|
+
end
|
33
|
+
|
34
|
+
context 'extended encoding' do
|
35
|
+
should 'encode single dataset' do
|
36
|
+
assert_match(/\bchd=e:AA\.\.gA\b/, @klass.new(:data => [0, 4095, 2048], :encoding => :extended).to_url)
|
37
|
+
end
|
38
|
+
|
39
|
+
should 'encode multiple datasets' do
|
40
|
+
assert_match(/\bchd=e:jpyh,JPYH\b/, @klass.new(:data => [[2281, 3233], [591, 1543]], :encoding => :extended).to_url)
|
41
|
+
end
|
42
|
+
|
43
|
+
should 'encode missing data' do
|
44
|
+
assert_match(/\bchd=e:jp__yh\b/, @klass.new(:data => [2281, nil, 3233], :encoding => :extended).to_url)
|
45
|
+
end
|
46
|
+
|
47
|
+
should 'encode floating point data' do
|
48
|
+
assert_match(/\bchd=e:jpyh\b/, @klass.new(:data => [2281.4, 3232.5], :encoding => :extended).to_url)
|
49
|
+
end
|
50
|
+
|
51
|
+
should 'scale data based on maximum value by default'
|
52
|
+
|
53
|
+
should 'scale data using custom scale'
|
54
|
+
end
|
55
|
+
|
56
|
+
context 'text encoding' do
|
57
|
+
should 'encode single dataset' do
|
58
|
+
assert_match(/\bchd=t:6,14,5.2\b/, @klass.new(:data => [6, 14, 5.23], :encoding => :text).to_url)
|
59
|
+
end
|
60
|
+
|
61
|
+
should 'encode multiple datasets' do
|
62
|
+
assert_match(/\bchd=t:6,14\|5,23\b/, @klass.new(:data => [[6, 14], [5, 23]], :encoding => :text).to_url)
|
63
|
+
end
|
64
|
+
|
65
|
+
should 'encode missing data' do
|
66
|
+
assert_match(/\bchd=t:6,-1,14\b/, @klass.new(:data => [6, nil, 14], :encoding => :text).to_url)
|
67
|
+
end
|
68
|
+
|
69
|
+
should 'round floating point data to nearest 10th' do
|
70
|
+
assert_match(/\bchd=t:6.1,8.2\b/, @klass.new(:data => [6.14, 8.16], :encoding => :text).to_url)
|
71
|
+
end
|
72
|
+
|
73
|
+
should 'round floating point data to nearest integer when possible' do
|
74
|
+
assert_match(/\bchd=t:6,14\b/, @klass.new(:data => [5.95, 14.04], :encoding => :text).to_url)
|
75
|
+
end
|
76
|
+
|
77
|
+
should 'scale data based on maximum value by default'
|
78
|
+
|
79
|
+
should 'scale data using custom scale'
|
80
|
+
end
|
81
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../test_helper'
|
2
|
+
|
3
|
+
class TestGridLines < Test::Unit::TestCase
|
4
|
+
def setup
|
5
|
+
@klass = Class.new(MockChart).class_eval { include GoogleChart::GridLines }
|
6
|
+
end
|
7
|
+
|
8
|
+
should 'not display grid lines by default' do
|
9
|
+
assert_no_match(/\bchg=/, @klass.new.to_url)
|
10
|
+
end
|
11
|
+
|
12
|
+
should 'display vertical grid lines' do
|
13
|
+
assert_match(/\bchg=5,0\b/, @klass.new(:grid => {:x => 5}).to_url)
|
14
|
+
end
|
15
|
+
|
16
|
+
should 'display horizontal grid lines' do
|
17
|
+
assert_match(/\bchg=0,5\b/, @klass.new(:grid => {:y => 5}).to_url)
|
18
|
+
end
|
19
|
+
|
20
|
+
should 'display horizontal and vertical grid lines' do
|
21
|
+
assert_match(/\bchg=6,14\b/, @klass.new(:grid => {:x => 6, :y => 14}).to_url)
|
22
|
+
end
|
23
|
+
|
24
|
+
should 'display solid grid lines' do
|
25
|
+
assert_match(/\bchg=8,16,1,0\b/, @klass.new(:grid => {:x => 8, :y => 16, :style => :solid}).to_url)
|
26
|
+
end
|
27
|
+
|
28
|
+
should 'display dashed grid lines' do
|
29
|
+
assert_match(/\bchg=8,16,3,2\b/, @klass.new(:grid => {:x => 8, :y => 16, :style => :dash}).to_url)
|
30
|
+
end
|
31
|
+
|
32
|
+
should 'display dotted grid lines' do
|
33
|
+
assert_match(/\bchg=8,16,1,2\b/, @klass.new(:grid => {:x => 8, :y => 16, :style => :dot}).to_url)
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../test_helper'
|
2
|
+
|
3
|
+
class TestLegends < Test::Unit::TestCase
|
4
|
+
def setup
|
5
|
+
@klass = Class.new(MockChart).class_eval { include GoogleChart::Legends }
|
6
|
+
end
|
7
|
+
|
8
|
+
should 'not display legend by default' do
|
9
|
+
assert_no_match(/\bchdl=/, @klass.new.to_url)
|
10
|
+
end
|
11
|
+
|
12
|
+
should 'display legend for single dataset' do
|
13
|
+
assert_match(/\bchdl=Foo\b/, @klass.new(:legend => 'Foo').to_url)
|
14
|
+
end
|
15
|
+
|
16
|
+
should 'display legend for multiple datasets' do
|
17
|
+
assert_match(/\bchdl=Foo\|Bar\|Baz\b/, @klass.new(:legend => %w[Foo Bar Baz]).to_url)
|
18
|
+
end
|
19
|
+
|
20
|
+
should 'escape legend text for url' do
|
21
|
+
assert_match(/\bchdl=Foo\+Bar%7CBaz\b/, @klass.new(:legend => 'Foo Bar|Baz').to_url)
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../test_helper'
|
2
|
+
|
3
|
+
class TestLine < Test::Unit::TestCase
|
4
|
+
should 'have a default chart type' do
|
5
|
+
assert_match(/\bcht=lc\b/, GoogleChart::Line.new.to_url)
|
6
|
+
end
|
7
|
+
|
8
|
+
should 'support xy chart type' do
|
9
|
+
assert_match(/\bcht=lxy\b/, GoogleChart::Line.new(:type => :xy).to_url)
|
10
|
+
end
|
11
|
+
|
12
|
+
should 'support sparkline chart type' do
|
13
|
+
assert_match(/\bcht=ls\b/, GoogleChart::Line.new(:type => :sparkline).to_url)
|
14
|
+
end
|
15
|
+
|
16
|
+
should 'support line as chart type' do
|
17
|
+
assert_match(/\bcht=lc\b/, GoogleChart::Line.new(:type => :line).to_url)
|
18
|
+
end
|
19
|
+
|
20
|
+
should 'include Axes module' do
|
21
|
+
assert GoogleChart::Axes === GoogleChart::Line.new
|
22
|
+
end
|
23
|
+
|
24
|
+
should 'include Colors module' do
|
25
|
+
assert GoogleChart::Colors === GoogleChart::Line.new
|
26
|
+
end
|
27
|
+
|
28
|
+
should 'include Data module' do
|
29
|
+
assert GoogleChart::Data === GoogleChart::Line.new
|
30
|
+
end
|
31
|
+
|
32
|
+
should 'include GridLines module' do
|
33
|
+
assert GoogleChart::GridLines === GoogleChart::Line.new
|
34
|
+
end
|
35
|
+
|
36
|
+
should 'include Legends module' do
|
37
|
+
assert GoogleChart::Legends === GoogleChart::Line.new
|
38
|
+
end
|
39
|
+
|
40
|
+
should 'include LineStyles module' do
|
41
|
+
assert GoogleChart::LineStyles === GoogleChart::Line.new
|
42
|
+
end
|
43
|
+
|
44
|
+
should 'include RangeMarkers module' do
|
45
|
+
assert GoogleChart::RangeMarkers === GoogleChart::Line.new
|
46
|
+
end
|
47
|
+
|
48
|
+
should 'include Sizes module' do
|
49
|
+
assert GoogleChart::Sizes === GoogleChart::Line.new
|
50
|
+
end
|
51
|
+
|
52
|
+
should 'include Titles module' do
|
53
|
+
assert GoogleChart::Titles === GoogleChart::Line.new
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../test_helper'
|
2
|
+
|
3
|
+
class TestLineStyles < Test::Unit::TestCase
|
4
|
+
def setup
|
5
|
+
@klass = Class.new(MockChart).class_eval { include GoogleChart::LineStyles }
|
6
|
+
end
|
7
|
+
|
8
|
+
should 'not include line styles by default' do
|
9
|
+
assert_no_match(/\bchls=/, @klass.new.to_url)
|
10
|
+
end
|
11
|
+
|
12
|
+
should 'support solid lines' do
|
13
|
+
assert_match(/\bchls=1,1,0\b/, @klass.new(:style => :solid).to_url)
|
14
|
+
end
|
15
|
+
|
16
|
+
should 'support dashed lines' do
|
17
|
+
assert_match(/\bchls=1,3,2\b/, @klass.new(:style => :dash).to_url)
|
18
|
+
end
|
19
|
+
|
20
|
+
should 'support dotted lines' do
|
21
|
+
assert_match(/\bchls=1,1,2\b/, @klass.new(:style => :dot).to_url)
|
22
|
+
end
|
23
|
+
|
24
|
+
should 'support multiple line styles' do
|
25
|
+
assert_match(/\bchls=1,3,2\|1,1,2\|1,1,0\b/, @klass.new(:style => [:dash, :dot, :solid]).to_url)
|
26
|
+
end
|
27
|
+
|
28
|
+
should 'support custom line width' do
|
29
|
+
assert_match(/\bchls=2,2,0\b/, @klass.new(:width => 2).to_url)
|
30
|
+
end
|
31
|
+
|
32
|
+
should 'support named line style with custom width' do
|
33
|
+
assert_match(/\bchls=2,6,4\b/, @klass.new(:style => :dash, :width => 2).to_url)
|
34
|
+
end
|
35
|
+
|
36
|
+
should 'support multiple line styles and multiple custom widths' do
|
37
|
+
assert_match(/\bchls=2,2,4\|3,9,6\b/, @klass.new(:style => [:dot, :dash], :width => [2, 3]).to_url)
|
38
|
+
end
|
39
|
+
|
40
|
+
should 'use default line width if there are more styles than widths' do
|
41
|
+
assert_match(/\bchls=2,2,4\|1,3,2\|1,1,0\b/, @klass.new(:style => [:dot, :dash, :solid], :width => 2).to_url)
|
42
|
+
end
|
43
|
+
|
44
|
+
should 'use default line style if there are more widths than styles' do
|
45
|
+
assert_match(/\bchls=2,2,4\|3,3,0\|2,2,0\b/, @klass.new(:style => :dot, :width => [2, 3, 2]).to_url)
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../test_helper'
|
2
|
+
|
3
|
+
class TestRangeMarkers < Test::Unit::TestCase
|
4
|
+
def setup
|
5
|
+
@klass = Class.new(MockChart).class_eval { include GoogleChart::RangeMarkers }
|
6
|
+
end
|
7
|
+
|
8
|
+
should 'not have range markers by default' do
|
9
|
+
assert_no_match(/\bchm=/, @klass.new.to_url)
|
10
|
+
end
|
11
|
+
|
12
|
+
should 'display horizontal range markers' do
|
13
|
+
assert_match(/\bchm=r,000000cc,x,0.45,0.55\b/, @klass.new(:ranges => [:h, 0.45, 0.55]).to_url)
|
14
|
+
end
|
15
|
+
|
16
|
+
should 'display vertical range markers' do
|
17
|
+
assert_match(/\bchm=R,000000cc,x,0.45,0.55\b/, @klass.new(:ranges => [:v, 0.45, 0.55]).to_url)
|
18
|
+
end
|
19
|
+
|
20
|
+
should 'display multiple range markers' do
|
21
|
+
assert_match(/\bchm=r,000000cc,x,0.1,0.2\|R,000000cc,x,0.3,0.4\b/, @klass.new(:ranges => [[:h, 0.1, 0.2], [:v, 0.3, 0.4]]).to_url)
|
22
|
+
end
|
23
|
+
|
24
|
+
should 'display range markers with custom colors' do
|
25
|
+
assert_match(/\bchm=r,abcdef,x,0.4,0.6\b/, @klass.new(:ranges => [:h, 'abcdef', 0.4, 0.6]).to_url)
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../test_helper'
|
2
|
+
|
3
|
+
class TestSizes < Test::Unit::TestCase
|
4
|
+
def setup
|
5
|
+
@klass = Class.new(MockChart).class_eval { include GoogleChart::Sizes }
|
6
|
+
end
|
7
|
+
|
8
|
+
should 'have a default size' do
|
9
|
+
assert_match(/\bchs=600x500\b/, @klass.new.to_url)
|
10
|
+
end
|
11
|
+
|
12
|
+
should 'accept custom size' do
|
13
|
+
assert_match(/\bchs=800x375\b/, @klass.new(:size => '800x375').to_url)
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../test_helper'
|
2
|
+
|
3
|
+
class TestTitles < Test::Unit::TestCase
|
4
|
+
def setup
|
5
|
+
@klass = Class.new(MockChart).class_eval { include GoogleChart::Titles }
|
6
|
+
end
|
7
|
+
|
8
|
+
should 'not have a title by default' do
|
9
|
+
assert_no_match(/\bchtt=/, @klass.new.to_url)
|
10
|
+
end
|
11
|
+
|
12
|
+
should 'url-encode title' do
|
13
|
+
assert_match(/\bchtt=foo\+bar\b/, @klass.new(:title => 'foo bar').to_url)
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,97 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper.rb'
|
2
|
+
|
3
|
+
class TestGoogleChart < Test::Unit::TestCase
|
4
|
+
context '#Line' do
|
5
|
+
context 'with hash parameters' do
|
6
|
+
setup do
|
7
|
+
@url = GoogleChart.Line(:size => '1000x300', :data => [[35, 41], [50, 33]])
|
8
|
+
end
|
9
|
+
|
10
|
+
should 'return Google Chart URL' do
|
11
|
+
assert_match(%r{\Ahttp://chart\.apis\.google\.com/chart\?}, @url)
|
12
|
+
end
|
13
|
+
|
14
|
+
should 'include chart type parameter' do
|
15
|
+
assert_match(/\bcht=lc\b/, @url)
|
16
|
+
end
|
17
|
+
|
18
|
+
should 'include chart size parameter' do
|
19
|
+
assert_match(/\bchs=1000x300\b/, @url)
|
20
|
+
end
|
21
|
+
|
22
|
+
should 'include chart data parameter' do
|
23
|
+
assert_match(/\bchd=s:jp,yh\b/, @url)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
context 'with block parameters' do
|
28
|
+
setup do
|
29
|
+
@url = GoogleChart.Line do |c|
|
30
|
+
c.size = '800x375'
|
31
|
+
c.data = [[35, 41], [50, 33]]
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
should 'return Google Chart URL' do
|
36
|
+
assert_match(%r{\Ahttp://chart\.apis\.google\.com/chart\?}, @url)
|
37
|
+
end
|
38
|
+
|
39
|
+
should 'include chart type parameter' do
|
40
|
+
assert_match(/\bcht=lc\b/, @url)
|
41
|
+
end
|
42
|
+
|
43
|
+
should 'include chart size parameter' do
|
44
|
+
assert_match(/\bchs=800x375\b/, @url)
|
45
|
+
end
|
46
|
+
|
47
|
+
should 'include chart data parameter' do
|
48
|
+
assert_match(/\bchd=s:jp,yh\b/, @url)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
context '#Bar' do
|
54
|
+
context 'with hash parameters' do
|
55
|
+
setup do
|
56
|
+
@url = GoogleChart.Bar(:size => '1000x300', :data => [[35, 41], [50, 33]])
|
57
|
+
end
|
58
|
+
|
59
|
+
should 'return Google Chart URL' do
|
60
|
+
assert_match(%r{\Ahttp://chart\.apis\.google\.com/chart\?}, @url)
|
61
|
+
end
|
62
|
+
|
63
|
+
should 'include chart type parameter' do
|
64
|
+
assert_match(/\bcht=bvs\b/, @url)
|
65
|
+
end
|
66
|
+
|
67
|
+
should 'include chart size parameter' do
|
68
|
+
assert_match(/\bchs=1000x300\b/, @url)
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
context 'with block parameters' do
|
73
|
+
setup do
|
74
|
+
@url = GoogleChart.Bar do |c|
|
75
|
+
c.size = '800x375'
|
76
|
+
c.data = [[35, 41], [50, 33]]
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
should 'return Google Chart URL' do
|
81
|
+
assert_match(%r{\Ahttp://chart\.apis\.google\.com/chart\?}, @url)
|
82
|
+
end
|
83
|
+
|
84
|
+
should 'include chart type parameter' do
|
85
|
+
assert_match(/\bcht=bvs\b/, @url)
|
86
|
+
end
|
87
|
+
|
88
|
+
should 'include chart size parameter' do
|
89
|
+
assert_match(/\bchs=800x375\b/, @url)
|
90
|
+
end
|
91
|
+
|
92
|
+
should 'include chart data parameter' do
|
93
|
+
assert_match(/\bchd=s:jp,yh\b/, @url)
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,113 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: jparker-ruby-googlechart
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.5.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- John Parker
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2008-11-11 00:00:00 -08:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: newgem
|
17
|
+
version_requirement:
|
18
|
+
version_requirements: !ruby/object:Gem::Requirement
|
19
|
+
requirements:
|
20
|
+
- - ">="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 1.0.3
|
23
|
+
version:
|
24
|
+
- !ruby/object:Gem::Dependency
|
25
|
+
name: hoe
|
26
|
+
version_requirement:
|
27
|
+
version_requirements: !ruby/object:Gem::Requirement
|
28
|
+
requirements:
|
29
|
+
- - ">="
|
30
|
+
- !ruby/object:Gem::Version
|
31
|
+
version: 1.8.0
|
32
|
+
version:
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: thoughtbot-shoulda
|
35
|
+
version_requirement:
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 2.0.0
|
41
|
+
version:
|
42
|
+
description: ruby-googlechart is a ruby library which provides object-oriented access to the Google Charts API
|
43
|
+
email: jparker@urgetopunt.com
|
44
|
+
executables: []
|
45
|
+
|
46
|
+
extensions: []
|
47
|
+
|
48
|
+
extra_rdoc_files:
|
49
|
+
- History.txt
|
50
|
+
- Manifest.txt
|
51
|
+
- README.txt
|
52
|
+
files:
|
53
|
+
- History.txt
|
54
|
+
- Manifest.txt
|
55
|
+
- Rakefile
|
56
|
+
- README.txt
|
57
|
+
- lib/google_chart.rb
|
58
|
+
- lib/google_chart/axes.rb
|
59
|
+
- lib/google_chart/bar.rb
|
60
|
+
- lib/google_chart/bar_styles.rb
|
61
|
+
- lib/google_chart/base.rb
|
62
|
+
- lib/google_chart/colors.rb
|
63
|
+
- lib/google_chart/data.rb
|
64
|
+
- lib/google_chart/grid_lines.rb
|
65
|
+
- lib/google_chart/legends.rb
|
66
|
+
- lib/google_chart/line.rb
|
67
|
+
- lib/google_chart/line_styles.rb
|
68
|
+
- lib/google_chart/range_markers.rb
|
69
|
+
- lib/google_chart/sizes.rb
|
70
|
+
- lib/google_chart/titles.rb
|
71
|
+
has_rdoc: false
|
72
|
+
homepage: http://github.com/jparker/ruby-googlechart
|
73
|
+
post_install_message:
|
74
|
+
rdoc_options:
|
75
|
+
- --main
|
76
|
+
- README.txt
|
77
|
+
require_paths:
|
78
|
+
- lib
|
79
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - ">="
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: "0"
|
84
|
+
version:
|
85
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: "0"
|
90
|
+
version:
|
91
|
+
requirements: []
|
92
|
+
|
93
|
+
rubyforge_project:
|
94
|
+
rubygems_version: 1.2.0
|
95
|
+
signing_key:
|
96
|
+
specification_version: 2
|
97
|
+
summary: Ruby wrapper for the Google Charts API
|
98
|
+
test_files:
|
99
|
+
- test/test_helper.rb
|
100
|
+
- test/test_google_chart.rb
|
101
|
+
- test/google_chart/test_axes.rb
|
102
|
+
- test/google_chart/test_bar.rb
|
103
|
+
- test/google_chart/test_bar_styles.rb
|
104
|
+
- test/google_chart/test_base.rb
|
105
|
+
- test/google_chart/test_colors.rb
|
106
|
+
- test/google_chart/test_data.rb
|
107
|
+
- test/google_chart/test_grid_lines.rb
|
108
|
+
- test/google_chart/test_legends.rb
|
109
|
+
- test/google_chart/test_line.rb
|
110
|
+
- test/google_chart/test_line_styles.rb
|
111
|
+
- test/google_chart/test_range_markers.rb
|
112
|
+
- test/google_chart/test_sizes.rb
|
113
|
+
- test/google_chart/test_titles.rb
|