tracks-chartjs-ror 3.6.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/CHANGELOG.md +137 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +207 -0
- data/Rakefile +8 -0
- data/lib/chartjs/chart_helpers.rb +108 -0
- data/lib/chartjs/engine.rb +13 -0
- data/lib/chartjs/version.rb +3 -0
- data/lib/chartjs.rb +14 -0
- data/lib/tracks-chartjs-ror.rb +2 -0
- data/test/chart_helpers_test.rb +51 -0
- data/test/test_helper.rb +3 -0
- data/tracks-chartjs-ror.gemspec +22 -0
- data/vendor/assets/javascripts/Chart.bundle.min.js +10 -0
- data/vendor/assets/javascripts/Chart.min.js +10 -0
- metadata +89 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: a5bba648f3157a4fcce42c146ee94402fa3136de2f4afbaa57e9437dcc294d19
|
4
|
+
data.tar.gz: 81885547026f01d766c3bc384c11df26b891d531a2bbe8b63b0e398a016c3843
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 6a31e195ff35d6f56008683d4be8486be4be65442c887e9e55507573aa1d270230fc8fc61f0183806ca6908cfb9b77491858ea7a5b3e5b25b947c6e771882c31
|
7
|
+
data.tar.gz: 4a8ceb4b5fb93fea805b15a8e5ffa8da417a857b82ca9ad0ebd7d90866048b196ed24c35650121632c1003bd18f2d3746f41bf7a696f050282265c6a6153de75
|
data/.gitignore
ADDED
data/CHANGELOG.md
ADDED
@@ -0,0 +1,137 @@
|
|
1
|
+
# CHANGELOG
|
2
|
+
|
3
|
+
|
4
|
+
## 3.6.4 - 2019-02-27
|
5
|
+
|
6
|
+
- Add argument for plugins.
|
7
|
+
|
8
|
+
|
9
|
+
## 3.6.3 - 2019-02-14
|
10
|
+
|
11
|
+
- Use Chart.js v2.7.3.
|
12
|
+
- Add CSP nonce to inline JavaScript.
|
13
|
+
- Do not modify options.
|
14
|
+
|
15
|
+
|
16
|
+
## 3.6.2 - 2018-03-14
|
17
|
+
|
18
|
+
- Use Chart.js v2.7.2.
|
19
|
+
|
20
|
+
|
21
|
+
## 3.6.1 - 2018-03-14
|
22
|
+
|
23
|
+
- Use Chart.js v2.7.1.
|
24
|
+
|
25
|
+
|
26
|
+
## 3.6.0 - 2018-03-14
|
27
|
+
|
28
|
+
- Use Chart.js v2.7.0.
|
29
|
+
|
30
|
+
|
31
|
+
## 3.5.0 - 2018-03-14
|
32
|
+
|
33
|
+
- Use Chart.js v2.6.0.
|
34
|
+
|
35
|
+
|
36
|
+
## 3.4.0 - 2018-03-14
|
37
|
+
|
38
|
+
- Use Chart.js v2.5.0.
|
39
|
+
|
40
|
+
|
41
|
+
## 3.3.1 - 2017-01-27
|
42
|
+
|
43
|
+
- Improve conversion of data and options hashes to JavaScript.
|
44
|
+
|
45
|
+
|
46
|
+
## 3.3.0 - 2017-01-26
|
47
|
+
|
48
|
+
- Use Chart.js v2.4.0.
|
49
|
+
|
50
|
+
|
51
|
+
## 3.2.0 - 2017-01-25
|
52
|
+
|
53
|
+
- Use Chart.js v2.3.0.
|
54
|
+
|
55
|
+
|
56
|
+
## 3.1.0 - 2017-01-25
|
57
|
+
|
58
|
+
- Use Chart.js v2.2.0.
|
59
|
+
- Add CHANGELOG.
|
60
|
+
|
61
|
+
|
62
|
+
## 3.0.2 - 2016-06-13
|
63
|
+
|
64
|
+
- Add pie chart helper.
|
65
|
+
|
66
|
+
|
67
|
+
## 3.0.1 - 2016-06-10
|
68
|
+
|
69
|
+
- Add horizontal bar chart helper.
|
70
|
+
|
71
|
+
|
72
|
+
## 3.0.0 - 2016-06-08
|
73
|
+
|
74
|
+
- Use Chart.js v2.1.4 (backwards incompatible).
|
75
|
+
|
76
|
+
|
77
|
+
## 2.2.0 - 2015-08-26
|
78
|
+
|
79
|
+
- Use Chart.js v1.0.2.
|
80
|
+
|
81
|
+
|
82
|
+
## 2.1.3 - 2015-08-26
|
83
|
+
|
84
|
+
- AJAX and Turbolinks compatibility.
|
85
|
+
|
86
|
+
|
87
|
+
## 2.1.2 - 2015-08-18
|
88
|
+
|
89
|
+
- Pass `onAnimationComplete` and `onAnimationProgress` functions correctly.
|
90
|
+
|
91
|
+
|
92
|
+
## 2.1.1 - 2015-03-13
|
93
|
+
|
94
|
+
- Provide original helpers with easy way to opt out.
|
95
|
+
- Namespace the view helpers.
|
96
|
+
- Fix name of polar_area_chart in README.
|
97
|
+
- Ensure any old charts are cleaned up.
|
98
|
+
- Clarify optional features.
|
99
|
+
- State Chart.js version in README.
|
100
|
+
|
101
|
+
|
102
|
+
## 2.1.0 - 2015-01-14
|
103
|
+
|
104
|
+
- Use Chart.js v1.0.1.
|
105
|
+
- Support Turbolinks.
|
106
|
+
- Do not generate a legend by default.
|
107
|
+
|
108
|
+
|
109
|
+
## 2.0.0 - 2014-11-07
|
110
|
+
|
111
|
+
- Use Chart.js 1cfa42bc (a little after v1.0.1-beta.4).
|
112
|
+
- Wrap generated JavaScript snippet in a closure.
|
113
|
+
- Use Chart.js' legends.
|
114
|
+
- Update README.
|
115
|
+
- Add link to scale calculation issue.
|
116
|
+
|
117
|
+
|
118
|
+
## 1.1.1 - 2014-03-20
|
119
|
+
|
120
|
+
- Rails 4 compatibility.
|
121
|
+
|
122
|
+
|
123
|
+
## 1.1.0 - 2014-03-14
|
124
|
+
|
125
|
+
- Use Rails' asset pipeline.
|
126
|
+
|
127
|
+
|
128
|
+
## 1.0.1 - 2013-06-21
|
129
|
+
|
130
|
+
- Opt-in alternative scale calculations.
|
131
|
+
- Code for filling gaps in series.
|
132
|
+
|
133
|
+
|
134
|
+
## 1.0.0 - 2013-06-16
|
135
|
+
|
136
|
+
- First release.
|
137
|
+
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Andy Stewart
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,207 @@
|
|
1
|
+
# chartjs-ror
|
2
|
+
|
3
|
+
Simplifies using [Chart.js][] in Rails views.
|
4
|
+
|
5
|
+
|
6
|
+
## Current Chart.js version
|
7
|
+
|
8
|
+
This gem includes [Chart.js v2.7.3](https://github.com/chartjs/Chart.js/tree/v2.7.3).
|
9
|
+
|
10
|
+
|
11
|
+
## Installation
|
12
|
+
|
13
|
+
Add this line to your application's Gemfile:
|
14
|
+
|
15
|
+
gem 'chartjs-ror'
|
16
|
+
|
17
|
+
And then execute:
|
18
|
+
|
19
|
+
$ bundle
|
20
|
+
|
21
|
+
There are two builds of Chart.js: with or without Moment.js.
|
22
|
+
|
23
|
+
Choose the appropriate one in your javascript manifest:
|
24
|
+
|
25
|
+
- Without Moment.js:
|
26
|
+
```javascript
|
27
|
+
//= require Chart.min
|
28
|
+
```
|
29
|
+
|
30
|
+
- With Moment.js:
|
31
|
+
```javascript
|
32
|
+
//= require Chart.bundle.min
|
33
|
+
```
|
34
|
+
|
35
|
+
Please note Chart.js [no longer supports IE8 and below](https://github.com/chartjs/Chart.js/issues/2396#issuecomment-215233106).
|
36
|
+
|
37
|
+
|
38
|
+
## Usage
|
39
|
+
|
40
|
+
Each chart type has a corresponding helper for your views. The helper methods take the same arguments as their Javascript counterparts. The `options` are optional.
|
41
|
+
|
42
|
+
|
43
|
+
```erb
|
44
|
+
<%= line_chart data, options %>
|
45
|
+
<%= bar_chart data, options %>
|
46
|
+
<%= horizontal_bar_chart data, options %>
|
47
|
+
<%= radar_chart data, options %>
|
48
|
+
<%= polar_area_chart data, options %>
|
49
|
+
<%= pie_chart data, options %>
|
50
|
+
<%= doughnut_chart data, options %>
|
51
|
+
<%= bubble_chart data, options %>
|
52
|
+
<%= scatter_chart data, options %>
|
53
|
+
```
|
54
|
+
|
55
|
+
If you don't want these helpers – perhaps they clash with other methods in your views – add this initializer to your app:
|
56
|
+
|
57
|
+
```ruby
|
58
|
+
# config/initializers/chartjs.rb
|
59
|
+
Chartjs.no_conflict!
|
60
|
+
```
|
61
|
+
|
62
|
+
Then you can use these helpers instead:
|
63
|
+
|
64
|
+
```erb
|
65
|
+
<%= chartjs_line_chart data, options %>
|
66
|
+
<%= chartjs_bar_chart data, options %>
|
67
|
+
<%= chartjs_horizontal_bar_chart data, options %>
|
68
|
+
<%= chartjs_radar_chart data, options %>
|
69
|
+
<%= chartjs_polar_area_chart data, options %>
|
70
|
+
<%= chartjs_pie_chart data, options %>
|
71
|
+
<%= chartjs_doughnut_chart data, options %>
|
72
|
+
<%= chartjs_bubble_chart data, options %>
|
73
|
+
<%= chartjs_scatter_chart data, options %>
|
74
|
+
```
|
75
|
+
|
76
|
+
For example, to render a [line chart][linechart] in Javascript:
|
77
|
+
|
78
|
+
```javascript
|
79
|
+
var data = {
|
80
|
+
labels: ["January", "February", "March", "April", "May", "June", "July"],
|
81
|
+
datasets: [
|
82
|
+
{
|
83
|
+
label: "My First dataset",
|
84
|
+
backgroundColor: "rgba(220,220,220,0.2)",
|
85
|
+
borderColor: "rgba(220,220,220,1)",
|
86
|
+
data: [65, 59, 80, 81, 56, 55, 40]
|
87
|
+
},
|
88
|
+
{
|
89
|
+
label: "My Second dataset",
|
90
|
+
backgroundColor: "rgba(151,187,205,0.2)",
|
91
|
+
borderColor: "rgba(151,187,205,1)",
|
92
|
+
data: [28, 48, 40, 19, 86, 27, 90]
|
93
|
+
}
|
94
|
+
]
|
95
|
+
};
|
96
|
+
var options = { ... };
|
97
|
+
new Chart(ctx, {
|
98
|
+
type: 'Line',
|
99
|
+
data: data,
|
100
|
+
options: options
|
101
|
+
});
|
102
|
+
```
|
103
|
+
|
104
|
+
The Ruby equivalent is:
|
105
|
+
|
106
|
+
```ruby
|
107
|
+
data = {
|
108
|
+
labels: ["January", "February", "March", "April", "May", "June", "July"],
|
109
|
+
datasets: [
|
110
|
+
{
|
111
|
+
label: "My First dataset",
|
112
|
+
backgroundColor: "rgba(220,220,220,0.2)",
|
113
|
+
borderColor: "rgba(220,220,220,1)",
|
114
|
+
data: [65, 59, 80, 81, 56, 55, 40]
|
115
|
+
},
|
116
|
+
{
|
117
|
+
label: "My Second dataset",
|
118
|
+
backgroundColor: "rgba(151,187,205,0.2)",
|
119
|
+
borderColor: "rgba(151,187,205,1)",
|
120
|
+
data: [28, 48, 40, 19, 86, 27, 90]
|
121
|
+
}
|
122
|
+
]
|
123
|
+
}
|
124
|
+
options = { ... }
|
125
|
+
<%= line_chart data, options %>
|
126
|
+
```
|
127
|
+
|
128
|
+
You can also use underscored symbols for keys, instead of the camelcase versions.
|
129
|
+
They will be converted to their lower camelcase counterparts on output.
|
130
|
+
|
131
|
+
```ruby
|
132
|
+
data = {
|
133
|
+
labels: ["January", "February", "March", "April", "May", "June", "July"],
|
134
|
+
datasets: [
|
135
|
+
{
|
136
|
+
label: "My First dataset",
|
137
|
+
background_color: "rgba(220,220,220,0.2)",
|
138
|
+
border_color: "rgba(220,220,220,1)",
|
139
|
+
data: [65, 59, 80, 81, 56, 55, 40]
|
140
|
+
},
|
141
|
+
{
|
142
|
+
label: "My Second dataset",
|
143
|
+
background_color: "rgba(151,187,205,0.2)",
|
144
|
+
border_color: "rgba(151,187,205,1)",
|
145
|
+
data: [28, 48, 40, 19, 86, 27, 90]
|
146
|
+
}
|
147
|
+
]
|
148
|
+
}
|
149
|
+
options = { ... }
|
150
|
+
<%= line_chart data, options %>
|
151
|
+
```
|
152
|
+
|
153
|
+
### Options
|
154
|
+
|
155
|
+
You can put anything in the `options` hash that Chart.js recognises. To pass a JavaScript function as an option value, wrap it in quotation marks to make it a string.
|
156
|
+
|
157
|
+
You can also use these non-Chart.js settings:
|
158
|
+
|
159
|
+
* `:class` - class of the DOM canvas element - default is `chart`.
|
160
|
+
* `:id` - id of the DOM canvas element - default is `chart-n` where `n` is the 0-based index of the chart on the page.
|
161
|
+
* `:width` - width of the canvas in px - default is `400`.
|
162
|
+
* `:height` - height of the canvas in px - default is `400`.
|
163
|
+
|
164
|
+
|
165
|
+
## Sample output
|
166
|
+
|
167
|
+
```html
|
168
|
+
<canvas id="chart-0" class="chart" width=400 height=400></canvas>
|
169
|
+
<script type="text/javascript">
|
170
|
+
//<![CDATA[
|
171
|
+
(function() {
|
172
|
+
var initChart = function() {
|
173
|
+
var ctx = document.getElementById("chart-0");
|
174
|
+
var chart = new Chart(ctx, {
|
175
|
+
type: "Line",
|
176
|
+
data = { ... };
|
177
|
+
options = { ... };
|
178
|
+
});
|
179
|
+
};
|
180
|
+
|
181
|
+
if (typeof Chart !== "undefined" && Chart !== null) {
|
182
|
+
initChart();
|
183
|
+
}
|
184
|
+
else {
|
185
|
+
if (window.addEventListener) {
|
186
|
+
window.addEventListener("load", initChart, false);
|
187
|
+
}
|
188
|
+
else if (window.attachEvent) {
|
189
|
+
window.attachEvent("onload", initChart);
|
190
|
+
}
|
191
|
+
}
|
192
|
+
})();
|
193
|
+
//]]>
|
194
|
+
</script>
|
195
|
+
```
|
196
|
+
|
197
|
+
The Javascript is actually written out on a single line but you get the idea.
|
198
|
+
|
199
|
+
|
200
|
+
## Intellectual Property
|
201
|
+
|
202
|
+
Copyright Andrew Stewart, AirBlade Software. Released under the MIT licence.
|
203
|
+
|
204
|
+
|
205
|
+
[Chart.js]: http://www.chartjs.org/
|
206
|
+
[linechart]: http://www.chartjs.org/docs/#line-chart
|
207
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,108 @@
|
|
1
|
+
require 'json'
|
2
|
+
require 'bigdecimal'
|
3
|
+
|
4
|
+
module Chartjs
|
5
|
+
module ChartHelpers
|
6
|
+
|
7
|
+
CHART_TYPES = %w[ bar bubble doughnut horizontal_bar line pie polar_area radar scatter ]
|
8
|
+
|
9
|
+
module Explicit
|
10
|
+
CHART_TYPES.each do |type|
|
11
|
+
define_method "chartjs_#{type}_chart" do |data, options = {}, plugins = {}| # def chartjs_polar_area_chart(data, options = {}, plugins = {})
|
12
|
+
chart type, data, options, plugins # chart 'polar_area', data, options, plugins
|
13
|
+
end # end
|
14
|
+
end
|
15
|
+
include Chartjs::ChartHelpers
|
16
|
+
end
|
17
|
+
|
18
|
+
module Implicit
|
19
|
+
CHART_TYPES.each do |type|
|
20
|
+
define_method "#{type}_chart" do |data, options = {}, plugins = {}| # def polar_area_chart(data, options = {}, plugins = {})
|
21
|
+
chart type, data, options, plugins # chart 'polar_area', data, options, plugins
|
22
|
+
end # end
|
23
|
+
end
|
24
|
+
include Chartjs::ChartHelpers
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def chart(type, data, options, plugins)
|
30
|
+
opts = options.dup
|
31
|
+
|
32
|
+
@chart_id ||= -1
|
33
|
+
element_id = opts.delete(:id) || "chart-#{@chart_id += 1}"
|
34
|
+
css_class = opts.delete(:class) || 'chart'
|
35
|
+
width = opts.delete(:width) || '400'
|
36
|
+
height = opts.delete(:height) || '400'
|
37
|
+
|
38
|
+
canvas = content_tag :canvas, '', id: element_id, class: css_class, width: width, height: height
|
39
|
+
container = content_tag :div, canvas, id: element_id + "-container", class: css_class, width: width, height: height
|
40
|
+
|
41
|
+
script = javascript_tag(nonce: true) do
|
42
|
+
<<-END.squish.html_safe
|
43
|
+
(function() {
|
44
|
+
var initChart = function() {
|
45
|
+
var ctx = document.getElementById(#{element_id.to_json});
|
46
|
+
var chart = new Chart(ctx, {
|
47
|
+
type: "#{camel_case type}",
|
48
|
+
data: #{to_javascript_string data},
|
49
|
+
options: #{to_javascript_string opts},
|
50
|
+
plugins: #{to_javascript_string plugins},
|
51
|
+
});
|
52
|
+
};
|
53
|
+
|
54
|
+
if (typeof Chart !== "undefined" && Chart !== null) {
|
55
|
+
initChart();
|
56
|
+
}
|
57
|
+
else {
|
58
|
+
/* W3C standard */
|
59
|
+
if (window.addEventListener) {
|
60
|
+
window.addEventListener("load", initChart, false);
|
61
|
+
}
|
62
|
+
/* IE */
|
63
|
+
else if (window.attachEvent) {
|
64
|
+
window.attachEvent("onload", initChart);
|
65
|
+
}
|
66
|
+
}
|
67
|
+
})();
|
68
|
+
END
|
69
|
+
end
|
70
|
+
|
71
|
+
container + script
|
72
|
+
end
|
73
|
+
|
74
|
+
# polar_area -> polarArea
|
75
|
+
def camel_case(string)
|
76
|
+
string.gsub(/_([a-z])/) { $1.upcase }
|
77
|
+
end
|
78
|
+
|
79
|
+
def to_javascript_string(element)
|
80
|
+
case element
|
81
|
+
when Hash
|
82
|
+
hash_elements = []
|
83
|
+
element.each do |key, value|
|
84
|
+
hash_elements << camel_case(key.to_s).to_json + ':' + to_javascript_string(value)
|
85
|
+
end
|
86
|
+
'{' + hash_elements.join(',') + '}'
|
87
|
+
when Array
|
88
|
+
array_elements = []
|
89
|
+
element.each do |value|
|
90
|
+
array_elements << to_javascript_string(value)
|
91
|
+
end
|
92
|
+
'[' + array_elements.join(',') + ']'
|
93
|
+
when String
|
94
|
+
if element.match(/^\s*function.*}\s*$/m)
|
95
|
+
# Raw-copy function definitions to the output without surrounding quotes.
|
96
|
+
element
|
97
|
+
else
|
98
|
+
element.to_json
|
99
|
+
end
|
100
|
+
when BigDecimal
|
101
|
+
element.to_s
|
102
|
+
else
|
103
|
+
element.to_json
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
end
|
108
|
+
end
|