chartkick 3.4.1 → 4.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5791c4748533e3fa0eaa1b2e1c6fbbffc726f8ab663f4590edfd0b9555894909
4
- data.tar.gz: fb3e99507dcdf7934cf1bfc25052ba14f8c109af05379a45b47fda728c2eb3b7
3
+ metadata.gz: a0db2fe39f763929c498438dc88f75dd51dbc9ace5ede4819e9f4d88c0cad72f
4
+ data.tar.gz: a1dbaf6eddf2bccfdc1e6f4b9fc114eea952eb021dedb7775832d3d5c2b8a389
5
5
  SHA512:
6
- metadata.gz: e417d6dcc2ae547cd63c7ded309329b112cfc9b6195c29c08d6192e3dbba4c630695815cc65f3d23e3b3833bdafc46bb04809b708362de8d98ee5f04568fbca3
7
- data.tar.gz: 730f4f49ced271c3f7e15e3a3e57d5f3f81c08d5a2bd0de3792ec6e2747c433bd37b185bd38db2dfaede7aa0c5ab0ad63255d30a675379e7035182df898bf836
6
+ metadata.gz: 0eccf48920536b5f4277cac070759d501ee77177805e06146db100e1818337de38e7c9ae45109362817da72425d3385993136012d57530266122deb09c670ea1
7
+ data.tar.gz: 55190f89407e99810198d82cda444c49f8877b86ea0148e8d408f06a4d3fc9935e07ec77414119f4d06ccd905bf9124b6678c044c2cd9d68e7de206b2901e3d3
data/CHANGELOG.md CHANGED
@@ -1,3 +1,35 @@
1
+ ## 4.0.3 (2021-04-10)
2
+
3
+ - Updated Chartkick.js to 4.0.3
4
+ - Updated Chart.js to 3.1.0
5
+
6
+ ## 4.0.2 (2021-04-06)
7
+
8
+ - Updated Chartkick.js to 4.0.2
9
+
10
+ ## 4.0.1 (2021-04-06)
11
+
12
+ - Fixed error with Uglifier
13
+ - Updated Chartkick.js to 4.0.1
14
+
15
+ ## 4.0.0 (2021-04-04)
16
+
17
+ - Added support for Chart.js 3
18
+ - Added `loading` option
19
+ - Made charts deferrable by default
20
+ - Set `nonce` automatically when present
21
+ - Prefer `empty` over `messages: {empty: ...}`
22
+ - Removed support for Ruby < 2.6 and Rails < 5.2
23
+ - Updated Chartkick.js to 4.0.0
24
+
25
+ Breaking changes
26
+
27
+ - Removed support for Chart.js 2
28
+
29
+ ## 3.4.2 (2020-10-20)
30
+
31
+ - Updated Chart.js to 2.9.4
32
+
1
33
  ## 3.4.1 (2020-10-06)
2
34
 
3
35
  - Relaxed validation for `width` and `height` options
data/LICENSE.txt CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2013-2019 Andrew Kane
1
+ Copyright (c) 2013-2021 Andrew Kane
2
2
 
3
3
  MIT License
4
4
 
data/README.md CHANGED
@@ -4,15 +4,17 @@ Create beautiful JavaScript charts with one line of Ruby. No more fighting with
4
4
 
