chartkick 4.2.1 → 5.0.1

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: 360203297ee43cced5a6adbdeef339d635ed4792c06aea02071aaa1a7c2d93dd
4
- data.tar.gz: 19261b37a5a18daa6b5a7d46887aa5e4b6d6c7e8cae45b763c6ff4246dfaae00
3
+ metadata.gz: 9ac4b71bb9edbf35b307e350adf2fc5ebfed0e23618ca5e079f37e3ce548f68c
4
+ data.tar.gz: b5a6a2499a5e9835d3dcc7b002a881d60d85a66efc45c79931cc9897cdeca6de
5
5
  SHA512:
6
- metadata.gz: fcfaffd5359b8f0bfd62d70bdc33964d0849fbbc28704b2f143280e8f0938c0b3328b7c0bfabae39be6d9d9b518d57b42d74d6d6b2471dc297a26a4a791fa028
7
- data.tar.gz: bf2cbfb80736502bc50a24d18e38f739d65e75374081fbd187694e5196eb9a73a2012ce765e8c02a3eadbfd2acf12ae1a6fd856cda6af2ac98109baedafa1d98
6
+ metadata.gz: 709e242eff3d7c82c2c78dee8f0693f055819e3a0e24db1d5d58fe4d3564d1309a2477d45fc2b261685a6d60f81d6a2b6ace5a7c03dce48db1fea4cdb7bce4ae
7
+ data.tar.gz: c866cf49a27a03a16fdaa276b5b002b5eb36378b8f16ce062a3d2a08a56c67b47d286035f5e47cea8bf549e33174d6581a0024f4c8e96ca32849ef236c91e24d
data/CHANGELOG.md CHANGED
@@ -1,3 +1,15 @@
1
+ ## 5.0.1 (2023-01-19)
2
+
3
+ - Updated Chartkick.js to 5.0.1
4
+
5
+ ## 5.0.0 (2023-01-18)
6
+
7
+ - Added support for Chart.js 4
8
+ - Changed `chart_json` to be defined on `Hash` and `Array` instead of `Enumerable`
9
+ - Updated Chartkick.js to 5.0.0
10
+ - Removed support for Chart.js 3
11
+ - Removed support for Ruby < 2.7 and Rails < 6
12
+
1
13
  ## 4.2.1 (2022-08-02)
2
14
 
3
15
  - Updated Chart.js to 3.9.0
data/LICENSE.txt CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2013-2021 Andrew Kane
1
+ Copyright (c) 2013-2023 Andrew Kane
2
2
 
3
3
  MIT License
4
4
 
data/README.md CHANGED
@@ -4,6 +4,8 @@ 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 5.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)
@@ -18,16 +20,16 @@ Add this line to your application’s Gemfile:
18
20
  gem "chartkick"
19
21
  ```
20
22
 
21
- Then follow the instructions for your framework:
23
+ Then follow the instructions for your JavaScript setup:
22
24
 
23
- - [Rails 7 / Importmap](#rails-7--importmap)
24
- - [Rails 7 / esbuild or Webpack](#rails-7--esbuild-or-webpack)
25
- - [Rails 6 / Webpacker](#rails-6--webpacker)
26
- - [Rails 5 / Sprockets](#rails-5--sprockets)
25
+ - [Importmap](#importmap) (Rails 7 default)
26
+ - [esbuild, rollup.js, or Webpack](#esbuild-rollupjs-or-webpack)
27
+ - [Webpacker](#webpacker) (Rails 6 default)
28
+ - [Sprockets](#sprockets)
27
29
 
28
30
  This sets up Chartkick with [Chart.js](https://www.chartjs.org/). For other charting libraries and frameworks, see [detailed instructions](#installation).
29
31
 
30
- ### Rails 7 / Importmap
32
+ ### Importmap
31
33
 
32
34
  In `config/importmap.rb`, add:
33
35
 
@@ -43,7 +45,7 @@ import "chartkick"
43
45
  import "Chart.bundle"
44
46
  ```
45
47
 
46
- ### Rails 7 / esbuild or Webpack
48
+ ### esbuild, rollup.js, or Webpack
47
49
 
48
50
  Run:
49
51
 
@@ -57,7 +59,9 @@ And in `app/javascript/application.js`, add:
57
59
  import "chartkick/chart.js"
58
60
  ```
59
61
 
60
- ### Rails 6 / Webpacker
62
+ Note: For rollup.js, this requires `format: "iife"` in `rollup.config.js`.
63
+
64
+ ### Webpacker
61
65
 
62
66
  Run:
63
67
 
@@ -71,7 +75,7 @@ And in `app/javascript/packs/application.js`, add:
71
75
  import "chartkick/chart.js"
72
76
  ```
