dbgrandi-googlecharts 1.3.2
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +33 -0
- data/License.txt +20 -0
- data/Manifest.txt +16 -0
- data/README.txt +180 -0
- data/Rakefile +32 -0
- data/config/hoe.rb +71 -0
- data/config/requirements.rb +16 -0
- data/lib/gchart.rb +392 -0
- data/lib/gchart/aliases.rb +14 -0
- data/lib/gchart/version.rb +9 -0
- data/setup.rb +1585 -0
- data/spec/gchart_spec.rb +411 -0
- data/spec/spec.opts +1 -0
- data/spec/spec_helper.rb +7 -0
- data/tasks/environment.rake +7 -0
- data/tasks/rspec.rake +21 -0
- data/website/index.txt +476 -0
- metadata +74 -0
data/History.txt
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
== 1.3.1
|
2
|
+
* added width and spacing options
|
3
|
+
|
4
|
+
== 1.3.0
|
5
|
+
* added support for google-o-meter
|
6
|
+
* fixed a bug when the max value of a data set was 0
|
7
|
+
|
8
|
+
== 1.2.0
|
9
|
+
* added support for sparklines
|
10
|
+
|
11
|
+
== 1.1.0
|
12
|
+
* fixed another bug fix related to the uri escaping required to download the file properly.
|
13
|
+
|
14
|
+
== 1.0.0
|
15
|
+
* fixed the (URI::InvalidURIError) issue
|
16
|
+
|
17
|
+
== 0.2.0
|
18
|
+
* added export options (file and image tag)
|
19
|
+
* added support for all arguments to be passed as a string or an array
|
20
|
+
|
21
|
+
== 0.1.0 2007-12-11
|
22
|
+
* fixed the axis labels
|
23
|
+
|
24
|
+
== 0.0.3 2007-12-11
|
25
|
+
* added :chart_background alias and fixed a bug related to the background colors.
|
26
|
+
|
27
|
+
== 0.0.2 2007-12-11
|
28
|
+
* added support for more features and aliases
|
29
|
+
|
30
|
+
== 0.0.1 2007-12-08
|
31
|
+
|
32
|
+
* 1 major enhancement:
|
33
|
+
* Initial release
|
data/License.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2007 Matt Aimonetti
|
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.
|
data/Manifest.txt
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
History.txt
|
2
|
+
License.txt
|
3
|
+
Manifest.txt
|
4
|
+
README.txt
|
5
|
+
Rakefile
|
6
|
+
config/hoe.rb
|
7
|
+
config/requirements.rb
|
8
|
+
lib/gchart.rb
|
9
|
+
lib/gchart/aliases.rb
|
10
|
+
lib/gchart/version.rb
|
11
|
+
setup.rb
|
12
|
+
spec/gchart_spec.rb
|
13
|
+
spec/spec.opts
|
14
|
+
spec/spec_helper.rb
|
15
|
+
tasks/environment.rake
|
16
|
+
tasks/rspec.rake
|
data/README.txt
ADDED
@@ -0,0 +1,180 @@
|
|
1
|
+
The goal of this Gem is to make the creation of Google Charts a simple and easy task.
|
2
|
+
|
3
|
+
Gchart.line( :size => '200x300',
|
4
|
+
:title => "example title",
|
5
|
+
:bg => 'efefef',
|
6
|
+
:legend => ['first data set label', 'second data set label'],
|
7
|
+
:data => [10, 30, 120, 45, 72])
|
8
|
+
|
9
|
+
|
10
|
+
|
11
|
+
==Chart Type
|
12
|
+
|
13
|
+
This gem supports the following types of charts:
|
14
|
+
|
15
|
+
* line,
|
16
|
+
* line_xy
|
17
|
+
* sparkline
|
18
|
+
* scatter
|
19
|
+
* bar
|
20
|
+
* venn
|
21
|
+
* pie
|
22
|
+
* pie_3d
|
23
|
+
|
24
|
+
To create a chart, simply require Gchart and call any of the existing type:
|
25
|
+
|
26
|
+
require 'gchart'
|
27
|
+
Gchart.pie
|
28
|
+
|
29
|
+
|
30
|
+
==Chart Title
|
31
|
+
|
32
|
+
To add a title to a chart pass the title to your chart:
|
33
|
+
|
34
|
+
Gchart.line(:title => 'Sexy Charts!')
|
35
|
+
|
36
|
+
You can also specify the color and/or size
|
37
|
+
|
38
|
+
Gchart.line(:title => 'Sexy Charts!', :title_color => 'FF0000', :title_size => '20')
|
39
|
+
|
40
|
+
==Colors
|
41
|
+
|
42
|
+
Specify a color with at least a 6-letter string of hexadecimal values in the format RRGGBB. For example:
|
43
|
+
|
44
|
+
* FF0000 = red
|
45
|
+
* 00FF00 = green
|
46
|
+
* 0000FF = blue
|
47
|
+
* 000000 = black
|
48
|
+
* FFFFFF = white
|
49
|
+
|
50
|
+
You can optionally specify transparency by appending a value between 00 and FF where 00 is completely transparent and FF completely opaque. For example:
|
51
|
+
|
52
|
+
* 0000FFFF = solid blue
|
53
|
+
* 0000FF00 = transparent blue
|
54
|
+
|
55
|
+
If you need to use multiple colors, check the doc. Usually you just need to pass :attribute => 'FF0000,00FF00'
|
56
|
+
|
57
|
+
Some charts have more options than other, make sure to refer to the documentation.
|
58
|
+
|
59
|
+
===Background options:
|
60
|
+
|
61
|
+
If you don't set the background option, your graph will be transparent.
|
62
|
+
|
63
|
+
* You have 3 types of background http://code.google.com/apis/chart/#chart_or_background_fill
|
64
|
+
|
65
|
+
- solid
|
66
|
+
- gradient
|
67
|
+
- stripes
|
68
|
+
|
69
|
+
By default, if you set a background color, the fill will be solid:
|
70
|
+
|
71
|
+
Gchart.bar(:bg => 'efefef')
|
72
|
+
|
73
|
+
However you can specify another fill type such as:
|
74
|
+
|
75
|
+
Gchart.line(:bg => {:color => 'efefef', :type => 'gradient'})
|
76
|
+
|
77
|
+
In the above code, we decided to have a gradient background, however since we only passed one color, the chart will start by the specified color and transition to white. By the default, the gradient angle is 0. Change it as follows:
|
78
|
+
|
79
|
+
Gchart.line(:title =>'bg example', :bg => {:color => 'efefef', :type => 'gradient', :angle => 90})
|
80
|
+
|
81
|
+
For a more advance use of colors, refer to http://code.google.com/apis/chart/#linear_gradient
|
82
|
+
|
83
|
+
Gchart.line(:bg => {:color => '76A4FB,1,ffffff,0', :type => 'gradient'})
|
84
|
+
|
85
|
+
|
86
|
+
The same way you set the background color, you can also set the graph background:
|
87
|
+
|
88
|
+
Gchart.line(:graph_bg => 'cccccc')
|
89
|
+
|
90
|
+
or both
|
91
|
+
|
92
|
+
Gchart.line(:bg => {:color => '76A4FB,1,ffffff,0', :type => 'gradient'}, :graph_bg => 'cccccc', :title => 'Sexy Chart')
|
93
|
+
|
94
|
+
|
95
|
+
Another type of fill is stripes http://code.google.com/apis/chart/#linear_stripes
|
96
|
+
|
97
|
+
Gchart.line(:bg => {:color => 'efefef', :type => 'stripes'})
|
98
|
+
|
99
|
+
You can customize the amount of stripes, colors and width by changing the color value.
|
100
|
+
|
101
|
+
|
102
|
+
== Legend & Labels
|
103
|
+
|
104
|
+
You probably will want to use a legend or labels for your graph.
|
105
|
+
|
106
|
+
Gchart.line(:legend => 'legend label')
|
107
|
+
or
|
108
|
+
Gchart.line(:legend => ['legend label 1', 'legend label 2'])
|
109
|
+
|
110
|
+
Will do the trick. You can also use the labels alias (makes more sense when using the pie charts)
|
111
|
+
|
112
|
+
chart = Gchart.pie(:labels => ['label 1', 'label 2'])
|
113
|
+
|
114
|
+
== Multiple axis labels
|
115
|
+
|
116
|
+
Multiple axis labels are available for line charts, bar charts and scatter plots.
|
117
|
+
|
118
|
+
* x = bottom x-axis
|
119
|
+
* t = top x-axis
|
120
|
+
* y = left y-axis
|
121
|
+
* r = right y-axis
|
122
|
+
|
123
|
+
Gchart.line(:label_axis => 'x,y,r')
|
124
|
+
|
125
|
+
To add labels on these axis:
|
126
|
+
|
127
|
+
Gchart.line(:axis_labels => ['Jan|July|Jan|July|Jan', '0|100', 'A|B|C', '2005|2006|2007'])
|
128
|
+
|
129
|
+
|
130
|
+
== Data options
|
131
|
+
|
132
|
+
Data are passed using an array or a nested array.
|
133
|
+
|
134
|
+
Gchart.bar(:data => [1,2,4,67,100,41,234])
|
135
|
+
|
136
|
+
Gchart.bar(:data => [[1,2,4,67,100,41,234],[45,23,67,12,67,300, 250]])
|
137
|
+
|
138
|
+
By default, the graph is drawn with your max value representing 100% of the height or width of the graph. You can change that my passing the max value.
|
139
|
+
|
140
|
+
Gchart.bar(:data => [1,2,4,67,100,41,234], :max_value => 300)
|
141
|
+
Gchart.bar(:data => [1,2,4,67,100,41,234], :max_value => 'auto')
|
142
|
+
|
143
|
+
or if you want to use the real values from your dataset:
|
144
|
+
|
145
|
+
Gchart.bar(:data => [1,2,4,67,100,41,234], :max_value => false)
|
146
|
+
|
147
|
+
|
148
|
+
You can also define a different encoding to add more granularity:
|
149
|
+
|
150
|
+
Gchart.bar(:data => [1,2,4,67,100,41,234], :encoding => 'simple')
|
151
|
+
Gchart.bar(:data => [1,2,4,67,100,41,234], :encoding => 'extended')
|
152
|
+
Gchart.bar(:data => [1,2,4,67,100,41,234], :encoding => 'text')
|
153
|
+
|
154
|
+
|
155
|
+
==Pies:
|
156
|
+
|
157
|
+
you have 2 type of pies:
|
158
|
+
- Gchart.pie() the standard 2D pie
|
159
|
+
_ Gchart.pie_3d() the fancy 3D pie
|
160
|
+
|
161
|
+
To set labels, you can use one of these two options:
|
162
|
+
|
163
|
+
@legend = ['Matt_fu', 'Rob_fu']
|
164
|
+
Gchart.pie_3d(:title => @title, :labels => @legend, :data => @data, :size => '400x200')
|
165
|
+
Gchart.pie_3d(:title => @title, :legend => @legend, :data => @data, :size => '400x200')
|
166
|
+
|
167
|
+
|
168
|
+
|
169
|
+
=== try yourself
|
170
|
+
|
171
|
+
Gchart.bar( :data => [[1,2,4,67,100,41,234],[45,23,67,12,67,300, 250]],
|
172
|
+
:title => 'SDRuby Fu level',
|
173
|
+
:legend => ['matt','patrick'],
|
174
|
+
:bg => {:color => '76A4FB', :type => 'gradient'},
|
175
|
+
:bar_colors => 'ff0000,00ff00')
|
176
|
+
|
177
|
+
"http://chart.apis.google.com/chart?chs=300x200&chdl=matt|patrick&chd=s:AAANUIv,JENCN9y&chtt=SDRuby+Fu+level&chf=bg,lg,0,76A4FB,0,ffffff,1&cht=bvs&chco=ff0000,00ff00"
|
178
|
+
|
179
|
+
Gchart.pie(:data => [20,10,15,5,50], :title => 'SDRuby Fu level', :size => '400x200', :labels => ['matt', 'rob', 'patrick', 'ryan', 'jordan'])
|
180
|
+
http://chart.apis.google.com/chart?cht=p&chs=400x200&chd=s:YMSG9&chtt=SDRuby+Fu+level&chl=matt|rob|patrick|ryan|jordan
|
data/Rakefile
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'config/requirements'
|
2
|
+
require 'config/hoe' # setup Hoe + all gem configuration
|
3
|
+
|
4
|
+
Dir['tasks/**/*.rake'].each { |rake| load rake }
|
5
|
+
|
6
|
+
desc %{Update ".manifest" with the latest list of project filenames. Respect\
|
7
|
+
.gitignore by excluding everything that git ignores. Update `files` and\
|
8
|
+
`test_files` arrays in "*.gemspec" file if it's present.}
|
9
|
+
task :manifest do
|
10
|
+
list = Dir['**/*'].sort
|
11
|
+
spec_file = Dir['*.gemspec'].first
|
12
|
+
list -= [spec_file] if spec_file
|
13
|
+
|
14
|
+
File.read('.gitignore').each_line do |glob|
|
15
|
+
glob = glob.chomp.sub(/^\//, '')
|
16
|
+
list -= Dir[glob]
|
17
|
+
list -= Dir["#{glob}/**/*"] if File.directory?(glob) and !File.symlink?(glob)
|
18
|
+
puts "excluding #{glob}"
|
19
|
+
end
|
20
|
+
|
21
|
+
if spec_file
|
22
|
+
spec = File.read spec_file
|
23
|
+
spec.gsub! /^(\s* s.(test_)?files \s* = \s* )( \[ [^\]]* \] | %w\( [^)]* \) )/mx do
|
24
|
+
assignment = $1
|
25
|
+
bunch = $2 ? list.grep(/^test\//) : list
|
26
|
+
'%s%%w(%s)' % [assignment, bunch.join(' ')]
|
27
|
+
end
|
28
|
+
|
29
|
+
File.open(spec_file, 'w') {|f| f << spec }
|
30
|
+
end
|
31
|
+
File.open('.manifest', 'w') {|f| f << list.join("\n") }
|
32
|
+
end
|
data/config/hoe.rb
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
require 'gchart/version'
|
2
|
+
|
3
|
+
AUTHOR = 'Matt Aimonetti' # can also be an array of Authors
|
4
|
+
EMAIL = "mattaimonetti@gmail.com"
|
5
|
+
DESCRIPTION = "description of gem"
|
6
|
+
GEM_NAME = 'googlecharts' # what ppl will type to install your gem
|
7
|
+
RUBYFORGE_PROJECT = 'googlecharts' # The unix name for your project
|
8
|
+
HOMEPATH = "http://#{RUBYFORGE_PROJECT}.rubyforge.org"
|
9
|
+
DOWNLOAD_PATH = "http://rubyforge.org/projects/#{RUBYFORGE_PROJECT}"
|
10
|
+
|
11
|
+
@config_file = "~/.rubyforge/user-config.yml"
|
12
|
+
@config = nil
|
13
|
+
RUBYFORGE_USERNAME = "matt_a"
|
14
|
+
def rubyforge_username
|
15
|
+
unless @config
|
16
|
+
begin
|
17
|
+
@config = YAML.load(File.read(File.expand_path(@config_file)))
|
18
|
+
rescue
|
19
|
+
puts <<-EOS
|
20
|
+
ERROR: No rubyforge config file found: #{@config_file}
|
21
|
+
Run 'rubyforge setup' to prepare your env for access to Rubyforge
|
22
|
+
- See http://newgem.rubyforge.org/rubyforge.html for more details
|
23
|
+
EOS
|
24
|
+
exit
|
25
|
+
end
|
26
|
+
end
|
27
|
+
RUBYFORGE_USERNAME.replace @config["username"]
|
28
|
+
end
|
29
|
+
|
30
|
+
|
31
|
+
REV = nil
|
32
|
+
# UNCOMMENT IF REQUIRED:
|
33
|
+
# REV = `svn info`.each {|line| if line =~ /^Revision:/ then k,v = line.split(': '); break v.chomp; else next; end} rescue nil
|
34
|
+
VERS = GchartInfo::VERSION::STRING + (REV ? ".#{REV}" : "")
|
35
|
+
RDOC_OPTS = ['--quiet', '--title', 'gchart documentation',
|
36
|
+
"--opname", "index.html",
|
37
|
+
"--line-numbers",
|
38
|
+
"--main", "README",
|
39
|
+
"--inline-source"]
|
40
|
+
|
41
|
+
class Hoe
|
42
|
+
def extra_deps
|
43
|
+
@extra_deps.reject! { |x| Array(x).first == 'hoe' }
|
44
|
+
@extra_deps
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
# Generate all the Rake tasks
|
49
|
+
# Run 'rake -T' to see list of generated tasks (from gem root directory)
|
50
|
+
hoe = Hoe.new(GEM_NAME, VERS) do |p|
|
51
|
+
p.author = AUTHOR
|
52
|
+
p.description = DESCRIPTION
|
53
|
+
p.email = EMAIL
|
54
|
+
p.summary = DESCRIPTION
|
55
|
+
p.url = HOMEPATH
|
56
|
+
p.rubyforge_name = RUBYFORGE_PROJECT if RUBYFORGE_PROJECT
|
57
|
+
p.test_globs = ["test/**/test_*.rb"]
|
58
|
+
p.clean_globs |= ['**/.*.sw?', '*.gem', '.config', '**/.DS_Store'] #An array of file patterns to delete on clean.
|
59
|
+
|
60
|
+
# == Optional
|
61
|
+
p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n")
|
62
|
+
#p.extra_deps = [] # An array of rubygem dependencies [name, version], e.g. [ ['active_support', '>= 1.3.1'] ]
|
63
|
+
|
64
|
+
#p.spec_extras = {} # A hash of extra values to set in the gemspec.
|
65
|
+
|
66
|
+
end
|
67
|
+
|
68
|
+
CHANGES = hoe.paragraphs_of('History.txt', 0..1).join("\\n\\n")
|
69
|
+
PATH = (RUBYFORGE_PROJECT == GEM_NAME) ? RUBYFORGE_PROJECT : "#{RUBYFORGE_PROJECT}/#{GEM_NAME}"
|
70
|
+
hoe.remote_rdoc_dir = File.join(PATH.gsub(/^#{RUBYFORGE_PROJECT}\/?/,''), 'rdoc')
|
71
|
+
hoe.rsync_args = '-av --delete --ignore-errors'
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'fileutils'
|
2
|
+
include FileUtils
|
3
|
+
|
4
|
+
require 'rubygems'
|
5
|
+
%w[rake hoe newgem rubigen].each do |req_gem|
|
6
|
+
begin
|
7
|
+
require req_gem
|
8
|
+
rescue LoadError
|
9
|
+
puts "This Rakefile could use '#{req_gem}' RubyGem."
|
10
|
+
puts "Installation: gem install #{req_gem} -y"
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
$:.unshift(File.join(File.dirname(__FILE__), %w[.. lib]))
|
15
|
+
|
16
|
+
require 'gchart'
|
data/lib/gchart.rb
ADDED
@@ -0,0 +1,392 @@
|
|
1
|
+
$:.unshift File.dirname(__FILE__)
|
2
|
+
require 'gchart/version'
|
3
|
+
require "open-uri"
|
4
|
+
require "uri"
|
5
|
+
|
6
|
+
class Gchart
|
7
|
+
|
8
|
+
include GchartInfo
|
9
|
+
|
10
|
+
@@url = "http://chart.apis.google.com/chart?"
|
11
|
+
@@types = ['line', 'line_xy', 'scatter', 'bar', 'venn', 'pie', 'pie_3d', 'jstize', 'sparkline', 'meter']
|
12
|
+
@@simple_chars = ('A'..'Z').to_a + ('a'..'z').to_a + ('0'..'9').to_a
|
13
|
+
@@chars = @@simple_chars + ['-', '.']
|
14
|
+
@@ext_pairs = @@chars.map { |char_1| @@chars.map { |char_2| char_1 + char_2 } }.flatten
|
15
|
+
@@file_name = 'chart.png'
|
16
|
+
|
17
|
+
attr_accessor :title, :type, :width, :height, :horizontal, :grouped, :legend, :data, :encoding, :max_value, :bar_colors,
|
18
|
+
:title_color, :title_size, :custom, :axis_with_labels, :axis_labels, :bar_width_and_spacing
|
19
|
+
|
20
|
+
class << self
|
21
|
+
# Support for Gchart.line(:title => 'my title', :size => '400x600')
|
22
|
+
def method_missing(m, options={})
|
23
|
+
# Extract the format and optional filename, then clean the hash
|
24
|
+
format = options[:format] || 'url'
|
25
|
+
@@file_name = options[:filename] unless options[:filename].nil?
|
26
|
+
options.delete(:format)
|
27
|
+
options.delete(:filename)
|
28
|
+
# create the chart and return it in the format asked for
|
29
|
+
if @@types.include?(m.to_s)
|
30
|
+
chart = new(options.merge!({:type => m}))
|
31
|
+
chart.send(format)
|
32
|
+
elsif m.to_s == 'version'
|
33
|
+
Gchart::VERSION::STRING
|
34
|
+
else
|
35
|
+
"#{m} is not a supported chart format, please use one of the following: #{supported_types}."
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
|
41
|
+
def initialize(options={})
|
42
|
+
@type = :line
|
43
|
+
@data = []
|
44
|
+
@width = 300
|
45
|
+
@height = 200
|
46
|
+
@horizontal = false
|
47
|
+
@grouped = false
|
48
|
+
@encoding = 'simple'
|
49
|
+
@max_value = 'auto'
|
50
|
+
|
51
|
+
# set the options value if definable
|
52
|
+
options.each do |attribute, value|
|
53
|
+
send("#{attribute.to_s}=", value) if self.respond_to?("#{attribute}=")
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def self.supported_types
|
58
|
+
@@types.join(' ')
|
59
|
+
end
|
60
|
+
|
61
|
+
# Defines the Graph size using the following format:
|
62
|
+
# width X height
|
63
|
+
def size=(size='300x200')
|
64
|
+
@width, @height = size.split("x").map { |dimension| dimension.to_i }
|
65
|
+
end
|
66
|
+
|
67
|
+
def size
|
68
|
+
"#{@width}x#{@height}"
|
69
|
+
end
|
70
|
+
|
71
|
+
# Sets the orientation of a bar graph
|
72
|
+
def orientation=(orientation='h')
|
73
|
+
if orientation == 'h' || orientation == 'horizontal'
|
74
|
+
self.horizontal = true
|
75
|
+
elsif orientation == 'v' || orientation == 'vertical'
|
76
|
+
self.horizontal = false
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
# Sets the bar graph presentation (stacked or grouped)
|
81
|
+
def stacked=(option=true)
|
82
|
+
@grouped = option ? false : true
|
83
|
+
end
|
84
|
+
|
85
|
+
def bg=(options)
|
86
|
+
if options.is_a?(String)
|
87
|
+
@bg_color = options
|
88
|
+
elsif options.is_a?(Hash)
|
89
|
+
@bg_color = options[:color]
|
90
|
+
@bg_type = options[:type]
|
91
|
+
@bg_angle = options[:angle]
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
def graph_bg=(options)
|
96
|
+
if options.is_a?(String)
|
97
|
+
@chart_color = options
|
98
|
+
elsif options.is_a?(Hash)
|
99
|
+
@chart_color = options[:color]
|
100
|
+
@chart_type = options[:type]
|
101
|
+
@chart_angle = options[:angle]
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
def self.jstize(string)
|
106
|
+
string.gsub(' ', '+').gsub(/\[|\{|\}|\||\\|\^|\[|\]|\`|\]/) {|c| "%#{c[0].to_s(16).upcase}"}
|
107
|
+
end
|
108
|
+
# load all the custom aliases
|
109
|
+
require 'gchart/aliases'
|
110
|
+
|
111
|
+
protected
|
112
|
+
|
113
|
+
# Returns the chart's generated PNG as a blob. (borrowed from John's gchart.rubyforge.org)
|
114
|
+
def fetch
|
115
|
+
open(query_builder) { |io| io.read }
|
116
|
+
end
|
117
|
+
|
118
|
+
# Writes the chart's generated PNG to a file. (borrowed from John's gchart.rubyforge.org)
|
119
|
+
def write(io_or_file=@@file_name)
|
120
|
+
return io_or_file.write(fetch) if io_or_file.respond_to?(:write)
|
121
|
+
open(io_or_file, "w+") { |io| io.write(fetch) }
|
122
|
+
end
|
123
|
+
|
124
|
+
# Format
|
125
|
+
|
126
|
+
def img_tag
|
127
|
+
"<img src='#{query_builder}'/>"
|
128
|
+
end
|
129
|
+
|
130
|
+
def url
|
131
|
+
query_builder
|
132
|
+
end
|
133
|
+
|
134
|
+
def file
|
135
|
+
write
|
136
|
+
end
|
137
|
+
|
138
|
+
#
|
139
|
+
def jstize(string)
|
140
|
+
Gchart.jstize(string)
|
141
|
+
end
|
142
|
+
|
143
|
+
private
|
144
|
+
|
145
|
+
# The title size cannot be set without specifying a color.
|
146
|
+
# A dark key will be used for the title color if no color is specified
|
147
|
+
def set_title
|
148
|
+
title_params = "chtt=#{title}"
|
149
|
+
unless (title_color.nil? && title_size.nil? )
|
150
|
+
title_params << "&chts=" + (color, size = (@title_color || '454545'), @title_size).compact.join(',')
|
151
|
+
end
|
152
|
+
title_params
|
153
|
+
end
|
154
|
+
|
155
|
+
def set_size
|
156
|
+
"chs=#{size}"
|
157
|
+
end
|
158
|
+
|
159
|
+
def set_data
|
160
|
+
data = send("#{@encoding}_encoding", @data)
|
161
|
+
"chd=#{data}"
|
162
|
+
end
|
163
|
+
|
164
|
+
def set_colors
|
165
|
+
bg_type = fill_type(@bg_type) || 's' if @bg_color
|
166
|
+
chart_type = fill_type(@chart_type) || 's' if @chart_color
|
167
|
+
|
168
|
+
"chf=" + {'bg' => fill_for(bg_type, @bg_color, @bg_angle), 'c' => fill_for(chart_type, @chart_color, @chart_angle)}.map{|k,v| "#{k},#{v}" unless v.nil?}.compact.join('|')
|
169
|
+
end
|
170
|
+
|
171
|
+
# set bar, line colors
|
172
|
+
def set_bar_colors
|
173
|
+
@bar_colors = @bar_colors.join(',') if @bar_colors.is_a?(Array)
|
174
|
+
"chco=#{@bar_colors}"
|
175
|
+
end
|
176
|
+
|
177
|
+
# set bar spacing
|
178
|
+
# chbh=
|
179
|
+
# <bar width in pixels>,
|
180
|
+
# <optional space between bars in a group>,
|
181
|
+
# <optional space between groups>
|
182
|
+
def set_bar_width_and_spacing
|
183
|
+
width_and_spacing_values = case @bar_width_and_spacing
|
184
|
+
when String
|
185
|
+
@bar_width_and_spacing
|
186
|
+
when Array
|
187
|
+
@bar_width_and_spacing.join(',')
|
188
|
+
when Hash
|
189
|
+
width = @bar_width_and_spacing[:width] || 23
|
190
|
+
spacing = @bar_width_and_spacing[:spacing] || 4
|
191
|
+
group_spacing = @bar_width_and_spacing[:group_spacing] || 8
|
192
|
+
[width,spacing,group_spacing].join(',')
|
193
|
+
else
|
194
|
+
@bar_width_and_spacing.to_s
|
195
|
+
end
|
196
|
+
"chbh=#{width_and_spacing_values}"
|
197
|
+
end
|
198
|
+
|
199
|
+
def fill_for(type=nil, color='', angle=nil)
|
200
|
+
unless type.nil?
|
201
|
+
case type
|
202
|
+
when 'lg'
|
203
|
+
angle ||= 0
|
204
|
+
color = "#{color},0,ffffff,1" if color.split(',').size == 1
|
205
|
+
"#{type},#{angle},#{color}"
|
206
|
+
when 'ls'
|
207
|
+
angle ||= 90
|
208
|
+
color = "#{color},0.2,ffffff,0.2" if color.split(',').size == 1
|
209
|
+
"#{type},#{angle},#{color}"
|
210
|
+
else
|
211
|
+
"#{type},#{color}"
|
212
|
+
end
|
213
|
+
end
|
214
|
+
end
|
215
|
+
|
216
|
+
# A chart can have one or many legends.
|
217
|
+
# Gchart.line(:legend => 'label')
|
218
|
+
# or
|
219
|
+
# Gchart.line(:legend => ['first label', 'last label'])
|
220
|
+
def set_legend
|
221
|
+
return set_labels if @type == :pie || @type == :pie_3d
|
222
|
+
|
223
|
+
if @legend.is_a?(Array)
|
224
|
+
"chdl=#{@legend.map{|label| "#{label}"}.join('|')}"
|
225
|
+
else
|
226
|
+
"chdl=#{@legend}"
|
227
|
+
end
|
228
|
+
|
229
|
+
end
|
230
|
+
|
231
|
+
def set_labels
|
232
|
+
if @legend.is_a?(Array)
|
233
|
+
"chl=#{@legend.map{|label| "#{label}"}.join('|')}"
|
234
|
+
else
|
235
|
+
"chl=#{@legend}"
|
236
|
+
end
|
237
|
+
end
|
238
|
+
|
239
|
+
def set_axis_with_labels
|
240
|
+
@axis_with_labels = @axis_with_labels.join(',') if @axis_with_labels.is_a?(Array)
|
241
|
+
"chxt=#{@axis_with_labels}"
|
242
|
+
end
|
243
|
+
|
244
|
+
def set_axis_labels
|
245
|
+
labels_arr = []
|
246
|
+
axis_labels.each_with_index do |labels,index|
|
247
|
+
if labels.is_a?(Array)
|
248
|
+
labels_arr << "#{index}:|#{labels.join('|')}"
|
249
|
+
else
|
250
|
+
labels_arr << "#{index}:|#{labels}"
|
251
|
+
end
|
252
|
+
end
|
253
|
+
"chxl=#{labels_arr.join('|')}"
|
254
|
+
end
|
255
|
+
|
256
|
+
def set_type
|
257
|
+
case @type
|
258
|
+
when :line
|
259
|
+
"cht=lc"
|
260
|
+
when :line_xy
|
261
|
+
"cht=lxy"
|
262
|
+
when :bar
|
263
|
+
"cht=b" + (horizontal? ? "h" : "v") + (grouped? ? "g" : "s")
|
264
|
+
when :pie_3d
|
265
|
+
"cht=p3"
|
266
|
+
when :pie
|
267
|
+
"cht=p"
|
268
|
+
when :venn
|
269
|
+
"cht=v"
|
270
|
+
when :scatter
|
271
|
+
"cht=s"
|
272
|
+
when :sparkline
|
273
|
+
"cht=ls"
|
274
|
+
when :meter
|
275
|
+
"cht=gom"
|
276
|
+
end
|
277
|
+
end
|
278
|
+
|
279
|
+
def fill_type(type)
|
280
|
+
case type
|
281
|
+
when 'solid'
|
282
|
+
's'
|
283
|
+
when 'gradient'
|
284
|
+
'lg'
|
285
|
+
when 'stripes'
|
286
|
+
'ls'
|
287
|
+
end
|
288
|
+
end
|
289
|
+
|
290
|
+
# Wraps a single dataset inside another array to support more datasets
|
291
|
+
def prepare_dataset(ds)
|
292
|
+
ds = [ds] unless ds.first.is_a?(Array)
|
293
|
+
ds
|
294
|
+
end
|
295
|
+
|
296
|
+
def convert_to_simple_value(number)
|
297
|
+
if number.nil?
|
298
|
+
"_"
|
299
|
+
else
|
300
|
+
value = @@simple_chars[number.to_i]
|
301
|
+
value.nil? ? "_" : value
|
302
|
+
end
|
303
|
+
end
|
304
|
+
|
305
|
+
# http://code.google.com/apis/chart/#simple
|
306
|
+
# Simple encoding has a resolution of 62 different values.
|
307
|
+
# Allowing five pixels per data point, this is sufficient for line and bar charts up
|
308
|
+
# to about 300 pixels. Simple encoding is suitable for all other types of chart regardless of size.
|
309
|
+
def simple_encoding(dataset=[])
|
310
|
+
dataset = prepare_dataset(dataset)
|
311
|
+
@max_value = dataset.map{|ds| ds.max}.max if @max_value == 'auto'
|
312
|
+
|
313
|
+
if @max_value == false || @max_value == 'false' || @max_value == :false || @max_value == 0
|
314
|
+
"s:" + dataset.map { |ds| ds.map { |number| convert_to_simple_value(number) }.join }.join(',')
|
315
|
+
else
|
316
|
+
"s:" + dataset.map { |ds| ds.map { |number| convert_to_simple_value( (@@simple_chars.size - 1) * number / @max_value) }.join }.join(',')
|
317
|
+
end
|
318
|
+
|
319
|
+
end
|
320
|
+
|
321
|
+
# http://code.google.com/apis/chart/#text
|
322
|
+
# Text encoding has a resolution of 1,000 different values,
|
323
|
+
# using floating point numbers between 0.0 and 100.0. Allowing five pixels per data point,
|
324
|
+
# integers (1.0, 2.0, and so on) are sufficient for line and bar charts up to about 500 pixels.
|
325
|
+
# Include a single decimal place (35.7 for example) if you require higher resolution.
|
326
|
+
# Text encoding is suitable for all other types of chart regardless of size.
|
327
|
+
def text_encoding(dataset=[])
|
328
|
+
dataset = prepare_dataset(dataset)
|
329
|
+
"t:" + dataset.map{ |ds| ds.join(',') }.join('|')
|
330
|
+
end
|
331
|
+
|
332
|
+
def convert_to_extended_value(number)
|
333
|
+
if number.nil?
|
334
|
+
'__'
|
335
|
+
else
|
336
|
+
value = @@ext_pairs[number.to_i]
|
337
|
+
value.nil? ? "__" : value
|
338
|
+
end
|
339
|
+
end
|
340
|
+
|
341
|
+
# http://code.google.com/apis/chart/#extended
|
342
|
+
# Extended encoding has a resolution of 4,096 different values
|
343
|
+
# and is best used for large charts where a large data range is required.
|
344
|
+
def extended_encoding(dataset=[])
|
345
|
+
|
346
|
+
dataset = prepare_dataset(dataset)
|
347
|
+
@max_value = dataset.map{|ds| ds.max}.max if @max_value == 'auto'
|
348
|
+
|
349
|
+
if @max_value == false || @max_value == 'false' || @max_value == :false
|
350
|
+
"e:" + dataset.map { |ds| ds.map { |number| convert_to_extended_value(number)}.join }.join(',')
|
351
|
+
else
|
352
|
+
"e:" + dataset.map { |ds| ds.map { |number| convert_to_extended_value( (@@ext_pairs.size - 1) * number / @max_value) }.join }.join(',')
|
353
|
+
end
|
354
|
+
|
355
|
+
end
|
356
|
+
|
357
|
+
|
358
|
+
def query_builder
|
359
|
+
query_params = instance_variables.map do |var|
|
360
|
+
case var
|
361
|
+
# Set the graph size
|
362
|
+
when '@width'
|
363
|
+
set_size unless @width.nil? || @height.nil?
|
364
|
+
when '@type'
|
365
|
+
set_type
|
366
|
+
when '@title'
|
367
|
+
set_title unless @title.nil?
|
368
|
+
when '@legend'
|
369
|
+
set_legend unless @legend.nil?
|
370
|
+
when '@bg_color'
|
371
|
+
set_colors
|
372
|
+
when '@chart_color'
|
373
|
+
set_colors if @bg_color.nil?
|
374
|
+
when '@data'
|
375
|
+
set_data unless @data == []
|
376
|
+
when '@bar_colors'
|
377
|
+
set_bar_colors
|
378
|
+
when '@bar_width_and_spacing'
|
379
|
+
set_bar_width_and_spacing
|
380
|
+
when '@axis_with_labels'
|
381
|
+
set_axis_with_labels
|
382
|
+
when '@axis_labels'
|
383
|
+
set_axis_labels
|
384
|
+
when '@custom'
|
385
|
+
@custom
|
386
|
+
end
|
387
|
+
end.compact
|
388
|
+
|
389
|
+
jstize(@@url + query_params.join('&'))
|
390
|
+
end
|
391
|
+
|
392
|
+
end
|