wayneeseguin-dynamic_reports 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/HISTORY +7 -0
- data/README +189 -0
- data/README.rdoc +189 -0
- data/dynamic_reports.gemspec +62 -0
- data/lib/dynamic_reports/charts.rb +217 -0
- data/lib/dynamic_reports/reports.rb +268 -0
- data/lib/dynamic_reports/templates.rb +178 -0
- data/lib/dynamic_reports/vendor/google_chart/bar_chart.rb +90 -0
- data/lib/dynamic_reports/vendor/google_chart/base.rb +539 -0
- data/lib/dynamic_reports/vendor/google_chart/financial_line_chart.rb +31 -0
- data/lib/dynamic_reports/vendor/google_chart/line_chart.rb +79 -0
- data/lib/dynamic_reports/vendor/google_chart/pie_chart.rb +33 -0
- data/lib/dynamic_reports/vendor/google_chart/scatter_chart.rb +38 -0
- data/lib/dynamic_reports/vendor/google_chart/venn_diagram.rb +36 -0
- data/lib/dynamic_reports/vendor/google_chart.rb +11 -0
- data/lib/dynamic_reports/views/default_layout.html.erb +1 -0
- data/lib/dynamic_reports/views/default_report.html.erb +73 -0
- data/lib/dynamic_reports/views/default_report.html.haml +62 -0
- data/lib/dynamic_reports/views.rb +30 -0
- data/lib/dynamic_reports.rb +47 -0
- data/test/dynamic_reports/charts_test.rb +61 -0
- data/test/dynamic_reports/reports_test.rb +77 -0
- data/test/dynamic_reports/templates_test.rb +3 -0
- data/test/dynamic_reports/views_test.rb +5 -0
- data/test/dynamic_reports.rb +18 -0
- data/test/factories/records.rb +0 -0
- data/test/test_helper.rb +64 -0
- metadata +81 -0
data/HISTORY
ADDED
data/README
ADDED
@@ -0,0 +1,189 @@
|
|
1
|
+
= Dynamic Reports
|
2
|
+
|
3
|
+
A dynamic reporting engine for Ruby / Rails
|
4
|
+
|
5
|
+
== Reports
|
6
|
+
|
7
|
+
The dynamic reports gem was created to fill a HUGE hole that we felt existed in the
|
8
|
+
Ruby community - the ability to QUICKLY create stylized admin reports and charts for
|
9
|
+
people to use to view key metrics and data.
|
10
|
+
|
11
|
+
Sample uses include the ability to quickly display sales data if your an eShop, our
|
12
|
+
site metrics if you are recording your own site visits, or user feedback if you are storing
|
13
|
+
feedback in a model somewhere.
|
14
|
+
|
15
|
+
Basically, with DR you can create a stylized table of ANY information found in a model
|
16
|
+
(kind of like looking at the grid output from a GUI query analyzer) as well as add Google
|
17
|
+
Charts API powered line, pie, bar or column charts of any numeric data. All this can
|
18
|
+
be done by simply creating a report definition and feeding it your data.
|
19
|
+
|
20
|
+
While this library is usable in any Ruby application it was made mainly with Rails in mind.
|
21
|
+
Suppose we have an online store and we wish to add reporting to the admin area quickly and easily.
|
22
|
+
First we define a report in app/reports/orders_report.rb, something like:
|
23
|
+
|
24
|
+
class OrdersReport < DynamicReports::Report
|
25
|
+
title "Orders Report"
|
26
|
+
subtitle "All orders recorded in database"
|
27
|
+
columns :total, :created_at
|
28
|
+
end
|
29
|
+
|
30
|
+
Then in our admin/reports controller (this can be any controller) we define an action to deliver the report:
|
31
|
+
|
32
|
+
def orders
|
33
|
+
@orders = Order.find(:all, :limit => 25)
|
34
|
+
render :text => OrdersReport.on(@orders).to_html, :layout => "application"
|
35
|
+
end
|
36
|
+
|
37
|
+
This will render an html table containing some basic styling and containing the columns 'total' and 'created_at' from the order objects.
|
38
|
+
Note that the report Title will be "Orders Report" and it's name will be :orders_report
|
39
|
+
Report#on expects that it receives an object that responds to #each and
|
40
|
+
That each object that it iterates over is either a
|
41
|
+
* An object
|
42
|
+
* A Hash
|
43
|
+
that responds to a method / has keys for each column defined within the report.
|
44
|
+
|
45
|
+
|
46
|
+
Templating engines may also be specified, currently :erb and :haml are supported (we will soon be adding :csv and :pdf) like so:
|
47
|
+
|
48
|
+
render :text => OrdersReport.on(@orders).to_html(:engine => :haml), :layout => "application"
|
49
|
+
|
50
|
+
Note that erb is the default templating engine since it is available by default in Ruby.
|
51
|
+
|
52
|
+
Now let us extend our report definition to specify a template to use!
|
53
|
+
|
54
|
+
class OrdersReport < DynamicReports::Report
|
55
|
+
title "Orders Report"
|
56
|
+
subtitle "All orders recorded in database"
|
57
|
+
columns :total, :created_at
|
58
|
+
|
59
|
+
template :my_custom_template
|
60
|
+
end
|
61
|
+
|
62
|
+
This will look in app/views/reports/ for a template named "my_custom_template.html.erb" by default.
|
63
|
+
If you specify :engine => :haml then it will look for "my_custom_template.html.haml"
|
64
|
+
|
65
|
+
If you happen to have your report templates in a different location you can specify this as follows:
|
66
|
+
|
67
|
+
class OrdersReport < DynamicReports::Report
|
68
|
+
title "Orders Report"
|
69
|
+
subtitle "All orders recorded in database"
|
70
|
+
columns :total, :created_at
|
71
|
+
|
72
|
+
template :my_custom_template
|
73
|
+
views "app/views/admin/reports/"
|
74
|
+
end
|
75
|
+
|
76
|
+
And DynamicReports will look for the specified template in app/views/reports as well as app/views/admin/reports.
|
77
|
+
|
78
|
+
It is also worth pointing out that you can have as many dynamic reports in a view as you wish, simply include
|
79
|
+
each report render where desired within the view.
|
80
|
+
|
81
|
+
== Charts
|
82
|
+
|
83
|
+
Charts can be defined on a report easily. Let's say we wish to chart the total versus the item quantity sold for our Orders Report exmaple:
|
84
|
+
|
85
|
+
class OrdersReport < DynamicReports::Report
|
86
|
+
title "Orders Report"
|
87
|
+
subtitle "All orders recorded in database"
|
88
|
+
columns :total, :created_at
|
89
|
+
|
90
|
+
chart :total_vs_quantity do
|
91
|
+
columns :total, :quantity
|
92
|
+
label_column "created_at"
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
This will render a *line* chart by default displaying the columns total and quantity.
|
97
|
+
Chart types may be specified easily:
|
98
|
+
|
99
|
+
type :bar
|
100
|
+
|
101
|
+
Available chart types are:
|
102
|
+
|
103
|
+
* :line (default)
|
104
|
+
* :bar
|
105
|
+
* :pie
|
106
|
+
|
107
|
+
Since DynamicReport's charts utilize the Google Chart API, you can easily extend each chart by passing a hash of chart options as part
|
108
|
+
of the block. The options are appended onto the request to the API so they should follow the Google's API commands (http://code.google.com/apis/chart/)
|
109
|
+
|
110
|
+
For example, to add min, max and average labels to the example chart, you would do something like this:
|
111
|
+
|
112
|
+
chart :total_vs_quantity, {:chxt => "r", :chxl => "0:|min|average|max"} do
|
113
|
+
columns :total, :quantity
|
114
|
+
label_column "created_at"
|
115
|
+
end
|
116
|
+
|
117
|
+
== Stylizing
|
118
|
+
|
119
|
+
The reports are, by default, stylized with an inline style sheet. The styles produce a nicely formatted grid with
|
120
|
+
a white on black header row and black on white columns with a gray border througout.
|
121
|
+
|
122
|
+
You can create your own styles by simply adding a class_name object to the report definition as such:
|
123
|
+
|
124
|
+
class OrdersReport < DynamicReports::Report
|
125
|
+
title "Orders Report"
|
126
|
+
subtitle "All orders recorded in database"
|
127
|
+
columns :total, :created_at
|
128
|
+
|
129
|
+
class_name "my_class_name"
|
130
|
+
end
|
131
|
+
|
132
|
+
This will cause DR to simply not include the inline style. From there you can customer the styles using the
|
133
|
+
following sub-classes for your class name, for example:
|
134
|
+
|
135
|
+
.my_class_name .report_title {}
|
136
|
+
.my_class_name .report_subtitle {}
|
137
|
+
.my_class_name table tr th {}
|
138
|
+
.my_class_name table tr td {}
|
139
|
+
.my_class_name .report_charts {} // all charts are displayed within this div
|
140
|
+
.my_class_name .report_chart {} // represents an individual chart
|
141
|
+
|
142
|
+
== Rails Usage
|
143
|
+
|
144
|
+
Inside the initializer block in config/environment.rb
|
145
|
+
|
146
|
+
config.gem "dynamic_reports"
|
147
|
+
|
148
|
+
Then define your reports (as exampled above) in app/reports/*_report.rb
|
149
|
+
If you would like to customize the default report simply create your report templates
|
150
|
+
within app/views/reports/*_report.<content-type>.<engine>.
|
151
|
+
|
152
|
+
Two Rails features that we are currently working on are:
|
153
|
+
|
154
|
+
* generator
|
155
|
+
* render extensions
|
156
|
+
|
157
|
+
== Optional Dependencies
|
158
|
+
|
159
|
+
We are currently examining solutions for csv, pdf and charting.
|
160
|
+
|
161
|
+
* Fastercsv # csv
|
162
|
+
* Prawn # pdf
|
163
|
+
* flying saucer # html => PDF - if jRuby available
|
164
|
+
* amcharts # Charting, note that default is built in google charts.
|
165
|
+
|
166
|
+
These will be defined/implemented using DynamicReports plugin API (not implemented yet)
|
167
|
+
Which allows for user defined plugins of arbitrary types beyond html,csv,pdf,xml
|
168
|
+
|
169
|
+
== Contact / Feedback
|
170
|
+
|
171
|
+
If you have any suggestions on improvement please send us an email.
|
172
|
+
|
173
|
+
== Authors (alphabetically)
|
174
|
+
|
175
|
+
Joshua Lippiner (jlippiner@gmail.com)
|
176
|
+
|
177
|
+
Wayne E. Seguin (wayneeseguin@gmail.com, irc: wayneeseguin)
|
178
|
+
|
179
|
+
== Thanks To
|
180
|
+
|
181
|
+
* Daniel Neighman
|
182
|
+
* Kenneth Kalmer (And his friend :))
|
183
|
+
* Yehuda Katz
|
184
|
+
|
185
|
+
For their encouragement, feedback and advise.
|
186
|
+
|
187
|
+
== Source
|
188
|
+
http://github.com/wayneeseguin/dynamic_reports
|
189
|
+
|
data/README.rdoc
ADDED
@@ -0,0 +1,189 @@
|
|
1
|
+
= Dynamic Reports
|
2
|
+
|
3
|
+
A dynamic reporting engine for Ruby / Rails
|
4
|
+
|
5
|
+
== Reports
|
6
|
+
|
7
|
+
The dynamic reports gem was created to fill a HUGE hole that we felt existed in the
|
8
|
+
Ruby community - the ability to QUICKLY create stylized admin reports and charts for
|
9
|
+
people to use to view key metrics and data.
|
10
|
+
|
11
|
+
Sample uses include the ability to quickly display sales data if your an eShop, our
|
12
|
+
site metrics if you are recording your own site visits, or user feedback if you are storing
|
13
|
+
feedback in a model somewhere.
|
14
|
+
|
15
|
+
Basically, with DR you can create a stylized table of ANY information found in a model
|
16
|
+
(kind of like looking at the grid output from a GUI query analyzer) as well as add Google
|
17
|
+
Charts API powered line, pie, bar or column charts of any numeric data. All this can
|
18
|
+
be done by simply creating a report definition and feeding it your data.
|
19
|
+
|
20
|
+
While this library is usable in any Ruby application it was made mainly with Rails in mind.
|
21
|
+
Suppose we have an online store and we wish to add reporting to the admin area quickly and easily.
|
22
|
+
First we define a report in app/reports/orders_report.rb, something like:
|
23
|
+
|
24
|
+
class OrdersReport < DynamicReports::Report
|
25
|
+
title "Orders Report"
|
26
|
+
subtitle "All orders recorded in database"
|
27
|
+
columns :total, :created_at
|
28
|
+
end
|
29
|
+
|
30
|
+
Then in our admin/reports controller (this can be any controller) we define an action to deliver the report:
|
31
|
+
|
32
|
+
def orders
|
33
|
+
@orders = Order.find(:all, :limit => 25)
|
34
|
+
render :text => OrdersReport.on(@orders).to_html, :layout => "application"
|
35
|
+
end
|
36
|
+
|
37
|
+
This will render an html table containing some basic styling and containing the columns 'total' and 'created_at' from the order objects.
|
38
|
+
Note that the report Title will be "Orders Report" and it's name will be :orders_report
|
39
|
+
Report#on expects that it receives an object that responds to #each and
|
40
|
+
That each object that it iterates over is either a
|
41
|
+
* An object
|
42
|
+
* A Hash
|
43
|
+
that responds to a method / has keys for each column defined within the report.
|
44
|
+
|
45
|
+
|
46
|
+
Templating engines may also be specified, currently :erb and :haml are supported (we will soon be adding :csv and :pdf) like so:
|
47
|
+
|
48
|
+
render :text => OrdersReport.on(@orders).to_html(:engine => :haml), :layout => "application"
|
49
|
+
|
50
|
+
Note that erb is the default templating engine since it is available by default in Ruby.
|
51
|
+
|
52
|
+
Now let us extend our report definition to specify a template to use!
|
53
|
+
|
54
|
+
class OrdersReport < DynamicReports::Report
|
55
|
+
title "Orders Report"
|
56
|
+
subtitle "All orders recorded in database"
|
57
|
+
columns :total, :created_at
|
58
|
+
|
59
|
+
template :my_custom_template
|
60
|
+
end
|
61
|
+
|
62
|
+
This will look in app/views/reports/ for a template named "my_custom_template.html.erb" by default.
|
63
|
+
If you specify :engine => :haml then it will look for "my_custom_template.html.haml"
|
64
|
+
|
65
|
+
If you happen to have your report templates in a different location you can specify this as follows:
|
66
|
+
|
67
|
+
class OrdersReport < DynamicReports::Report
|
68
|
+
title "Orders Report"
|
69
|
+
subtitle "All orders recorded in database"
|
70
|
+
columns :total, :created_at
|
71
|
+
|
72
|
+
template :my_custom_template
|
73
|
+
views "app/views/admin/reports/"
|
74
|
+
end
|
75
|
+
|
76
|
+
And DynamicReports will look for the specified template in app/views/reports as well as app/views/admin/reports.
|
77
|
+
|
78
|
+
It is also worth pointing out that you can have as many dynamic reports in a view as you wish, simply include
|
79
|
+
each report render where desired within the view.
|
80
|
+
|
81
|
+
== Charts
|
82
|
+
|
83
|
+
Charts can be defined on a report easily. Let's say we wish to chart the total versus the item quantity sold for our Orders Report exmaple:
|
84
|
+
|
85
|
+
class OrdersReport < DynamicReports::Report
|
86
|
+
title "Orders Report"
|
87
|
+
subtitle "All orders recorded in database"
|
88
|
+
columns :total, :created_at
|
89
|
+
|
90
|
+
chart :total_vs_quantity do
|
91
|
+
columns :total, :quantity
|
92
|
+
label_column "created_at"
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
This will render a *line* chart by default displaying the columns total and quantity.
|
97
|
+
Chart types may be specified easily:
|
98
|
+
|
99
|
+
type :bar
|
100
|
+
|
101
|
+
Available chart types are:
|
102
|
+
|
103
|
+
* :line (default)
|
104
|
+
* :bar
|
105
|
+
* :pie
|
106
|
+
|
107
|
+
Since DynamicReport's charts utilize the Google Chart API, you can easily extend each chart by passing a hash of chart options as part
|
108
|
+
of the block. The options are appended onto the request to the API so they should follow the Google's API commands (http://code.google.com/apis/chart/)
|
109
|
+
|
110
|
+
For example, to add min, max and average labels to the example chart, you would do something like this:
|
111
|
+
|
112
|
+
chart :total_vs_quantity, {:chxt => "r", :chxl => "0:|min|average|max"} do
|
113
|
+
columns :total, :quantity
|
114
|
+
label_column "created_at"
|
115
|
+
end
|
116
|
+
|
117
|
+
== Stylizing
|
118
|
+
|
119
|
+
The reports are, by default, stylized with an inline style sheet. The styles produce a nicely formatted grid with
|
120
|
+
a white on black header row and black on white columns with a gray border througout.
|
121
|
+
|
122
|
+
You can create your own styles by simply adding a class_name object to the report definition as such:
|
123
|
+
|
124
|
+
class OrdersReport < DynamicReports::Report
|
125
|
+
title "Orders Report"
|
126
|
+
subtitle "All orders recorded in database"
|
127
|
+
columns :total, :created_at
|
128
|
+
|
129
|
+
class_name "my_class_name"
|
130
|
+
end
|
131
|
+
|
132
|
+
This will cause DR to simply not include the inline style. From there you can customer the styles using the
|
133
|
+
following sub-classes for your class name, for example:
|
134
|
+
|
135
|
+
.my_class_name .report_title {}
|
136
|
+
.my_class_name .report_subtitle {}
|
137
|
+
.my_class_name table tr th {}
|
138
|
+
.my_class_name table tr td {}
|
139
|
+
.my_class_name .report_charts {} // all charts are displayed within this div
|
140
|
+
.my_class_name .report_chart {} // represents an individual chart
|
141
|
+
|
142
|
+
== Rails Usage
|
143
|
+
|
144
|
+
Inside the initializer block in config/environment.rb
|
145
|
+
|
146
|
+
config.gem "dynamic_reports"
|
147
|
+
|
148
|
+
Then define your reports (as exampled above) in app/reports/*_report.rb
|
149
|
+
If you would like to customize the default report simply create your report templates
|
150
|
+
within app/views/reports/*_report.<content-type>.<engine>.
|
151
|
+
|
152
|
+
Two Rails features that we are currently working on are:
|
153
|
+
|
154
|
+
* generator
|
155
|
+
* render extensions
|
156
|
+
|
157
|
+
== Optional Dependencies
|
158
|
+
|
159
|
+
We are currently examining solutions for csv, pdf and charting.
|
160
|
+
|
161
|
+
* Fastercsv # csv
|
162
|
+
* Prawn # pdf
|
163
|
+
* flying saucer # html => PDF - if jRuby available
|
164
|
+
* amcharts # Charting, note that default is built in google charts.
|
165
|
+
|
166
|
+
These will be defined/implemented using DynamicReports plugin API (not implemented yet)
|
167
|
+
Which allows for user defined plugins of arbitrary types beyond html,csv,pdf,xml
|
168
|
+
|
169
|
+
== Contact / Feedback
|
170
|
+
|
171
|
+
If you have any suggestions on improvement please send us an email.
|
172
|
+
|
173
|
+
== Authors (alphabetically)
|
174
|
+
|
175
|
+
Joshua Lippiner (jlippiner@gmail.com)
|
176
|
+
|
177
|
+
Wayne E. Seguin (wayneeseguin@gmail.com, irc: wayneeseguin)
|
178
|
+
|
179
|
+
== Thanks To
|
180
|
+
|
181
|
+
* Daniel Neighman
|
182
|
+
* Kenneth Kalmer (And his friend :))
|
183
|
+
* Yehuda Katz
|
184
|
+
|
185
|
+
For their encouragement, feedback and advise.
|
186
|
+
|
187
|
+
== Source
|
188
|
+
http://github.com/wayneeseguin/dynamic_reports
|
189
|
+
|
@@ -0,0 +1,62 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = %q{dynamic_reports}
|
5
|
+
s.version = "0.0.2"
|
6
|
+
|
7
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
8
|
+
s.authors = ["Wayne E. Seguin", "Joshua Lippiner"]
|
9
|
+
s.date = %q{2009-07-01}
|
10
|
+
s.description = %q{Dynamic Ruby Reporting Engine with support for Charts.}
|
11
|
+
s.email = %q{wayneeseguin@gmail.com, jlippiner@gmail.com}
|
12
|
+
s.extra_rdoc_files = [
|
13
|
+
"README",
|
14
|
+
"README.rdoc"
|
15
|
+
]
|
16
|
+
s.files = [
|
17
|
+
"HISTORY",
|
18
|
+
"README",
|
19
|
+
"dynamic_reports.gemspec",
|
20
|
+
"lib/dynamic_reports.rb",
|
21
|
+
"lib/dynamic_reports/charts.rb",
|
22
|
+
"lib/dynamic_reports/reports.rb",
|
23
|
+
"lib/dynamic_reports/templates.rb",
|
24
|
+
"lib/dynamic_reports/vendor/google_chart.rb",
|
25
|
+
"lib/dynamic_reports/vendor/google_chart/bar_chart.rb",
|
26
|
+
"lib/dynamic_reports/vendor/google_chart/base.rb",
|
27
|
+
"lib/dynamic_reports/vendor/google_chart/financial_line_chart.rb",
|
28
|
+
"lib/dynamic_reports/vendor/google_chart/line_chart.rb",
|
29
|
+
"lib/dynamic_reports/vendor/google_chart/pie_chart.rb",
|
30
|
+
"lib/dynamic_reports/vendor/google_chart/scatter_chart.rb",
|
31
|
+
"lib/dynamic_reports/vendor/google_chart/venn_diagram.rb",
|
32
|
+
"lib/dynamic_reports/views.rb",
|
33
|
+
"lib/dynamic_reports/views/default_layout.html.erb",
|
34
|
+
"lib/dynamic_reports/views/default_report.html.erb",
|
35
|
+
"lib/dynamic_reports/views/default_report.html.haml"
|
36
|
+
]
|
37
|
+
s.homepage = %q{http://dynamicreports.rubyforge.org/}
|
38
|
+
s.rdoc_options = ["--inline-source", "--charset=UTF-8"]
|
39
|
+
s.require_paths = ["lib"]
|
40
|
+
s.rubyforge_project = %q{dynamicreports}
|
41
|
+
s.rubygems_version = %q{1.3.3}
|
42
|
+
s.summary = %q{Dynamic Ruby Reporting Engine with support for Charts}
|
43
|
+
s.test_files = [
|
44
|
+
"test/dynamic_reports/charts_test.rb",
|
45
|
+
"test/dynamic_reports/reports_test.rb",
|
46
|
+
"test/dynamic_reports/templates_test.rb",
|
47
|
+
"test/dynamic_reports/views_test.rb",
|
48
|
+
"test/dynamic_reports.rb",
|
49
|
+
"test/factories/records.rb",
|
50
|
+
"test/test_helper.rb"
|
51
|
+
]
|
52
|
+
|
53
|
+
if s.respond_to? :specification_version then
|
54
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
55
|
+
s.specification_version = 3
|
56
|
+
|
57
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
58
|
+
else
|
59
|
+
end
|
60
|
+
else
|
61
|
+
end
|
62
|
+
end
|
@@ -0,0 +1,217 @@
|
|
1
|
+
require 'enumerator'
|
2
|
+
|
3
|
+
module DynamicReports
|
4
|
+
class Chart
|
5
|
+
def self.configure(name, *chart_options, &block)
|
6
|
+
chart_options = chart_options.shift || {}
|
7
|
+
chart = new(chart_options)
|
8
|
+
chart.instance_eval(&block)
|
9
|
+
chart.name name
|
10
|
+
chart
|
11
|
+
end
|
12
|
+
|
13
|
+
def initialize(*chart_options)
|
14
|
+
chart_options = chart_options.shift || {}
|
15
|
+
options.merge!(chart_options)
|
16
|
+
end
|
17
|
+
|
18
|
+
def options
|
19
|
+
@options ||= {} ; @options
|
20
|
+
end
|
21
|
+
|
22
|
+
# (Optional) Accessor used to set the chart title:
|
23
|
+
#
|
24
|
+
# title "Pageviews versus Visits"
|
25
|
+
#
|
26
|
+
# This is displayed above the chart
|
27
|
+
def title(value = nil)
|
28
|
+
value ? options[:title] = value : options[:title]
|
29
|
+
end
|
30
|
+
|
31
|
+
# Accessor used to set or get a specific report. Must be unique:
|
32
|
+
#
|
33
|
+
# name "PV_Visits"
|
34
|
+
#
|
35
|
+
def name(value = nil)
|
36
|
+
value ? options[:name] = value.to_sym : options[:name]
|
37
|
+
end
|
38
|
+
|
39
|
+
# (Optional) Accessor used by bar and pie charts to determine the
|
40
|
+
# independent varible chart labels. Due to size constraints
|
41
|
+
# this is NOT used by column or line charts, but you can add
|
42
|
+
# labels to the X-axis for those charts via chart_options and
|
43
|
+
# passing Google Chart API calls."
|
44
|
+
#
|
45
|
+
# label_column should be a SINGLE column name from the dataset.
|
46
|
+
#
|
47
|
+
# label_column "recorded_at"
|
48
|
+
#
|
49
|
+
def label_column(value = nil)
|
50
|
+
value ? options[:label_column] = value : options[:label_column]
|
51
|
+
end
|
52
|
+
|
53
|
+
# (Optional - Default = line). Accessor used to determine the
|
54
|
+
# type of chart to display. Valid options are line, column, bar
|
55
|
+
# or pie:
|
56
|
+
#
|
57
|
+
# type "bar"
|
58
|
+
#
|
59
|
+
def type(value = nil)
|
60
|
+
value ? options[:type] = value : (options[:type] || :line)
|
61
|
+
end
|
62
|
+
|
63
|
+
# (Optional - Default = 250). Accessor used to set the width, in pixels,
|
64
|
+
# of the chart.
|
65
|
+
#
|
66
|
+
# width "400"
|
67
|
+
#
|
68
|
+
def width(value = nil)
|
69
|
+
value ? options[:width] = value : (options[:width] || "250")
|
70
|
+
end
|
71
|
+
|
72
|
+
# (Optional - Default = 175). Accessor used to set the height, in pixels,
|
73
|
+
# of the chart.
|
74
|
+
#
|
75
|
+
# height "350"
|
76
|
+
#
|
77
|
+
def height(value = nil)
|
78
|
+
value ? options[:height] = value : (options[:height] || "175")
|
79
|
+
end
|
80
|
+
|
81
|
+
# (Optional - Default = false). Accessor used to determine if axis labels
|
82
|
+
# should be shown. By default y-axis labels are shown.
|
83
|
+
#
|
84
|
+
# no_labels true
|
85
|
+
#
|
86
|
+
def no_labels(value = nil)
|
87
|
+
value ? options[:no_labels] = value : options[:no_labels]
|
88
|
+
end
|
89
|
+
|
90
|
+
# (Optional) Accessor for columns
|
91
|
+
#
|
92
|
+
# Pass an array of symbols to define columns to chart. Columns MUST
|
93
|
+
# be numeric in value to chart.
|
94
|
+
#
|
95
|
+
# Example:
|
96
|
+
#
|
97
|
+
# columns :pageviews, :visits
|
98
|
+
#
|
99
|
+
# You may leave this accessor blank to default to the report columns
|
100
|
+
# specified that are numeric.
|
101
|
+
#
|
102
|
+
def columns(*array)
|
103
|
+
unless array.empty?
|
104
|
+
if (array.class == Array)
|
105
|
+
options[:columns] = array
|
106
|
+
else
|
107
|
+
raise "Report columns must be specified."
|
108
|
+
end
|
109
|
+
else
|
110
|
+
options[:columns]
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
# DynamicReports::Charts
|
116
|
+
#
|
117
|
+
# Class used to display different chart types internally. Charts are generated
|
118
|
+
# using Google Charts API
|
119
|
+
#
|
120
|
+
class Charts
|
121
|
+
class << self
|
122
|
+
# Method to select a random color from a list of hex codes
|
123
|
+
#
|
124
|
+
# Example: random_color()
|
125
|
+
# => "ff0000"
|
126
|
+
def random_color()
|
127
|
+
color_list = %w{000000 0000ff ff0000 ffff00 00ffff ff00ff 00ff00}
|
128
|
+
return color_list[rand(color_list.size)]
|
129
|
+
end
|
130
|
+
|
131
|
+
# Method to display a line chart for a given chart definition and report
|
132
|
+
def line_chart(chart, columns, report)
|
133
|
+
::GoogleChart::LineChart.new("#{chart.width}x#{chart.height}", chart.title, false) do |c|
|
134
|
+
all_data = []
|
135
|
+
columns.each do |column|
|
136
|
+
data = []
|
137
|
+
report.records.each do |record|
|
138
|
+
if record.is_a?(Hash)
|
139
|
+
data << record[column] if record[column].is_a?(Numeric)
|
140
|
+
elsif record.respond_to?(column.to_sym)
|
141
|
+
data << record.send(column.to_sym) if record.send(column.to_sym).is_a?(Numeric)
|
142
|
+
else
|
143
|
+
data << column if column.is_a?(Numeric)
|
144
|
+
end
|
145
|
+
end
|
146
|
+
c.data column, data, random_color() unless data.empty?
|
147
|
+
all_data << data
|
148
|
+
end
|
149
|
+
all_data.flatten!
|
150
|
+
c.axis :y, :range => [all_data.min,all_data.max], :color => 'ff00ff' unless chart.no_labels
|
151
|
+
c.show_legend = columns.size > 1
|
152
|
+
|
153
|
+
return c.to_url(chart.options)
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
157
|
+
# Method to display a pie chart for a given chart definition and report
|
158
|
+
def pie_chart(chart, columns, report)
|
159
|
+
return if columns.size > 1 || chart.label_column.nil?
|
160
|
+
column = columns.first.to_s
|
161
|
+
|
162
|
+
::GoogleChart::PieChart.new("#{chart.width}x#{chart.height}", chart.title, false) do |c|
|
163
|
+
report.records.each do |record|
|
164
|
+
if record.is_a?(Hash)
|
165
|
+
c.data record[chart.label_column.to_s], record[column] if record[column].is_a?(Numeric)
|
166
|
+
elsif record.respond_to?(column.to_sym)
|
167
|
+
c.data record.send(chart.label_column.to_s), record.send(column.to_sym) if record.send(column.to_sym).is_a?(Numeric)
|
168
|
+
else
|
169
|
+
c.data chart.label_column.to_s, column if column.is_a?(Numeric)
|
170
|
+
end
|
171
|
+
end
|
172
|
+
c.show_legend = false
|
173
|
+
c.show_labels = true
|
174
|
+
|
175
|
+
return c.to_url(chart.options)
|
176
|
+
end
|
177
|
+
end
|
178
|
+
|
179
|
+
# Method to display a bar or column chart for a given chart definition and report
|
180
|
+
def bar_column_chart(chart, columns, report, orientation)
|
181
|
+
::GoogleChart::BarChart.new("#{chart.width}x#{chart.height}", chart.title, orientation, true) do |c|
|
182
|
+
all_data = []
|
183
|
+
all_labels = []
|
184
|
+
columns.each do |column|
|
185
|
+
data = []
|
186
|
+
report.records.each do |record|
|
187
|
+
if record.is_a?(Hash)
|
188
|
+
data << record[column] if record[column].is_a?(Numeric)
|
189
|
+
all_labels << record[chart.label_column.to_s] if chart.label_column
|
190
|
+
elsif record.respond_to?(column.to_sym)
|
191
|
+
data << record.send(column.to_sym) if record.send(column.to_sym).is_a?(Numeric)
|
192
|
+
all_labels << record.send(chart.label_column.to_s) if chart.label_column
|
193
|
+
else
|
194
|
+
data << column if column.is_a?(Numeric)
|
195
|
+
all_labels << chart.label_column.to_s if chart.label_column
|
196
|
+
end
|
197
|
+
end
|
198
|
+
c.data column, data, random_color() unless data.empty?
|
199
|
+
all_data << data
|
200
|
+
end
|
201
|
+
all_data.flatten!
|
202
|
+
|
203
|
+
if(orientation==:vertical)
|
204
|
+
c.axis :y, :range => [all_data.min,all_data.max], :color => 'ff00ff' unless chart.no_labels
|
205
|
+
else
|
206
|
+
c.axis :x, :range => [all_data.min,all_data.max], :color => 'ff00ff' unless chart.no_labels
|
207
|
+
c.axis :y, :labels => all_labels
|
208
|
+
end
|
209
|
+
|
210
|
+
c.show_legend = columns.size > 1
|
211
|
+
|
212
|
+
return c.to_url(chart.options)
|
213
|
+
end
|
214
|
+
end
|
215
|
+
end
|
216
|
+
end
|
217
|
+
end
|