google_charts 1.2.0 → 2.0.0.alpha.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +84 -0
- data/init.rb +0 -7
- data/lib/google_charts.rb +22 -1
- data/lib/google_charts/charts/base.rb +3 -3
- data/lib/google_charts/charts/line.rb +3 -3
- data/lib/google_charts/charts/pie.rb +4 -4
- data/lib/google_charts/version.rb +2 -1
- metadata +8 -11
- data/README.rdoc +0 -59
- data/lib/google_charts/helpers.rb +0 -7
- data/lib/google_charts/helpers/action_view.rb +0 -26
data/README.md
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
GoogleCharts is a ruby wrapper to the Google Chart API (http://code.google.com/apis/charttools/)
|
2
|
+
|
3
|
+
## Summary
|
4
|
+
|
5
|
+
For now, the plugin supports the following charts:
|
6
|
+
- pie chart
|
7
|
+
- line chart
|
8
|
+
- bar chart
|
9
|
+
- column chart
|
10
|
+
- area chart
|
11
|
+
|
12
|
+
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...
|
13
|
+
|
14
|
+
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
|
15
|
+
|
16
|
+
|
17
|
+
## Installation
|
18
|
+
|
19
|
+
System wide:
|
20
|
+
|
21
|
+
```console
|
22
|
+
gem install google_charts
|
23
|
+
```
|
24
|
+
|
25
|
+
Or in your Gemfile:
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
gem 'google_charts'
|
29
|
+
```
|
30
|
+
|
31
|
+
|
32
|
+
## Usage
|
33
|
+
|
34
|
+
GoogleCharts is based on a collection of elements. Usually they are records from the database, but can also just be an array.
|
35
|
+
|
36
|
+
The following **Stock** class is a Mongoid document and used to keep track of how much we have of any product in our online store. All we need is the product's name and the amount. For simplicity, we'll skip any default values or validations.
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
class Stock
|
40
|
+
include Mongoid::Document
|
41
|
+
|
42
|
+
field :name, type: String
|
43
|
+
field :amount, type: Integer
|
44
|
+
end
|
45
|
+
```
|
46
|
+
|
47
|
+
Now, let's supply some data for our store.
|
48
|
+
|
49
|
+
```ruby
|
50
|
+
Stock.create name: "Apple", amount: 10
|
51
|
+
Stock.create name: "Pear", amount: 5
|
52
|
+
Stock.create name: "Banana", amount: 1
|
53
|
+
```
|
54
|
+
|
55
|
+
### Example: Pie chart
|
56
|
+
|
57
|
+
In order to figure out how much we have of any product, all we need to do is:
|
58
|
+
|
59
|
+
```erb
|
60
|
+
<%= pie_chart Stock.all do |c| %>
|
61
|
+
<% c.title "Total Stock" %>
|
62
|
+
|
63
|
+
<% c.label "Name", :name %>
|
64
|
+
<% c.value "Amount", :amount %>
|
65
|
+
<% end %>
|
66
|
+
```
|
67
|
+
|
68
|
+
### Example: Pie chart with block parameters for label and value
|
69
|
+
|
70
|
+
In order to dynamically display labels or values within a chart, you may also pass a block:
|
71
|
+
|
72
|
+
```erb
|
73
|
+
<%= pie_chart Stock.all do |c| %>
|
74
|
+
<% c.title "Total Stock" %>
|
75
|
+
|
76
|
+
<% c.label("Name") { |s| "#{s.name} (percent)" } %>
|
77
|
+
<% c.value("Percentage") { |s| s.amount / Stock.sum(:amount) * 100 } %>
|
78
|
+
<% end %>
|
79
|
+
|
80
|
+
```
|
81
|
+
NOTE: I know that Stock.sum(:amount) is not good practice, but it serves the example.
|
82
|
+
|
83
|
+
|
84
|
+
Copyright © 2010 - 2012 Rudolf Schmidt, released under the MIT license.
|
data/init.rb
CHANGED
data/lib/google_charts.rb
CHANGED
@@ -1,8 +1,29 @@
|
|
1
1
|
module GoogleCharts
|
2
2
|
|
3
3
|
autoload :Charts, File.dirname(__FILE__) + "/google_charts/charts"
|
4
|
-
|
4
|
+
|
5
|
+
module Helpers
|
6
|
+
def self.define( name, options = {} )
|
7
|
+
helper_name = [options[:prefix], name, options[:suffix]||'chart'].compact.join( '_' )
|
8
|
+
|
9
|
+
module_eval <<-DEF
|
10
|
+
def #{helper_name}( *options )
|
11
|
+
chart = GoogleCharts::Charts::#{name.to_s.capitalize}.new( self, *options )
|
12
|
+
yield chart if block_given?
|
13
|
+
|
14
|
+
chart.to_html
|
15
|
+
end
|
16
|
+
DEF
|
17
|
+
end
|
18
|
+
|
19
|
+
define :line
|
20
|
+
define :pie
|
21
|
+
define :area
|
22
|
+
define :bar
|
23
|
+
define :column
|
24
|
+
end
|
5
25
|
|
6
26
|
end
|
7
27
|
|
28
|
+
ActionView::Base.send :include, GoogleCharts::Helpers
|
8
29
|
|
@@ -2,12 +2,12 @@ module GoogleCharts::Charts
|
|
2
2
|
|
3
3
|
class Base
|
4
4
|
|
5
|
-
def initialize( template, collection, options = {}
|
5
|
+
def initialize( template, collection, options = {} )
|
6
6
|
@template = template
|
7
7
|
@collection = collection
|
8
8
|
|
9
9
|
@options = options
|
10
|
-
@html_options = { :id => "googleChart" }.merge(
|
10
|
+
@html_options = { :id => "googleChart" }.merge( options.delete(:html) || {} )
|
11
11
|
|
12
12
|
@columns, @rows = [], []
|
13
13
|
end
|
@@ -21,7 +21,7 @@ module GoogleCharts::Charts
|
|
21
21
|
google_jsapi,
|
22
22
|
container_div,
|
23
23
|
google_chart
|
24
|
-
].join("\n")
|
24
|
+
].join("\n").html_safe
|
25
25
|
end
|
26
26
|
|
27
27
|
|
@@ -1,14 +1,14 @@
|
|
1
1
|
module GoogleCharts::Charts
|
2
2
|
|
3
3
|
class Line < GoogleCharts::Charts::Base
|
4
|
-
def initialize( template, collection, options = {}
|
4
|
+
def initialize( template, collection, options = {} )
|
5
5
|
super
|
6
6
|
|
7
7
|
@label, @values = [], []
|
8
8
|
end
|
9
9
|
|
10
|
-
def label(
|
11
|
-
def value(
|
10
|
+
def label(name, method = nil, &block); @label = [name, block ? block : method]; end
|
11
|
+
def value(name, method = nil, &block); @values << [name, block ? block : method]; end
|
12
12
|
|
13
13
|
|
14
14
|
private
|
@@ -1,14 +1,14 @@
|
|
1
1
|
module GoogleCharts::Charts
|
2
2
|
|
3
3
|
class Pie < GoogleCharts::Charts::Base
|
4
|
-
def initialize( template, collection, options = {}
|
4
|
+
def initialize( template, collection, options = {} )
|
5
5
|
super
|
6
6
|
|
7
7
|
@label, @value = [], []
|
8
8
|
end
|
9
9
|
|
10
|
-
def label(
|
11
|
-
def value(
|
10
|
+
def label(name, method = nil, &block); @label = [name, block ? block : method]; end
|
11
|
+
def value(name, method = nil, &block); @value = [name, block ? block : method]; end
|
12
12
|
|
13
13
|
|
14
14
|
private
|
@@ -20,7 +20,7 @@ module GoogleCharts::Charts
|
|
20
20
|
|
21
21
|
# setup the rows
|
22
22
|
@collection.each do |col|
|
23
|
-
add_row( [value_for(
|
23
|
+
add_row( [value_for(@label.last, col), value_for(@value.last, col)] )
|
24
24
|
end
|
25
25
|
end
|
26
26
|
end
|
metadata
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: google_charts
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
prerelease:
|
5
|
-
version:
|
4
|
+
prerelease: 6
|
5
|
+
version: 2.0.0.alpha.1
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Rudolf Schmidt
|
@@ -10,8 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date:
|
14
|
-
default_executable:
|
13
|
+
date: 2012-02-09 00:00:00 Z
|
15
14
|
dependencies:
|
16
15
|
- !ruby/object:Gem::Dependency
|
17
16
|
name: actionpack
|
@@ -36,7 +35,7 @@ files:
|
|
36
35
|
- .gitignore
|
37
36
|
- Gemfile
|
38
37
|
- LICENSE.txt
|
39
|
-
- README.
|
38
|
+
- README.md
|
40
39
|
- Rakefile
|
41
40
|
- google_charts.gemspec
|
42
41
|
- init.rb
|
@@ -48,10 +47,7 @@ files:
|
|
48
47
|
- lib/google_charts/charts/column.rb
|
49
48
|
- lib/google_charts/charts/line.rb
|
50
49
|
- lib/google_charts/charts/pie.rb
|
51
|
-
- lib/google_charts/helpers.rb
|
52
|
-
- lib/google_charts/helpers/action_view.rb
|
53
50
|
- lib/google_charts/version.rb
|
54
|
-
has_rdoc: true
|
55
51
|
homepage: http://github.com/rudionrails/google_charts
|
56
52
|
licenses: []
|
57
53
|
|
@@ -69,15 +65,16 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
69
65
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
70
66
|
none: false
|
71
67
|
requirements:
|
72
|
-
- - "
|
68
|
+
- - ">"
|
73
69
|
- !ruby/object:Gem::Version
|
74
|
-
version:
|
70
|
+
version: 1.3.1
|
75
71
|
requirements: []
|
76
72
|
|
77
73
|
rubyforge_project: google_charts
|
78
|
-
rubygems_version: 1.
|
74
|
+
rubygems_version: 1.8.9
|
79
75
|
signing_key:
|
80
76
|
specification_version: 3
|
81
77
|
summary: Google Charts with Ruby
|
82
78
|
test_files: []
|
83
79
|
|
80
|
+
has_rdoc:
|
data/README.rdoc
DELETED
@@ -1,59 +0,0 @@
|
|
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
|
-
gem install google_charts
|
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.
|
@@ -1,26 +0,0 @@
|
|
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
|