73
77
 
74
- ### Rails 5 / Sprockets
78
+ ### Sprockets
75
79
 
76
80
  In `app/assets/javascripts/application.js`, add:
77
81
 
@@ -447,7 +451,7 @@ gem "chartkick"
447
451
 
448
452
  Next, choose your charting library.
449
453
 
450
- - [Chart.js](#chart-js)
454
+ - [Chart.js](#chartjs)
451
455
  - [Google Charts](#google-charts)
452
456
  - [Highcharts](#highcharts)
453
457
 
@@ -584,7 +588,7 @@ Download [chartkick.js](https://raw.githubusercontent.com/ankane/chartkick/maste
584
588
 
585
589
  Then include the charting library.
586
590
 
587
- 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)
591
+ Chart.js - download [Chart.js](https://unpkg.com/chart.js@4/dist/chart.umd.js) and the [date-fns adapter bundle](https://unpkg.com/chartjs-adapter-date-fns@3/dist/chartjs-adapter-date-fns.bundle.js)
588
592
 
589
593
  ```html
590
594
  <script src="chart.js"></script>
@@ -691,35 +695,22 @@ Check out [chartkick.js](https://github.com/ankane/chartkick.js)
691
695
 
692
696
  ## Upgrading
693
697
 
694
- ### 4.0
698
+ ### 5.0
695
699
 
696
- If you use Sprockets, update the gem and you’re good to go!
700
+ If you use Importmaps or Sprockets, update the gem and you’re good to go!
697
701
 
698
- If you use Webpacker, run:
702
+ If you use esbuild, Webpack, or Webpacker, run:
699
703
 
700
704
  ```sh
701
705
  yarn upgrade chartkick --latest
702
706
  ```
703
707
 
704
- If you use Chart.js with Webpacker, also run:
708
+ If you use Chart.js with esbuild, Webpack, or Webpacker, also run:
705
709
 
706
710
  ```sh
707
711
  yarn upgrade chart.js --latest
708
712
  ```
709
713
 
710
- And in `app/javascript/packs/application.js`, change:
711
-
712
- ```js
713
- require("chartkick")
714
- require("chart.js")
715
- ```
716
-
717
- to:
718
-
719
- ```js
720
- require("chartkick/chart.js")
721
- ```
722
-
723
714
  ## History
724
715
 
725
716
  View the [changelog](https://github.com/ankane/chartkick/blob/master/CHANGELOG.md)
@@ -0,0 +1,26 @@
1
+ # for both multiple series and
2
+ # making sure hash order is preserved in JavaScript
3
+
4
+ class Array
5
+ def chart_json
6
+ map do |v|
7
+ if v.is_a?(Hash) && v[:data].is_a?(Hash)
8
+ v = v.dup
9
+ v[:data] = v[:data].to_a
10
+ end
11
+ v
12
+ end.to_json
13
+ end
14
+ end
15
+
16
+ class Hash
17
+ def chart_json
18
+ if (key = keys.first) && key.is_a?(Array) && key.size == 2
19
+ group_by { |k, _v| k[0] }.map do |name, data|
20
+ {name: name, data: data.map { |k, v| [k[1], v] }}
21
+ end
22
+ else
23
+ to_a
24
+ end.to_json
25
+ end
26
+ end
@@ -1,6 +1,3 @@
1
- require "json"
2
- require "erb"
3
-
4
1
  module Chartkick
5
2
  module Helper
6
3
  def line_chart(data_source, **options)
@@ -39,7 +36,7 @@ module Chartkick
39
36
 
40
37
  # don't break out options since need to merge with default options
41
38
  def chartkick_chart(klass, data_source, **options)
42
- options = chartkick_deep_merge(Chartkick.options, options)
39
+ options = Chartkick::Utils.deep_merge(Chartkick.options, options)
43
40
 
44
41
  @chartkick_chart_id ||= 0
45
42
  element_id = options.delete(:id) || "chart-#{@chartkick_chart_id += 1}"
@@ -57,7 +54,7 @@ module Chartkick
57
54
  # Secure Headers also defines content_security_policy_nonce but it takes an argument
58
55
  # Rails 5.2 overrides this method, but earlier versions do not
59
56
  if respond_to?(:content_security_policy_nonce) && (content_security_policy_nonce rescue nil)
60
- # Rails 5.2
57
+ # Rails 5.2+
61
58
  nonce = content_security_policy_nonce
62
59
  elsif respond_to?(:content_security_policy_script_nonce)
63
60
  # Secure Headers
@@ -101,7 +98,7 @@ module Chartkick
101
98
  options: options.to_json
102
99
  }
103
100
  js_vars.each_key do |k|
104
- js_vars[k] = chartkick_json_escape(js_vars[k])
101
+ js_vars[k] = Chartkick::Utils.json_escape(js_vars[k])
105
102
  end
106
103
  createjs = "new Chartkick[%{type}](%{id}, %{data}, %{options});" % js_vars
107
104
 
@@ -137,26 +134,5 @@ module Chartkick
137
134
 
138
135
  html.respond_to?(:html_safe) ? html.html_safe : html
139
136
  end
140
-
141
- # https://github.com/rails/rails/blob/master/activesupport/lib/active_support/core_ext/hash/deep_merge.rb
142
- def chartkick_deep_merge(hash_a, hash_b)
143
- hash_a = hash_a.dup
144
- hash_b.each_pair do |k, v|
145
- tv = hash_a[k]
146
- hash_a[k] = tv.is_a?(Hash) && v.is_a?(Hash) ? chartkick_deep_merge(tv, v) : v
147
- end
148
- hash_a
149
- end
150
-
151
- # from https://github.com/rails/rails/blob/master/activesupport/lib/active_support/core_ext/string/output_safety.rb
152
- JSON_ESCAPE = { "&" => '\u0026', ">" => '\u003e', "<" => '\u003c', "\u2028" => '\u2028', "\u2029" => '\u2029' }
153
- JSON_ESCAPE_REGEXP = /[\u2028\u2029&><]/u
154
- def chartkick_json_escape(s)
155
- if ERB::Util.respond_to?(:json_escape)
156
- ERB::Util.json_escape(s)
157
- else
158
- s.to_s.gsub(JSON_ESCAPE_REGEXP, JSON_ESCAPE)
159
- end
160
- end
161
137
  end
162
138
  end
@@ -0,0 +1,24 @@
1
+ module Chartkick
2
+ module Utils
3
+ # https://github.com/rails/rails/blob/master/activesupport/lib/active_support/core_ext/hash/deep_merge.rb
4
+ def self.deep_merge(hash_a, hash_b)
5
+ hash_a = hash_a.dup
6
+ hash_b.each_pair do |k, v|
7
+ tv = hash_a[k]
8
+ hash_a[k] = tv.is_a?(Hash) && v.is_a?(Hash) ? deep_merge(tv, v) : v
9
+ end
10
+ hash_a
11
+ end
12
+
13
+ # from https://github.com/rails/rails/blob/master/activesupport/lib/active_support/core_ext/string/output_safety.rb
14
+ JSON_ESCAPE = { "&" => '\u0026', ">" => '\u003e', "<" => '\u003c', "\u2028" => '\u2028', "\u2029" => '\u2029' }
15
+ JSON_ESCAPE_REGEXP = /[\u2028\u2029&><]/u
16
+ def self.json_escape(s)
17
+ if ERB::Util.respond_to?(:json_escape)
18
+ ERB::Util.json_escape(s)
19
+ else
20
+ s.to_s.gsub(JSON_ESCAPE_REGEXP, JSON_ESCAPE)
21
+ end
22
+ end
23
+ end
24
+ end
@@ -1,3 +1,3 @@
1
1
  module Chartkick
2
- VERSION = "4.2.1"
2
+ VERSION = "5.0.1"
3
3
  end
data/lib/chartkick.rb CHANGED
@@ -1,11 +1,16 @@
1
+ # stdlib
2
+ require "erb"
3
+ require "json"
4
+
1
5
  # modules
2
- require "chartkick/enumerable"
3
- require "chartkick/helper"
4
- require "chartkick/version"
6
+ require_relative "chartkick/core_ext"
7
+ require_relative "chartkick/helper"
8
+ require_relative "chartkick/utils"
9
+ require_relative "chartkick/version"
5
10
 
6
11
  # integrations
7
- require "chartkick/engine" if defined?(Rails)
8
- require "chartkick/sinatra" if defined?(Sinatra)
12
+ require_relative "chartkick/engine" if defined?(Rails)
13
+ require_relative "chartkick/sinatra" if defined?(Sinatra)
9
14
 
10
15
  if defined?(ActiveSupport.on_load)
11
16
  ActiveSupport.on_load(:action_view) do
@@ -0,0 +1,9 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018-2021 Jukka Kurkela
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.