google_charts 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +7 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +19 -0
- data/README.rdoc +59 -0
- data/Rakefile +4 -0
- data/google_charts.gemspec +23 -0
- data/init.rb +9 -0
- data/lib/google_charts.rb +8 -0
- data/lib/google_charts/charts.rb +12 -0
- data/lib/google_charts/charts/area.rb +7 -0
- data/lib/google_charts/charts/bar.rb +7 -0
- data/lib/google_charts/charts/base.rb +97 -0
- data/lib/google_charts/charts/column.rb +7 -0
- data/lib/google_charts/charts/line.rb +32 -0
- data/lib/google_charts/charts/pie.rb +28 -0
- data/lib/google_charts/helpers.rb +7 -0
- data/lib/google_charts/helpers/action_view.rb +26 -0
- data/lib/google_charts/version.rb +3 -0
- metadata +98 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
Copyright (c) 2010 Rudolf Schmidt
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
5
|
+
in the Software without restriction, including without limitation the rights
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
8
|
+
furnished to do so, subject to the following conditions:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
11
|
+
all copies or substantial portions of the Software.
|
12
|
+
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
= GoogleCharts
|
2
|
+
|
3
|
+
GoogleCharts is a ruby wrapper to the Google Chart API (http://code.google.com/apis/charttools/)
|
4
|
+
|
5
|
+
== Summary
|
6
|
+
For now, the plugin supports the following charts:
|
7
|
+
- pie chart
|
8
|
+
- line chart
|
9
|
+
- bar chart
|
10
|
+
- column chart
|
11
|
+
- area chart
|
12
|
+
|
13
|
+
Basically, you can give the chart all the options you would give a GoogleChart when using the Google library: height, width, title, and so on...
|
14
|
+
|
15
|
+
For a detailed description of which options to use visit the Google Visualization API and check out the charts there: http://code.google.com/apis/visualization/documentation/gallery.html
|
16
|
+
|
17
|
+
|
18
|
+
== Installation
|
19
|
+
script/plugin install git://github.com/rudionrails/google_charts.git
|
20
|
+
|
21
|
+
|
22
|
+
== Usage
|
23
|
+
GoogleCharts is based on a collection of elements (usually records from the database). They do not need to be ActiveRecord collections, simple arrays will do as well.
|
24
|
+
|
25
|
+
For a simple pie chart, simply type this in your view:
|
26
|
+
<% pie_chart( [ [:eggs, 3], [:bacon, 1] ], { :height => 300, :is3D => true }, { :id => "breakfastChart" } ) do |c| -%>
|
27
|
+
<% c.title "breakfast" %>
|
28
|
+
|
29
|
+
<% c.label "ingredient", :first %>
|
30
|
+
<% c.value "amount", :last %>
|
31
|
+
<% end -%>
|
32
|
+
|
33
|
+
As you may imagine, the pie_chart cycles through the collection [ [:eggs, 3], [:bacon, 1] ]. In this example, every item is itself an array again which have a :first and :last method in ruby. Those methods are called.
|
34
|
+
|
35
|
+
|
36
|
+
Now you may ask yourself: thats nice, but how can I get labels or values in my pie chart that are not direct methods on the collection elements? Have no fear, lamda is our friend in this case. The same pie chart can be visualized by calling those methods differently:
|
37
|
+
<% pie_chart( [ [:eggs, 3], [:bacon, 1] ], { :height => 300, :is3D => true }, { :id => "breakfastChart" } ) do |c| -%>
|
38
|
+
<% c.title "breakfast" %>
|
39
|
+
|
40
|
+
<% c.label "ingredient", lambda { |e| e[0] } %>
|
41
|
+
<% c.value "amount", lambda { |e| e[1] } %>
|
42
|
+
<% end -%>
|
43
|
+
|
44
|
+
Now we get exactly the same chart, but it's been called in a different way via lamda. Like this, you can use view helpers such as number_to_currency, or any fancy helper method that will give you what you need.
|
45
|
+
|
46
|
+
|
47
|
+
Here's another example. Once again we have 3 eggs and 1 bacon, but now there's a price to the amounts which we can use in the displayed label:
|
48
|
+
<% pie_chart( [ [:eggs, 1.99, 3], [:bacon, 3.99, 1] ], { :height => 300, :is3D => true }, { :id => "breakfastChart" } ) do |c| -%>
|
49
|
+
<% c.title "breakfast" %>
|
50
|
+
|
51
|
+
<% c.label "ingredient", lambda { |e| "#{e.last} #{e.first} for #{number_to_currency(e[1])}" } %>
|
52
|
+
<% c.value "amount", :last %>
|
53
|
+
<% end -%>
|
54
|
+
|
55
|
+
|
56
|
+
One important thing to mention is, that you should always give the chart an :id. It will get a defaut "googleChart" id, but it may not work well for multiple charts on one page. Also, for charts that support 3D, you can always add the :is3D option (see examples above). All charts are interactive, there are no image charts as of now.
|
57
|
+
|
58
|
+
|
59
|
+
Copyright (c) 2010 - 2011 Rudolf Schmidt, released under the MIT license.
|
data/Rakefile
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "google_charts/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "google_charts"
|
7
|
+
s.version = GoogleCharts::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Rudolf Schmidt"]
|
10
|
+
|
11
|
+
s.homepage = ""
|
12
|
+
s.summary = %q{Google Charts with Ruby}
|
13
|
+
s.description = %q{GoogleCharts is a Ruby wrapper to the Google Chart API}
|
14
|
+
|
15
|
+
s.rubyforge_project = "google_charts"
|
16
|
+
|
17
|
+
s.files = `git ls-files`.split("\n")
|
18
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
19
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
20
|
+
s.require_paths = ["lib"]
|
21
|
+
|
22
|
+
s.add_dependency "actionpack", "~> 2.x"
|
23
|
+
end
|
data/init.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
module GoogleCharts
|
2
|
+
module Charts
|
3
|
+
|
4
|
+
autoload :Base, File.dirname(__FILE__) + "/charts/base"
|
5
|
+
autoload :Pie, File.dirname(__FILE__) + "/charts/pie"
|
6
|
+
autoload :Line, File.dirname(__FILE__) + "/charts/line"
|
7
|
+
autoload :Area, File.dirname(__FILE__) + "/charts/area"
|
8
|
+
autoload :Bar, File.dirname(__FILE__) + "/charts/bar"
|
9
|
+
autoload :Column, File.dirname(__FILE__) + "/charts/column"
|
10
|
+
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,97 @@
|
|
1
|
+
module GoogleCharts::Charts
|
2
|
+
|
3
|
+
class Base
|
4
|
+
|
5
|
+
def initialize( template, collection, options = {}, html_options = {} )
|
6
|
+
@template = template
|
7
|
+
@collection = collection
|
8
|
+
|
9
|
+
@options = options
|
10
|
+
@html_options = { :id => "googleChart" }.merge( html_options )
|
11
|
+
|
12
|
+
@columns, @rows = [], []
|
13
|
+
end
|
14
|
+
|
15
|
+
def title( val ); @options[:title] = val; end
|
16
|
+
|
17
|
+
def to_html
|
18
|
+
setup_data
|
19
|
+
|
20
|
+
[
|
21
|
+
google_jsapi,
|
22
|
+
container_div,
|
23
|
+
google_chart
|
24
|
+
].join("\n")
|
25
|
+
end
|
26
|
+
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
def packages; ['corechart'].to_json end
|
31
|
+
|
32
|
+
def value_for( method_or_proc, obj )
|
33
|
+
method_or_proc.is_a?( Proc ) ? method_or_proc.call( obj ) : obj.send( method_or_proc )
|
34
|
+
end
|
35
|
+
|
36
|
+
# stub
|
37
|
+
def setup_data; end
|
38
|
+
|
39
|
+
# === columns and rows
|
40
|
+
def columns
|
41
|
+
@columns.join( "\n" )
|
42
|
+
end
|
43
|
+
|
44
|
+
def add_column( type, value )
|
45
|
+
@columns << "data.addColumn( '#{type}', '#{value}' );"
|
46
|
+
end
|
47
|
+
|
48
|
+
def rows
|
49
|
+
[
|
50
|
+
"data.addRows([ ",
|
51
|
+
@rows.join( ", \n" ),
|
52
|
+
"]);"
|
53
|
+
].join( "\n" )
|
54
|
+
end
|
55
|
+
|
56
|
+
def add_row( value )
|
57
|
+
@rows << value.to_json
|
58
|
+
end
|
59
|
+
|
60
|
+
def google_jsapi
|
61
|
+
@template.javascript_tag(
|
62
|
+
<<-EOS
|
63
|
+
// if( typeof google == 'undefined' ) {
|
64
|
+
document.write(unescape("%3Cscript src='http://www.google.com/jsapi' type='text/javascript'%3E%3C/script%3E"));
|
65
|
+
// };
|
66
|
+
EOS
|
67
|
+
)
|
68
|
+
end
|
69
|
+
|
70
|
+
def google_chart
|
71
|
+
@template.javascript_tag(
|
72
|
+
<<-EOS
|
73
|
+
google.load( "visualization", "1", { packages: #{packages} } );
|
74
|
+
google.setOnLoadCallback( function() {
|
75
|
+
var data = new google.visualization.DataTable();
|
76
|
+
|
77
|
+
#{columns}
|
78
|
+
#{rows}
|
79
|
+
|
80
|
+
var chart = new google.visualization.#{google_visualization_class}( document.getElementById('#{@html_options[:id]}') );
|
81
|
+
chart.draw( data, #{@options.to_json} );
|
82
|
+
});
|
83
|
+
EOS
|
84
|
+
)
|
85
|
+
end
|
86
|
+
|
87
|
+
def google_visualization_class
|
88
|
+
self.class.name.split( "::" ).last + 'Chart'
|
89
|
+
end
|
90
|
+
|
91
|
+
def container_div
|
92
|
+
@template.content_tag( :div, "", :id => @html_options[:id] )
|
93
|
+
end
|
94
|
+
|
95
|
+
end
|
96
|
+
|
97
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
module GoogleCharts::Charts
|
2
|
+
|
3
|
+
class Line < GoogleCharts::Charts::Base
|
4
|
+
def initialize( template, collection, options = {}, html_options = {} )
|
5
|
+
super
|
6
|
+
|
7
|
+
@label, @values = [], []
|
8
|
+
end
|
9
|
+
|
10
|
+
def label( name, method ); @label = [name, method]; end
|
11
|
+
def value( name, method ); @values << [name, method]; end
|
12
|
+
|
13
|
+
|
14
|
+
private
|
15
|
+
|
16
|
+
def setup_data
|
17
|
+
# setup the columns
|
18
|
+
add_column( 'string', @label.first )
|
19
|
+
@values.each { |val| add_column( 'number', val.first ) }
|
20
|
+
|
21
|
+
# setup the rows
|
22
|
+
@collection.each do |col|
|
23
|
+
label = value_for( @label.last, col )
|
24
|
+
values = @values.map { |value| value_for( value.last, col ) }
|
25
|
+
|
26
|
+
add_row( [label, *values] )
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module GoogleCharts::Charts
|
2
|
+
|
3
|
+
class Pie < GoogleCharts::Charts::Base
|
4
|
+
def initialize( template, collection, options = {}, html_options = {} )
|
5
|
+
super
|
6
|
+
|
7
|
+
@label, @value = [], []
|
8
|
+
end
|
9
|
+
|
10
|
+
def label( name, method ); @label = [name, method]; end
|
11
|
+
def value( name, method ); @value = [name, method]; end
|
12
|
+
|
13
|
+
|
14
|
+
private
|
15
|
+
|
16
|
+
def setup_data
|
17
|
+
# setup the columns
|
18
|
+
add_column( 'string', @label.first )
|
19
|
+
add_column( 'number', @value.first )
|
20
|
+
|
21
|
+
# setup the rows
|
22
|
+
@collection.each do |col|
|
23
|
+
add_row( [value_for( @label.last, col ), value_for( @value.last, col )] )
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module GoogleCharts::Helpers
|
2
|
+
|
3
|
+
module ActionView
|
4
|
+
|
5
|
+
def self.define_helper( name, options = {} )
|
6
|
+
helper_name = [options[:prefix], name, options[:suffix]||'chart'].compact.join( '_' )
|
7
|
+
|
8
|
+
module_eval <<-DEF
|
9
|
+
def #{helper_name}( *options )
|
10
|
+
chart = GoogleCharts::Charts::#{name.to_s.capitalize}.new( self, *options )
|
11
|
+
yield chart if block_given?
|
12
|
+
|
13
|
+
concat chart.to_html
|
14
|
+
end
|
15
|
+
DEF
|
16
|
+
end
|
17
|
+
|
18
|
+
define_helper :line
|
19
|
+
define_helper :pie
|
20
|
+
define_helper :area
|
21
|
+
define_helper :bar
|
22
|
+
define_helper :column
|
23
|
+
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: google_charts
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 23
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
- 0
|
10
|
+
version: 1.0.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Rudolf Schmidt
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-03-22 00:00:00 +01:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: actionpack
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 121
|
30
|
+
segments:
|
31
|
+
- 2
|
32
|
+
- x
|
33
|
+
version: 2.x
|
34
|
+
type: :runtime
|
35
|
+
version_requirements: *id001
|
36
|
+
description: GoogleCharts is a Ruby wrapper to the Google Chart API
|
37
|
+
email:
|
38
|
+
executables: []
|
39
|
+
|
40
|
+
extensions: []
|
41
|
+
|
42
|
+
extra_rdoc_files: []
|
43
|
+
|
44
|
+
files:
|
45
|
+
- .gitignore
|
46
|
+
- Gemfile
|
47
|
+
- LICENSE.txt
|
48
|
+
- README.rdoc
|
49
|
+
- Rakefile
|
50
|
+
- google_charts.gemspec
|
51
|
+
- init.rb
|
52
|
+
- lib/google_charts.rb
|
53
|
+
- lib/google_charts/charts.rb
|
54
|
+
- lib/google_charts/charts/area.rb
|
55
|
+
- lib/google_charts/charts/bar.rb
|
56
|
+
- lib/google_charts/charts/base.rb
|
57
|
+
- lib/google_charts/charts/column.rb
|
58
|
+
- lib/google_charts/charts/line.rb
|
59
|
+
- lib/google_charts/charts/pie.rb
|
60
|
+
- lib/google_charts/helpers.rb
|
61
|
+
- lib/google_charts/helpers/action_view.rb
|
62
|
+
- lib/google_charts/version.rb
|
63
|
+
has_rdoc: true
|
64
|
+
homepage: ""
|
65
|
+
licenses: []
|
66
|
+
|
67
|
+
post_install_message:
|
68
|
+
rdoc_options: []
|
69
|
+
|
70
|
+
require_paths:
|
71
|
+
- lib
|
72
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ">="
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
hash: 3
|
78
|
+
segments:
|
79
|
+
- 0
|
80
|
+
version: "0"
|
81
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
82
|
+
none: false
|
83
|
+
requirements:
|
84
|
+
- - ">="
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
hash: 3
|
87
|
+
segments:
|
88
|
+
- 0
|
89
|
+
version: "0"
|
90
|
+
requirements: []
|
91
|
+
|
92
|
+
rubyforge_project: google_charts
|
93
|
+
rubygems_version: 1.5.0
|
94
|
+
signing_key:
|
95
|
+
specification_version: 3
|
96
|
+
summary: Google Charts with Ruby
|
97
|
+
test_files: []
|
98
|
+
|