ruby-googlechart 0.6.4

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.
@@ -0,0 +1,3 @@
1
+ pkg
2
+ dog
3
+ Manifest
@@ -0,0 +1,30 @@
1
+ == 0.0.1 2008-05-14
2
+
3
+ * Initial release
4
+
5
+ == 0.5.0 2008-11-08
6
+
7
+ * Code cleanup, improved tests
8
+ * First working gem release
9
+
10
+ == 0.6.0 2008-11-12
11
+
12
+ * Continued cleanup
13
+ * Changed data scaling behavior with negative values
14
+
15
+ == 0.6.1 2008-11-12
16
+
17
+ * Gem cleanup (switched from newgem + hoe to echoe)
18
+ * Fixed data scaling bug with all-zero data
19
+
20
+ == 0.6.2 2008-11-20
21
+
22
+ * Fixed axis label encoding to handle +nil+
23
+
24
+ == 0.6.3 2008-11-23
25
+
26
+ * Increased speed of Data#normalize
27
+ * Fixed axis label and legend encoding to handle non-Strings
28
+ * Raise ArgumentError if all data sets are empty
29
+ * Fixed auto-scaling of data containing +nil+
30
+ * Redesigned bar grouping (now supports overlapping) [BACKWARDS INCOMPATIBLE]
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 John Parker
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,59 @@
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
17
+
18
+ * Line charts (line, XY, sparkline)
19
+ * Bar charts (horizontal, vertical, grouped, stacked)
20
+
21
+ == SYNOPSIS
22
+
23
+ require 'google_chart'
24
+
25
+ url = GoogleChart.Line(:data => [1, nil, 2, 8, 1])
26
+ url = GoogleChart.Bar(:data => [5, 23, 6, 14])
27
+
28
+ url = GoogleChart.Bar do |c|
29
+ c.axes = {:x => %w[Q1 Q2 Q3 Q4], :y => 0..400}
30
+ c.data = [335, 285, 240, 220, 160, 175, 200, 205]
31
+ c.encoding = :extended
32
+ c.scale = 0..400
33
+ c.size = '800x375'
34
+ c.style = :dash
35
+ c.title = "Are You Pondering What I'm Pondering?"
36
+ c.type = :line
37
+ end
38
+ url = GoogleChart.Bar do |c|
39
+ c.horizontal = true
40
+ c.grouped = true
41
+ c.data = [[5, 6, 7, 8], [23, 14, 17, 16]]
42
+ c.encoding = :text
43
+ c.title = 'Lorem Ipsum Dolor'
44
+ end
45
+
46
+ chart = GoogleChart::LineChart.new
47
+ chart.type = :sparkline
48
+ chart.size = '75x25'
49
+ chart.data = [1, 1, 2, 3, 5, 8, 13]
50
+ chart.color = 'ff0000'
51
+ url = chart.to_url
52
+
53
+ == REQUIREMENTS
54
+
55
+ * Low standards
56
+
57
+ == INSTALL
58
+
59
+ * sudo gem install ruby-googlechart
@@ -0,0 +1,52 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "ruby-googlechart"
8
+ gem.summary = %Q{Ruby wrapper around the Google Charts API}
9
+ gem.description = %Q{ruby-googlechart is yet another wrapper around the Google Charts API}
10
+ gem.email = "jparker@urgetopunt.com"
11
+ gem.homepage = "http://github.com/jparker/ruby-googlechart"
12
+ gem.authors = ["John Parker"]
13
+ gem.add_development_dependency "shoulda", ">= 0"
14
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
+ end
16
+ rescue LoadError
17
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
18
+ end
19
+
20
+ require 'rake/testtask'
21
+ Rake::TestTask.new(:test) do |test|
22
+ test.libs << 'lib' << 'test'
23
+ test.pattern = 'test/**/test_*.rb'
24
+ test.verbose = true
25
+ end
26
+
27
+ begin
28
+ require 'rcov/rcovtask'
29
+ Rcov::RcovTask.new do |test|
30
+ test.libs << 'test'
31
+ test.pattern = 'test/**/test_*.rb'
32
+ test.verbose = true
33
+ end
34
+ rescue LoadError
35
+ task :rcov do
36
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
37
+ end
38
+ end
39
+
40
+ task :test => :check_dependencies
41
+
42
+ task :default => :test
43
+
44
+ require 'rake/rdoctask'
45
+ Rake::RDocTask.new do |rdoc|
46
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
47
+
48
+ rdoc.rdoc_dir = 'rdoc'
49
+ rdoc.title = "ruby-googlechart #{version}"
50
+ rdoc.rdoc_files.include('README*')
51
+ rdoc.rdoc_files.include('lib/**/*.rb')
52
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.6.4
@@ -0,0 +1,7 @@
1
+ unless [].respond_to?(:sum)
2
+ Array.class_eval do
3
+ def sum
4
+ inject(0) {|sum, n| sum + n }
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,30 @@
1
+ $:.unshift(File.dirname(__FILE__)) unless
2
+ $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
3
+
4
+ require 'cgi'
5
+
6
+ require 'array'
7
+
8
+ require 'google_chart/axis'
9
+ require 'google_chart/bar_style'
10
+ require 'google_chart/color'
11
+ require 'google_chart/data'
12
+ require 'google_chart/grid_line'
13
+ require 'google_chart/legend'
14
+ require 'google_chart/line_style'
15
+ require 'google_chart/range_marker'
16
+ require 'google_chart/title'
17
+
18
+ require 'google_chart/abstract_chart'
19
+ require 'google_chart/bar_chart'
20
+ require 'google_chart/line_chart'
21
+
22
+ module GoogleChart
23
+ def self.Line(options = {}, &block)
24
+ GoogleChart::LineChart.new(options, &block).to_url
25
+ end
26
+
27
+ def self.Bar(options = {}, &block)
28
+ GoogleChart::BarChart.new(options, &block).to_url
29
+ end
30
+ end
@@ -0,0 +1,48 @@
1
+ module GoogleChart
2
+ class AbstractChart
3
+ @@base_url = 'http://chart.apis.google.com/chart'
4
+ @@default_size = '600x500'
5
+
6
+ def initialize(options = {})
7
+ options.each {|key, value| send("#{key}=", value) }
8
+ yield self if block_given?
9
+ end
10
+
11
+ def to_url
12
+ "#{@@base_url}?#{query_string}"
13
+ end
14
+
15
+ attr_writer :size
16
+
17
+ def size
18
+ @size ||= @@default_size
19
+ "chs=#{@size}"
20
+ end
21
+
22
+ def self.inherited(klass)
23
+ klass.class_eval do
24
+ class_variable_set(:@@parameters, [])
25
+ register!(:chart_type, :size)
26
+ end
27
+ end
28
+
29
+ def self.register!(*parameters)
30
+ parameters.each do |parameter|
31
+ registry.push(parameter) unless registered?(parameter)
32
+ end
33
+ end
34
+
35
+ def self.registered?(parameter)
36
+ registry.include?(parameter)
37
+ end
38
+
39
+ def self.registry
40
+ class_variable_get :@@parameters
41
+ end
42
+
43
+ private
44
+ def query_string
45
+ self.class.registry.map {|m| send(m) }.compact.join('&')
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,40 @@
1
+ # <URL:http://code.google.com/apis/chart/#multiple_axes_labels>
2
+ module GoogleChart
3
+ module Axis
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=(axes)
11
+ idx = 0
12
+ @axes, @axis_labels, @axis_ranges = [], [], []
13
+ [:x, :y, :r, :t].each do |axis|
14
+ case axes[axis]
15
+ when Array
16
+ @axis_labels << ("#{idx}:|" + axes[axis].map {|l| CGI::escape(l.to_s) }.join('|'))
17
+ @axes << axis
18
+ idx += 1
19
+ when Range
20
+ @axis_ranges << ("#{idx},#{axes[axis].first},#{axes[axis].last}")
21
+ @axes << axis
22
+ idx += 1
23
+ when true
24
+ @axes << axis
25
+ idx += 1
26
+ end
27
+ end
28
+ end
29
+
30
+ def axes
31
+ unless @axes.nil? || @axes.empty?
32
+ [
33
+ 'chxt=' + @axes.join(','),
34
+ @axis_labels.empty? ? nil : 'chxl=' + @axis_labels.join('|'),
35
+ @axis_ranges.empty? ? nil : 'chxr=' + @axis_ranges.join('|')
36
+ ].compact.join('&')
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,57 @@
1
+ require 'matrix'
2
+
3
+ module GoogleChart
4
+ class BarChart < AbstractChart
5
+ include Axis
6
+ include BarStyle
7
+ include Color
8
+ include Data
9
+ include GridLine
10
+ include Legend
11
+ include RangeMarker
12
+ include Title
13
+
14
+ @@orientations = {:horizontal => 'h', :vertical => 'v'}
15
+ @@groupings = {:group => 'g', :stack => 's', :overlap => 's'}
16
+
17
+ attr_writer :orientation, :grouping
18
+
19
+ def horizontal=(arg)
20
+ self.orientation = arg ? :horizontal : :vertical
21
+ end
22
+
23
+ def chart_type
24
+ @grouping ||= :group
25
+ @orientation ||= :vertical
26
+ "cht=b#{@@orientations[@orientation]}#{@@groupings[@grouping]}"
27
+ end
28
+
29
+ private
30
+ def reduce(data)
31
+ if @grouping == :overlap
32
+ # FIXME: This is really ugly!
33
+ (data.size - 1).downto(1) do |i|
34
+ data[i].size.times do |j|
35
+ data[i][j] -= data[0...i].map {|set|
36
+ set[j].nil? || set[j] < 0 ? 0 : set[j]
37
+ }.sum
38
+ end
39
+ end
40
+ data
41
+ else
42
+ super
43
+ end
44
+ end
45
+
46
+ def set_scale
47
+ if @grouping == :stack
48
+ if @scale.nil? && !@data.nil?
49
+ min = [0, @data.map {|set| set.compact.min }.compact.min].max
50
+ max = Matrix[*@data].column_vectors.map {|v| v.to_a.compact.sum }.max
51
+ @scale = min..max
52
+ end
53
+ end
54
+ super
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,19 @@
1
+ # <URL:http://code.google.com/apis/chart/#bar_width>
2
+ module GoogleChart
3
+ module BarStyle
4
+ def self.included(klass)
5
+ klass.register!(:style)
6
+ end
7
+
8
+ attr_writer :width
9
+
10
+ # TODO: raise error if bar spacing > group spacing?
11
+ def spacing=(spacing)
12
+ @spacing = [spacing].flatten
13
+ end
14
+
15
+ def style
16
+ "chbh=#{[@width, *@spacing].compact.join(',')}" if @width
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,16 @@
1
+ # <URL:http://code.google.com/apis/chart/#line_bar_pie_colors>
2
+ module GoogleChart
3
+ module Color
4
+ def self.included(klass)
5
+ klass.register!(:color)
6
+ end
7
+
8
+ def color=(color)
9
+ @color = [color].flatten
10
+ end
11
+
12
+ def color
13
+ "chco=#{@color.join(',')}" if @color
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,99 @@
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 = ('A'..'Z').to_a + ('a'..'z').to_a + ('0'..'9').to_a
11
+ alphabet = ('A'..'Z').to_a + ('a'..'z').to_a + ('0'..'9').to_a + %w[- .]
12
+ @@extended = alphabet.map {|a| alphabet.map {|b| a + b }}.flatten
13
+
14
+ def data=(data)
15
+ @data = data.any? {|e| Array === e } ? data : [data]
16
+ if @data.all? {|set| set.compact.empty? }
17
+ raise ArgumentError, 'data must contain at least 1 non-nil value'
18
+ end
19
+ end
20
+
21
+ def data
22
+ set_scale
23
+ 'chd=' + send(:"#{encoding}_encode", reduce(@data)) if @data
24
+ end
25
+
26
+ private
27
+ def encoding
28
+ @encoding ||= :simple
29
+ end
30
+
31
+ def set_scale
32
+ if @scale.nil? && !@data.nil?
33
+ min = @data.map {|set| set.compact.min }.compact.min
34
+ min = 0 if min > 0
35
+ max = @data.map {|set| set.compact.max }.compact.max
36
+ @scale = min..max
37
+ end
38
+ @scale
39
+ end
40
+
41
+ def reduce(data)
42
+ data
43
+ end
44
+
45
+ def normalize(set, encoding_max)
46
+ min, max = @scale.first, @scale.last
47
+ if min != max
48
+ set.map {|e| (e.to_f - min) / (max - min) * encoding_max if e }
49
+ else
50
+ set
51
+ end
52
+ end
53
+
54
+ def simple_encode(data)
55
+ 's:' + data.map {|set|
56
+ normalize(set, @@simple.size - 1).map {|e|
57
+ case
58
+ when e.nil? then '_'
59
+ when e <= 0 then @@simple[0]
60
+ when e >= @@simple.size then @@simple[-1]
61
+ else @@simple[e.floor]
62
+ end
63
+ }.join
64
+ }.join(',')
65
+ end
66
+
67
+ def extended_encode(data)
68
+ 'e:' + data.map {|set|
69
+ normalize(set, @@extended.size - 1).collect {|e|
70
+ case
71
+ when e.nil? then '__'
72
+ when e <= 0 then @@extended[0]
73
+ when e >= @@extended.size then @@extended[-1]
74
+ else @@extended[e.floor]
75
+ end
76
+ }.join
77
+ }.join(',')
78
+ end
79
+
80
+ def text_encode(data)
81
+ 't:' + data.map {|set|
82
+ normalize(set, 100).map {|e|
83
+ case
84
+ when e.nil? then -1
85
+ when e < 0 then 0
86
+ when e > 100 then 100
87
+ else
88
+ # More than one decimal place yields negligible resolution at the
89
+ # potentially great cost of one extra character in the URL. Avoid
90
+ # this by rounding to the nearest tenth. Further round down to an
91
+ # integer when possible to save the extra two characters (".0").
92
+ n = (e * 10).round
93
+ n / (n % 10 == 0 ? 10 : 10.0)
94
+ end
95
+ }.join(',')
96
+ }.join('|')
97
+ end
98
+ end
99
+ end