5
5
  [See it in action](https://chartkick.com)
6
6
 
7
+ **Chartkick 4.0 was recently released** - see [how to upgrade](#upgrading)
8
+
7
9
  :fire: For admin charts and dashboards, check out [Blazer](https://github.com/ankane/blazer/), and for advanced visualizations, check out [Vega](https://github.com/ankane/vega)
8
10
 
9
11
  :two_hearts: A perfect companion to [Groupdate](https://github.com/ankane/groupdate), [Hightop](https://github.com/ankane/hightop), and [ActiveMedian](https://github.com/ankane/active_median)
10
12
 
11
- [![Build Status](https://travis-ci.org/ankane/chartkick.svg?branch=master)](https://travis-ci.org/ankane/chartkick)
13
+ [![Build Status](https://github.com/ankane/chartkick/workflows/build/badge.svg?branch=master)](https://github.com/ankane/chartkick/actions)
12
14
 
13
15
  ## Quick Start
14
16
 
15
- Add this line to your application's Gemfile:
17
+ Add this line to your applications Gemfile:
16
18
 
17
19
  ```ruby
18
20
  gem "chartkick"
@@ -27,8 +29,7 @@ yarn add chartkick chart.js
27
29
  And in `app/javascript/packs/application.js`, add:
28
30
 
29
31
  ```js
30
- require("chartkick")
31
- require("chart.js")
32
+ require("chartkick/chart.js")
32
33
  ```
33
34
 
34
35
  For Rails 5 / Sprockets, in `app/assets/javascripts/application.js`, add:
@@ -38,7 +39,7 @@ For Rails 5 / Sprockets, in `app/assets/javascripts/application.js`, add:
38
39
  //= require Chart.bundle
39
40
  ```
40
41
 
41
- This sets up Chartkick with Chart.js. For other charting libraries, see [detailed instructions](#installation).
42
+ This sets up Chartkick with [Chart.js](https://www.chartjs.org/). For other charting libraries, see [detailed instructions](#installation).
42
43
 
43
44
  ## Charts
44
45
 
@@ -151,7 +152,7 @@ Min and max values
151
152
  Min and max for x-axis - *Chart.js*
152
153
 
153
154
  ```erb
154
- <%= line_chart data, xmin: "2018-01-01", xmax: "2019-01-01" %>
155
+ <%= line_chart data, xmin: "2021-01-01", xmax: "2022-01-01" %>
155
156
  ```
156
157
 
157
158
  Colors
@@ -208,12 +209,6 @@ Specify legend position
208
209
  <%= line_chart data, legend: "bottom" %>
209
210
  ```
210
211
 
211
- Defer chart creation until after the page loads
212
-
213
- ```erb
214
- <%= line_chart data, defer: true %>
215
- ```
216
-
217
212
  Donut chart
218
213
 
219
214
  ```erb
@@ -262,16 +257,22 @@ Show insignificant zeros, useful for currency - *Chart.js, Highcharts*
262
257
  <%= line_chart data, round: 2, zeros: true %>
263
258
  ```
264
259
 
265
- Friendly byte sizes - *Chart.js 2.8+*
260
+ Friendly byte sizes - *Chart.js*
266
261
 
267
262
  ```erb
268
263
  <%= line_chart data, bytes: true %>
269
264
  ```
270
265
 
271
- Show a message when data is empty
266
+ Specify the message when data is loading
267
+
268
+ ```erb
269
+ <%= line_chart data, loading: "Loading..." %>
270
+ ```
271
+
272
+ Specify the message when data is empty
272
273
 
273
274
  ```erb
274
- <%= line_chart data, messages: {empty: "No data"} %>
275
+ <%= line_chart data, empty: "No data" %>
275
276
  ```
276
277
 
277
278
  Refresh data from a remote source every `n` seconds
@@ -310,7 +311,7 @@ Chartkick.options = {
310
311
  Customize the html
311
312
 
312
313
  ```ruby
313
- Chartkick.options[:html] = '<div id="%{id}" style="height: %{height};">Loading...</div>'
314
+ Chartkick.options[:html] = '<div id="%{id}" style="height: %{height};">%{loading}</div>'
314
315
  ```
315
316
 
316
317
  You capture the JavaScript in a content block with:
@@ -350,7 +351,7 @@ For multiple series, use the format
350
351
  Times can be a time or a string (strings are parsed)
351
352
 
352
353
  ```erb
353
- <%= line_chart({20.day.ago => 5, "2013-05-07 00:00:00 UTC" => 7}) %>
354
+ <%= line_chart({20.day.ago => 5, "2021-05-07 00:00:00 UTC" => 7}) %>
354
355
  ```
355
356
 
356
357
  ### Multiple Series
@@ -418,8 +419,6 @@ Next, choose your charting library.
418
419
  - [Google Charts](#google-charts)
419
420
  - [Highcharts](#highcharts)
420
421
 
421
- In the instructions below, `application.js` must be included **before** the charts in your views, unless using the `:content_for` option.
422
-
423
422
  ### Chart.js
424
423
 
425
424
  For Rails 6 / Webpacker, run:
@@ -431,8 +430,7 @@ yarn add chartkick chart.js
431
430
  And in `app/javascript/packs/application.js`, add:
432
431
 
433
432
  ```js
434
- require("chartkick")
435
- require("chart.js")
433
+ require("chartkick/chart.js")
436
434
  ```
437
435
 
438
436
  For Rails 5 / Sprockets, in `app/assets/javascripts/application.js`, add:
@@ -487,7 +485,7 @@ yarn add chartkick highcharts
487
485
  And in `app/javascript/packs/application.js`, add:
488
486
 
489
487
  ```js
490
- require("chartkick").use(require("highcharts"))
488
+ require("chartkick/highcharts")
491
489
  ```
492
490
 
493
491
  For Rails 5 / Sprockets, download [highcharts.js](https://code.highcharts.com/highcharts.js) into `vendor/assets/javascripts` (or use `yarn add highcharts` in Rails 5.1+), and in `app/assets/javascripts/application.js`, add:
@@ -505,6 +503,27 @@ Download [chartkick.js](https://raw.githubusercontent.com/ankane/chartkick/maste
505
503
  <script src="chartkick.js"></script>
506
504
  ```
507
505
 
506
+ Then include the charting library.
507
+
508
+ Chart.js - download [Chart.js](https://unpkg.com/chart.js@3/dist/chart.js) and the [date-fns adapter bundle](https://unpkg.com/chartjs-adapter-date-fns@2/dist/chartjs-adapter-date-fns.bundle.js)
509
+
510
+ ```html
511
+ <script src="chart.js"></script>
512
+ <script src="chartjs-adapter-date-fns.bundle.js"></script>
513
+ ```
514
+
515
+ Google Charts
516
+
517
+ ```html
518
+ <script src="https://www.gstatic.com/charts/loader.js"></script>
519
+ ```
520
+
521
+ Highcharts - download [highcharts.js](https://code.highcharts.com/highcharts.js)
522
+
523
+ ```html
524
+ <script src="highcharts.js"></script>
525
+ ```
526
+
508
527
  ### Multiple Libraries
509
528
 
510
529
  If more than one charting library is loaded, choose between them with:
@@ -593,19 +612,34 @@ Check out [chartkick.js](https://github.com/ankane/chartkick.js)
593
612
 
594
613
  ## Upgrading
595
614
 
596
- ### 3.0
615
+ ### 4.0
597
616
 
598
- Breaking changes
617
+ If you use Sprockets, update the gem and you’re good to go!
599
618
 
600
- - Removed support for Rails < 4.2
601
- - Removed chartkick.js from asset precompile (no longer needed)
602
- - Removed `xtype` option - numeric axes are automatically detected
603
- - Removed `window.Chartkick = {...}` way to set config - use `Chartkick.configure` instead
604
- - Removed support for the Google Charts jsapi loader - use loader.js instead
619
+ If you use Webpacker, run:
605
620
 
606
- ## Credits
621
+ ```sh
622
+ yarn upgrade chartkick --latest
623
+ ```
624
+
625
+ If you use Chart.js with Webpacker, also run:
607
626
 
608
- Chartkick uses [iso8601.js](https://github.com/Do/iso8601.js) to parse dates and times.
627
+ ```sh
628
+ yarn upgrade chart.js --latest
629
+ ```
630
+
631
+ And in `app/javascript/packs/application.js`, change:
632
+
633
+ ```js
634
+ require("chartkick")
635
+ require("chart.js")
636
+ ```
637
+
638
+ to:
639
+
640
+ ```js
641
+ require("chartkick/chart.js")
642
+ ```
609
643
 
610
644
  ## History
611
645
 
@@ -37,17 +37,22 @@ module Chartkick
37
37
 
38
38
  private
39
39
 
40
+ # don't break out options since need to merge with default options
40
41
  def chartkick_chart(klass, data_source, **options)
41
- @chartkick_chart_id ||= 0
42
42
  options = chartkick_deep_merge(Chartkick.options, options)
43
+
44
+ @chartkick_chart_id ||= 0
43
45
  element_id = options.delete(:id) || "chart-#{@chartkick_chart_id += 1}"
46
+
44
47
  height = (options.delete(:height) || "300px").to_s
45
48
  width = (options.delete(:width) || "100%").to_s
46
49
  defer = !!options.delete(:defer)
50
+
47
51
  # content_for: nil must override default
48
52
  content_for = options.key?(:content_for) ? options.delete(:content_for) : Chartkick.content_for
49
53
 
50
- nonce = options.delete(:nonce)
54
+ nonce = options.fetch(:nonce, true)
55
+ options.delete(:nonce)
51
56
  if nonce == true
52
57
  # Secure Headers also defines content_security_policy_nonce but it takes an argument
53
58
  # Rails 5.2 overrides this method, but earlier versions do not
@@ -57,34 +62,36 @@ module Chartkick
57
62
  elsif respond_to?(:content_security_policy_script_nonce)
58
63
  # Secure Headers
59
64
  nonce = content_security_policy_script_nonce
65
+ else
66
+ nonce = nil
60
67
  end
61
68
  end
62
69
  nonce_html = nonce ? " nonce=\"#{ERB::Util.html_escape(nonce)}\"" : nil
63
70
 
64
71
  # html vars
65
72
  html_vars = {
66
- id: element_id
67
- }
68
- html_vars.each_key do |k|
69
- html_vars[k] = ERB::Util.html_escape(html_vars[k])
70
- end
71
-
72
- # css vars
73
- css_vars = {
73
+ id: element_id,
74
74
  height: height,
75
- width: width
75
+ width: width,
76
+ # don't delete loading option since it needs to be passed to JS
77
+ loading: options[:loading] || "Loading..."
76
78
  }
77
- css_vars.each_key do |k|
79
+
80
+ [:height, :width].each do |k|
78
81
  # limit to alphanumeric and % for simplicity
79
82
  # this prevents things like calc() but safety is the priority
80
83
  # dot does not need escaped in square brackets
81
- raise ArgumentError, "Invalid #{k}" unless css_vars[k] =~ /\A[a-zA-Z0-9%.]*\z/
82
- # we limit above, but escape for safety as fail-safe
84
+ raise ArgumentError, "Invalid #{k}" unless html_vars[k] =~ /\A[a-zA-Z0-9%.]*\z/
85
+ end
86
+
87
+ html_vars.each_key do |k|
88
+ # escape all variables
89
+ # we already limit height and width above, but escape for safety as fail-safe
83
90
  # to prevent XSS injection in worse-case scenario
84
- css_vars[k] = ERB::Util.html_escape(css_vars[k])
91
+ html_vars[k] = ERB::Util.html_escape(html_vars[k])
85
92
  end
86
93
 
87
- html = (options.delete(:html) || %(<div id="%{id}" style="height: %{height}; width: %{width}; text-align: center; color: #999; line-height: %{height}; font-size: 14px; font-family: 'Lucida Grande', 'Lucida Sans Unicode', Verdana, Arial, Helvetica, sans-serif;">Loading...</div>)) % html_vars.merge(css_vars)
94
+ html = (options.delete(:html) || %(<div id="%{id}" style="height: %{height}; width: %{width}; text-align: center; color: #999; line-height: %{height}; font-size: 14px; font-family: 'Lucida Grande', 'Lucida Sans Unicode', Verdana, Arial, Helvetica, sans-serif;">%{loading}</div>)) % html_vars
88
95
 
89
96
  # js vars
90
97
  js_vars = {
@@ -98,35 +105,33 @@ module Chartkick
98
105
  end
99
106
  createjs = "new Chartkick[%{type}](%{id}, %{data}, %{options});" % js_vars
100
107
 
101
- if defer
102
- # TODO remove type in 4.0
103
- js = <<JS
104
- <script type="text/javascript"#{nonce_html}>
105
- (function() {
106
- var createChart = function() { #{createjs} };
107
- if (window.addEventListener) {
108
- window.addEventListener("load", createChart, true);
109
- } else if (window.attachEvent) {
110
- window.attachEvent("onload", createChart);
111
- } else {
112
- createChart();
113
- }
114
- })();
115
- </script>
116
- JS
117
- else
118
- # TODO remove type in 4.0
119
- js = <<JS
120
- <script type="text/javascript"#{nonce_html}>
121
- #{createjs}
122
- </script>
123
- JS
124
- end
108
+ warn "[chartkick] The defer option is no longer needed and can be removed" if defer
109
+
110
+ # Turbolinks preview restores the DOM except for painted <canvas>
111
+ # since it uses cloneNode(true) - https://developer.mozilla.org/en-US/docs/Web/API/Node/
112
+ #
113
+ # don't rerun JS on preview to prevent
114
+ # 1. animation
115
+ # 2. loading data from URL
116
+ js = <<~JS
117
+ <script#{nonce_html}>
118
+ (function() {
119
+ if (document.documentElement.hasAttribute("data-turbolinks-preview")) return;
120
+
121
+ var createChart = function() { #{createjs} };
122
+ if ("Chartkick" in window) {
123
+ createChart();
124
+ } else {
125
+ window.addEventListener("chartkick:load", createChart, true);
126
+ }
127
+ })();
128
+ </script>
129
+ JS
125
130
 
126
131
  if content_for
127
132
  content_for(content_for) { js.respond_to?(:html_safe) ? js.html_safe : js }
128
133
  else
129
- html += js
134
+ html += "\n#{js}"
130
135
  end
131
136
 
132
137
  html.respond_to?(:html_safe) ? html.html_safe : html
@@ -1,3 +1,3 @@
1
1
  module Chartkick
2
- VERSION = "3.4.1"
2
+ VERSION = "4.0.3"
3
3
  end
@@ -0,0 +1,9 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014-2021 Chart.js Contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,9 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Chart.js Contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013-2021 Andrew Kane
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.
@@ -0,0 +1,20 @@
1
+ Copyright (C) 2020 Sasha Koss and Lesha Koss
2
+
3
+ # License
4
+
5
+ date-fns is licensed under the [MIT license](http://kossnocorp.mit-license.org).
6
+ Read more about MIT at [TLDRLegal](https://tldrlegal.com/license/mit-license).
7
+
8
+ ---
9
+
10
+ Text from http://kossnocorp.mit-license.org
11
+
12
+ The MIT License (MIT)
13
+
14
+ Copyright © 2021 Sasha Koss
15
+
16
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
17
+
18
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
19
+
20
+ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.