chartjs-ror 0.0.1 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +215 -4
- data/chartjs-ror.gemspec +2 -2
- data/lib/chartjs-ror.rb +2 -7
- data/lib/chartjs.rb +5 -0
- data/lib/chartjs/chart_helpers.rb +91 -0
- data/lib/chartjs/railtie.rb +9 -0
- data/lib/chartjs/version.rb +3 -0
- metadata +6 -3
- data/lib/chartjs-ror/version.rb +0 -5
data/README.md
CHANGED
@@ -1,6 +1,14 @@
|
|
1
1
|
# chartjs-ror
|
2
2
|
|
3
|
-
Simplifies using [Chart.js][] in Rails.
|
3
|
+
Simplifies using [Chart.js][] in Rails views.
|
4
|
+
|
5
|
+
* All of Chart.js's features via one line of Ruby.
|
6
|
+
* Legends for your charts.
|
7
|
+
* Renders charts on page load rather than DOMContentReady ([reason][browsersupport]).
|
8
|
+
* Animates unless you have Modernizr and it doesn't detect canvas support ([reason][browsersupport]). You can manually override this.
|
9
|
+
|
10
|
+
NOTE: this is Rails 3.0 only at the moment, so pre-asset pipeline. I plan to upgrade soon.
|
11
|
+
|
4
12
|
|
5
13
|
## Installation
|
6
14
|
|
@@ -12,12 +20,215 @@ And then execute:
|
|
12
20
|
|
13
21
|
$ bundle
|
14
22
|
|
15
|
-
|
23
|
+
Add [Chart.js][] (and [Modernizr][] and [ExplorerCanvas][] if you need them) to your assets.
|
24
|
+
|
25
|
+
Ensure your browsers will display `<figure/>` and `<figcaption/>` correctly.
|
16
26
|
|
17
|
-
$ gem install chartjs-ror
|
18
27
|
|
19
28
|
## Usage
|
20
29
|
|
21
|
-
|
30
|
+
Each chart type has a corresponding helper for your views. The helper methods take the same arguments as their Javascript counterparts. The `options` are always optional.
|
31
|
+
|
32
|
+
### Charts with multiple datasets
|
33
|
+
|
34
|
+
```erb
|
35
|
+
<%= line_chart labels, datasets, options %>
|
36
|
+
<%= bar_chart labels, datasets, options %>
|
37
|
+
<%= radar_chart labels, datasets, options %>
|
38
|
+
```
|
39
|
+
|
40
|
+
For example, to render a [line chart][linechart] in Javascript:
|
41
|
+
|
42
|
+
```javascript
|
43
|
+
var data = {
|
44
|
+
labels : ["January","February","March","April","May","June","July"],
|
45
|
+
datasets : [
|
46
|
+
{
|
47
|
+
fillColor : "rgba(220,220,220,0.5)",
|
48
|
+
strokeColor : "rgba(220,220,220,1)",
|
49
|
+
pointColor : "rgba(220,220,220,1)",
|
50
|
+
pointStrokeColor : "#fff",
|
51
|
+
data : [65,59,90,81,56,55,40]
|
52
|
+
},
|
53
|
+
{
|
54
|
+
fillColor : "rgba(151,187,205,0.5)",
|
55
|
+
strokeColor : "rgba(151,187,205,1)",
|
56
|
+
pointColor : "rgba(151,187,205,1)",
|
57
|
+
pointStrokeColor : "#fff",
|
58
|
+
data : [28,48,40,19,96,27,100]
|
59
|
+
}
|
60
|
+
]
|
61
|
+
}
|
62
|
+
var options = {};
|
63
|
+
new Chart(ctx).Line(data,options);
|
64
|
+
```
|
65
|
+
|
66
|
+
The Ruby equivalent is:
|
67
|
+
|
68
|
+
```ruby
|
69
|
+
@data = {
|
70
|
+
labels: ["January","February","March","April","May","June","July"],
|
71
|
+
datasets: [
|
72
|
+
{
|
73
|
+
fillColor: "rgba(220,220,220,0.5)",
|
74
|
+
strokeColor: "rgba(220,220,220,1)",
|
75
|
+
pointColor: "rgba(220,220,220,1)",
|
76
|
+
pointStrokeColor: "#fff",
|
77
|
+
data: [65,59,90,81,56,55,40]
|
78
|
+
},
|
79
|
+
{
|
80
|
+
fillColor: "rgba(151,187,205,0.5)",
|
81
|
+
strokeColor: "rgba(151,187,205,1)",
|
82
|
+
pointColor: "rgba(151,187,205,1)",
|
83
|
+
pointStrokeColor: "#fff",
|
84
|
+
data: [28,48,40,19,96,27,100]
|
85
|
+
}
|
86
|
+
]
|
87
|
+
}
|
88
|
+
@options = {}
|
89
|
+
<%= line_chart @data, @options %>
|
90
|
+
```
|
91
|
+
|
92
|
+
### Charts with one dataset
|
93
|
+
|
94
|
+
```erb
|
95
|
+
<%= polar_chart data, options %>
|
96
|
+
<%= pie_chart data, options %>
|
97
|
+
<%= doughnut_chart data, options %>
|
98
|
+
```
|
99
|
+
|
100
|
+
For example, to render a [pie chart][piechart] in Javascript:
|
101
|
+
|
102
|
+
```javascript
|
103
|
+
var data = [
|
104
|
+
{
|
105
|
+
value : 30,
|
106
|
+
color: "#D97041"
|
107
|
+
},
|
108
|
+
{
|
109
|
+
value : 90,
|
110
|
+
color: "#C7604C"
|
111
|
+
},
|
112
|
+
{
|
113
|
+
value : 24,
|
114
|
+
color: "#21323D"
|
115
|
+
},
|
116
|
+
{
|
117
|
+
value : 58,
|
118
|
+
color: "#9D9B7F"
|
119
|
+
},
|
120
|
+
{
|
121
|
+
value : 82,
|
122
|
+
color: "#7D4F6D"
|
123
|
+
},
|
124
|
+
{
|
125
|
+
value : 8,
|
126
|
+
color: "#584A5E"
|
127
|
+
}
|
128
|
+
]
|
129
|
+
new Chart(ctx).Pie(data,options);
|
130
|
+
```
|
131
|
+
|
132
|
+
And in Ruby:
|
133
|
+
|
134
|
+
```ruby
|
135
|
+
@data = [
|
136
|
+
{
|
137
|
+
value: 30,
|
138
|
+
color: "#D97041"
|
139
|
+
},
|
140
|
+
{
|
141
|
+
value: 90,
|
142
|
+
color: "#C7604C"
|
143
|
+
},
|
144
|
+
{
|
145
|
+
value: 24,
|
146
|
+
color: "#21323D"
|
147
|
+
},
|
148
|
+
{
|
149
|
+
value: 58,
|
150
|
+
color: "#9D9B7F"
|
151
|
+
},
|
152
|
+
{
|
153
|
+
value: 82,
|
154
|
+
color: "#7D4F6D"
|
155
|
+
},
|
156
|
+
{
|
157
|
+
value: 8,
|
158
|
+
color: "#584A5E"
|
159
|
+
}
|
160
|
+
]
|
161
|
+
<%= pie_chart @data %>
|
162
|
+
```
|
163
|
+
|
164
|
+
### Options
|
165
|
+
|
166
|
+
You can put anything in the `options` hash that Chart.js recognises. It also supports these non-Chart.js settings:
|
167
|
+
|
168
|
+
* `:css_class` - class of the enclosing `<figure/>` - default is `chart`.
|
169
|
+
* `:element_id` - id of the `<canvas/>` - default is `chart-n` where `n` is the 0-based index of the chart on the page.
|
170
|
+
* `:width` - width of the canvas in px - default is `400`.
|
171
|
+
* `:height` - height of the canvas in px - default is `400`.
|
172
|
+
* `:legend` - an array of names for the datasets.
|
173
|
+
|
174
|
+
### Sample output:
|
175
|
+
|
176
|
+
```html
|
177
|
+
<figure class="chart">
|
178
|
+
<canvas id="chart-0" width=400 height=400></canvas>
|
179
|
+
<!-- if :legend option is given -->
|
180
|
+
<figcaption>
|
181
|
+
<ol>
|
182
|
+
<li class="chart-0 dataset-0"><span></span>Apples</li>
|
183
|
+
<li class="chart-0 dataset-1"><span></span>Bananas</li>
|
184
|
+
<li class="chart-0 dataset-2"><span></span>Cherries</li>
|
185
|
+
</ol>
|
186
|
+
</figcaption>
|
187
|
+
</figure>
|
188
|
+
<script type="text/javascript">
|
189
|
+
var initChart = function() {
|
190
|
+
var data = {labels: ["Apples","Bananas","Cherries"], datasets: [{"data":[42,153,...],...}, ...]};
|
191
|
+
var opts = {"scaleFontSize":10};
|
192
|
+
if (!('animation' in opts)) {
|
193
|
+
opts['animation'] = (typeof Modernizr == 'undefined') || Modernizr.canvas;
|
194
|
+
}
|
195
|
+
var ctx = document.getElementById("chart-0").getContext('2d');
|
196
|
+
new Chart(ctx).Bar(data, opts);
|
197
|
+
};
|
198
|
+
if (window.addEventListener) { // W3C standard
|
199
|
+
window.addEventListener('load', initChart, false);
|
200
|
+
}
|
201
|
+
else if (window.attachEvent) { // IE
|
202
|
+
window.attachEvent('onload', initChart);
|
203
|
+
}
|
204
|
+
</script>
|
205
|
+
```
|
206
|
+
|
207
|
+
### Legend
|
208
|
+
|
209
|
+
Each item in the legend array is given two classes:
|
210
|
+
|
211
|
+
* `dataset-m` where `m` is the 0-based index of the item.
|
212
|
+
* The id value of the canvas.
|
213
|
+
|
214
|
+
This lets you style legends in general but override the styles for specific charts.
|
215
|
+
|
216
|
+
|
217
|
+
## Inspiration
|
218
|
+
|
219
|
+
* [Chart.js][] (obviously)
|
220
|
+
* [Chartkick][]
|
221
|
+
|
222
|
+
|
223
|
+
## Intellectual Property
|
224
|
+
|
225
|
+
Copyright Andrew Stewart, AirBlade Software. Released under the MIT licence.
|
226
|
+
|
22
227
|
|
23
228
|
[Chart.js]: http://www.chartjs.org/
|
229
|
+
[Chartkick]: http://ankane.github.io/chartkick/
|
230
|
+
[browsersupport]: http://www.chartjs.org/docs/#generalIssues-browserSupport
|
231
|
+
[linechart]: http://www.chartjs.org/docs/#lineChart-exampleUsage
|
232
|
+
[piechart]: http://www.chartjs.org/docs/#pieChart-exampleUsage
|
233
|
+
[Modernizr]: http://modernizr.com
|
234
|
+
[ExplorerCanvas]: https://code.google.com/p/explorercanvas
|
data/chartjs-ror.gemspec
CHANGED
@@ -1,11 +1,11 @@
|
|
1
1
|
# -*- encoding: utf-8 -*-
|
2
2
|
lib = File.expand_path('../lib', __FILE__)
|
3
3
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
-
require 'chartjs
|
4
|
+
require 'chartjs/version'
|
5
5
|
|
6
6
|
Gem::Specification.new do |gem|
|
7
7
|
gem.name = "chartjs-ror"
|
8
|
-
gem.version = Chartjs::
|
8
|
+
gem.version = Chartjs::VERSION
|
9
9
|
gem.authors = ["Andy Stewart"]
|
10
10
|
gem.email = ["boss@airbladesoftware.com"]
|
11
11
|
gem.description = 'Simplifies using Chart.js in Rails'
|
data/lib/chartjs-ror.rb
CHANGED
data/lib/chartjs.rb
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
module Chartjs
|
2
|
+
module ChartHelpers
|
3
|
+
|
4
|
+
def line_chart(labels, datasets, options = {})
|
5
|
+
chart 'Line', labels, datasets, options
|
6
|
+
end
|
7
|
+
|
8
|
+
def bar_chart(labels, datasets, options = {})
|
9
|
+
chart 'Bar', labels, datasets, options
|
10
|
+
end
|
11
|
+
|
12
|
+
def radar_chart(labels, datasets, options = {})
|
13
|
+
chart 'Radar', labels, datasets, options
|
14
|
+
end
|
15
|
+
|
16
|
+
def polar_area_chart(data, options = {})
|
17
|
+
chart 'PolarArea', nil, data, options
|
18
|
+
end
|
19
|
+
|
20
|
+
def pie_chart(data, options = {})
|
21
|
+
chart 'Pie', nil, data, options
|
22
|
+
end
|
23
|
+
|
24
|
+
def doughnut_chart(data, options = {})
|
25
|
+
chart 'Doughnut', nil, data, options
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
def chart(klass, labels, datasets, options)
|
31
|
+
@chart_id ||= -1
|
32
|
+
element_id = options.delete(:id) || "chart-#{@chart_id += 1}"
|
33
|
+
css_class = options.delete(:class) || 'chart'
|
34
|
+
width = options.delete(:width) || '400'
|
35
|
+
height = options.delete(:height) || '400'
|
36
|
+
|
37
|
+
canvas = content_tag(:canvas, id: element_id, width: width, height: height) do
|
38
|
+
end
|
39
|
+
|
40
|
+
# <figcaption>
|
41
|
+
# <ol>
|
42
|
+
# <li class="chart-0 dataset-0"><span/>Apples</li>
|
43
|
+
# <li class="chart-0 dataset-1"><span/>Bananas</li>
|
44
|
+
# <li class="chart-0 dataset-2"><span/>Cherries</li>
|
45
|
+
# </ol>
|
46
|
+
# </figcaption>
|
47
|
+
legend = if options[:legend]
|
48
|
+
content_tag :figcaption do
|
49
|
+
content_tag :ol do
|
50
|
+
options.delete(:legend).each_with_index do |name, index|
|
51
|
+
concat content_tag(:li, (content_tag(:span) + name), class: "#{element_id} dataset-#{index}")
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
else
|
56
|
+
''
|
57
|
+
end
|
58
|
+
|
59
|
+
script = javascript_tag do
|
60
|
+
<<-END.html_safe
|
61
|
+
var initChart = function() {
|
62
|
+
var data = #{data_for labels, datasets};
|
63
|
+
var opts = #{options.to_json};
|
64
|
+
if (!('animation' in opts)) {
|
65
|
+
opts['animation'] = (typeof Modernizr == 'undefined') || Modernizr.canvas;
|
66
|
+
}
|
67
|
+
var ctx = document.getElementById(#{element_id.to_json}).getContext('2d');
|
68
|
+
new Chart(ctx).#{klass}(data, opts);
|
69
|
+
};
|
70
|
+
if (window.addEventListener) { // W3C standard
|
71
|
+
window.addEventListener('load', initChart, false);
|
72
|
+
}
|
73
|
+
else if (window.attachEvent) { // IE
|
74
|
+
window.attachEvent('onload', initChart);
|
75
|
+
}
|
76
|
+
END
|
77
|
+
end
|
78
|
+
|
79
|
+
content_tag(:figure, (canvas + legend), class: css_class) + script
|
80
|
+
end
|
81
|
+
|
82
|
+
def data_for(labels, datasets)
|
83
|
+
if labels
|
84
|
+
"{labels: #{labels.to_json}, datasets: #{datasets.to_json}}"
|
85
|
+
else
|
86
|
+
datasets.to_json
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
end
|
91
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: chartjs-ror
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 1.0.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-06-
|
12
|
+
date: 2013-06-06 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: Simplifies using Chart.js in Rails
|
15
15
|
email:
|
@@ -25,7 +25,10 @@ files:
|
|
25
25
|
- Rakefile
|
26
26
|
- chartjs-ror.gemspec
|
27
27
|
- lib/chartjs-ror.rb
|
28
|
-
- lib/chartjs
|
28
|
+
- lib/chartjs.rb
|
29
|
+
- lib/chartjs/chart_helpers.rb
|
30
|
+
- lib/chartjs/railtie.rb
|
31
|
+
- lib/chartjs/version.rb
|
29
32
|
homepage: https://github.com/airblade/chartjs-ror
|
30
33
|
licenses: []
|
31
34
|
post_install_message:
|