chartkick 3.4.0 → 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: 5afacd4c10d0cfddc6a2a660efead206ee042e80d48dd749f9de79feb365c1fd
4
- data.tar.gz: c7fc99b0b2b467a6326df99f5832de2b3674f5f80415749cf57177142eecfc45
3
+ metadata.gz: 9ac4b71bb9edbf35b307e350adf2fc5ebfed0e23618ca5e079f37e3ce548f68c
4
+ data.tar.gz: b5a6a2499a5e9835d3dcc7b002a881d60d85a66efc45c79931cc9897cdeca6de
5
5
  SHA512:
6
- metadata.gz: 75b1793b427c5d9d4604b813773253adb864b6134fad48f952c72377f75da40c8daa19eaf06fd7ce0b2da31a1ba436cfe54bde318ac849ec2080652582ac7076
7
- data.tar.gz: 8f47e12f6e9c746871d02f0d5bd3093da1017c5a222837696b5b0d7b7e4008c400c8556e4f85ec7b0ccd79c15a9cb510a91a7d9ecf896d1a106b7c82e68ddc36
6
+ metadata.gz: 709e242eff3d7c82c2c78dee8f0693f055819e3a0e24db1d5d58fe4d3564d1309a2477d45fc2b261685a6d60f81d6a2b6ace5a7c03dce48db1fea4cdb7bce4ae
7
+ data.tar.gz: c866cf49a27a03a16fdaa276b5b002b5eb36378b8f16ce062a3d2a08a56c67b47d286035f5e47cea8bf549e33174d6581a0024f4c8e96ca32849ef236c91e24d
data/CHANGELOG.md CHANGED
@@ -1,6 +1,94 @@
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
+
13
+ ## 4.2.1 (2022-08-02)
14
+
15
+ - Updated Chart.js to 3.9.0
16
+
17
+ ## 4.2.0 (2022-06-12)
18
+
19
+ - Updated Chartkick.js to 4.2.0
20
+ - Updated Chart.js to 3.8.0
21
+
22
+ ## 4.1.3 (2022-01-15)
23
+
24
+ - Support for `importmap-rails` is no longer experimental
25
+ - Updated Chart.js to 3.7.0
26
+ - Fixed asset precompilation error with `importmap-rails`
27
+
28
+ ## 4.1.2 (2021-11-06)
29
+
30
+ - Removed automatic pinning for `importmap-rails` (still experimental)
31
+
32
+ ## 4.1.1 (2021-11-06)
33
+
34
+ - Updated Chartkick.js to 4.1.1
35
+ - Updated Chart.js to 3.6.0
36
+
37
+ ## 4.1.0 (2021-10-23)
38
+
39
+ - Added support for Turbo
40
+ - Added experimental support for `importmap-rails`
41
+ - Updated Chartkick.js to 4.1.0
42
+
43
+ ## 4.0.5 (2021-07-07)
44
+
45
+ - Updated Chartkick.js to 4.0.5
46
+ - Updated Chart.js to 3.4.1
47
+
48
+ ## 4.0.4 (2021-05-01)
49
+
50
+ - Updated Chartkick.js to 4.0.4
51
+ - Updated Chart.js to 3.2.1
52
+
53
+ ## 4.0.3 (2021-04-10)
54
+
55
+ - Updated Chartkick.js to 4.0.3
56
+ - Updated Chart.js to 3.1.0
57
+
58
+ ## 4.0.2 (2021-04-06)
59
+
60
+ - Updated Chartkick.js to 4.0.2
61
+
62
+ ## 4.0.1 (2021-04-06)
63
+
64
+ - Fixed error with Uglifier
65
+ - Updated Chartkick.js to 4.0.1
66
+
67
+ ## 4.0.0 (2021-04-04)
68
+
69
+ - Added support for Chart.js 3
70
+ - Added `loading` option
71
+ - Made charts deferrable by default
72
+ - Set `nonce` automatically when present
73
+ - Prefer `empty` over `messages: {empty: ...}`
74
+ - Removed support for Ruby < 2.6 and Rails < 5.2
75
+ - Updated Chartkick.js to 4.0.0
76
+
77
+ Breaking changes
78
+
79
+ - Removed support for Chart.js 2
80
+
81
+ ## 3.4.2 (2020-10-20)
82
+
83
+ - Updated Chart.js to 2.9.4
84
+
85
+ ## 3.4.1 (2020-10-06)
86
+
87
+ - Relaxed validation for `width` and `height` options
88
+
1
89
  ## 3.4.0 (2020-08-04)
2
90
 
3
- - Fixed CSS injection with `width` and `height` options
91
+ - Fixed CSS injection with `width` and `height` options - [more info](https://github.com/ankane/chartkick/issues/546)
4
92
 
5
93
  ## 3.3.2 (2020-07-23)
6
94
 
@@ -9,7 +97,7 @@
9
97
  ## 3.3.1 (2019-12-26)
10
98
 
11
99
  - Updated Chart.js to 2.9.3
12
- - Fixed deprecating warnings in Ruby 2.7
100
+ - Fixed deprecation warnings in Ruby 2.7
13
101
 
14
102
  ## 3.3.0 (2019-11-09)
15
103
 
@@ -27,7 +115,7 @@
27
115
 
28
116
  ## 3.2.0 (2019-06-04)
29
117
 
30
- - Fixed XSS vulnerability - see [#488](https://github.com/ankane/chartkick/issues/488)
118
+ - Fixed XSS vulnerability - [more info](https://github.com/ankane/chartkick/issues/488)
31
119
 
32
120
  ## 3.1.0 (2019-05-26)
33
121
 
data/LICENSE.txt CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2013-2019 Andrew Kane
1
+ Copyright (c) 2013-2023 Andrew Kane
2
2
 
3
3
  MIT License
4
4
 
data/README.md CHANGED
@@ -4,21 +4,66 @@ 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
- :fire: For admin charts and dashboards, check out [Blazer](https://github.com/ankane/blazer/)
7
+ **Chartkick 5.0 was recently released** - see [how to upgrade](#upgrading)
8
+
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"
19
21
  ```
20
22
 
21
- For Rails 6 / Webpacker, run:
23
+ Then follow the instructions for your JavaScript setup:
24
+
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)
29
+
30
+ This sets up Chartkick with [Chart.js](https://www.chartjs.org/). For other charting libraries and frameworks, see [detailed instructions](#installation).
31
+
32
+ ### Importmap
33
+
34
+ In `config/importmap.rb`, add:
35
+
36
+ ```ruby
37
+ pin "chartkick", to: "chartkick.js"
38
+ pin "Chart.bundle", to: "Chart.bundle.js"
39
+ ```
40
+
41
+ And in `app/javascript/application.js`, add:
42
+
43
+ ```js
44
+ import "chartkick"
45
+ import "Chart.bundle"
46
+ ```
47
+
48
+ ### esbuild, rollup.js, or Webpack
49
+
50
+ Run:
51
+
52
+ ```sh
53
+ yarn add chartkick chart.js
54
+ ```
55
+
56
+ And in `app/javascript/application.js`, add:
57
+
58
+ ```js
59
+ import "chartkick/chart.js"
60
+ ```
61
+
62
+ Note: For rollup.js, this requires `format: "iife"` in `rollup.config.js`.
63
+
64
+ ### Webpacker
65
+
66
+ Run:
22
67
 
23
68
  ```sh
24
69
  yarn add chartkick chart.js
@@ -27,19 +72,18 @@ yarn add chartkick chart.js
27
72
  And in `app/javascript/packs/application.js`, add:
28
73
 
29
74
  ```js
30
- require("chartkick")
31
- require("chart.js")
75
+ import "chartkick/chart.js"
32
76
  ```
33
77
 
34
- For Rails 5 / Sprockets, in `app/assets/javascripts/application.js`, add:
78
+ ### Sprockets
79
+
80
+ In `app/assets/javascripts/application.js`, add:
35
81
 
36
82
  ```js
37
83
  //= require chartkick
38
84
  //= require Chart.bundle
39
85
  ```
40
86
 
41
- This sets up Chartkick with Chart.js. For other charting libraries, see [detailed instructions](#installation).
42
-
43
87
  ## Charts
44
88
 
45
89
  Line chart
@@ -97,9 +141,10 @@ Timeline - *Google Charts*
97
141
  Multiple series
98
142
 
99
143
  ```erb
100
- <%= line_chart @goals.map { |goal|
101
- {name: goal.name, data: goal.feats.group_by_week(:created_at).count}
102
- } %>
144
+ <%= line_chart [
145
+ {name: "Workout", data: {"2021-01-01" => 3, "2021-01-02" => 4}},
146
+ {name: "Call parents", data: {"2021-01-01" => 5, "2021-01-02" => 3}}
147
+ ] %>
103
148
  ```
104
149
 
105
150
  or
@@ -108,7 +153,23 @@ or
108
153
  <%= line_chart Feat.group(:goal_id).group_by_week(:created_at).count %>
109
154
  ```
110
155
 
111
- ### Say Goodbye To Timeouts
156
+ ## Data
157
+
158
+ Data can be a hash, array, or URL.
159
+
160
+ #### Hash
161
+
162
+ ```erb
163
+ <%= line_chart({"2021-01-01" => 2, "2021-01-02" => 3}) %>
164
+ ```
165
+
166
+ #### Array
167
+
168
+ ```erb
169
+ <%= line_chart [["2021-01-01", 2], ["2021-01-02", 3]] %>
170
+ ```
171
+
172
+ #### URL
112
173
 
113
174
  Make your pages load super fast and stop worrying about timeouts. Give each chart its own endpoint.
114
175
 
@@ -132,7 +193,7 @@ For multiple series, add `chart_json` at the end.
132
193
  render json: Task.group(:goal_id).group_by_day(:completed_at).count.chart_json
133
194
  ```
134
195
 
135
- ### Options
196
+ ## Options
136
197
 
137
198
  Id, width, and height
138
199
 
@@ -151,7 +212,7 @@ Min and max values
151
212
  Min and max for x-axis - *Chart.js*
152
213
 
153
214
  ```erb
154
- <%= line_chart data, xmin: "2018-01-01", xmax: "2019-01-01" %>
215
+ <%= line_chart data, xmin: "2021-01-01", xmax: "2022-01-01" %>
155
216
  ```
156
217
 
157
218
  Colors
@@ -208,12 +269,6 @@ Specify legend position
208
269
  <%= line_chart data, legend: "bottom" %>
209
270
  ```
210
271
 
211
- Defer chart creation until after the page loads
212
-
213
- ```erb
214
- <%= line_chart data, defer: true %>
215
- ```
216
-
217
272
  Donut chart
218
273
 
219
274
  ```erb
@@ -262,16 +317,22 @@ Show insignificant zeros, useful for currency - *Chart.js, Highcharts*
262
317
  <%= line_chart data, round: 2, zeros: true %>
263
318
  ```
264
319
 
265
- Friendly byte sizes - *Chart.js 2.8+*
320
+ Friendly byte sizes - *Chart.js*
266
321
 
267
322
  ```erb
268
323
  <%= line_chart data, bytes: true %>
269
324
  ```
270
325
 
271
- Show a message when data is empty
326
+ Specify the message when data is loading
327
+
328
+ ```erb
329
+ <%= line_chart data, loading: "Loading..." %>
330
+ ```
331
+
332
+ Specify the message when data is empty
272
333
 
273
334
  ```erb
274
- <%= line_chart data, messages: {empty: "No data"} %>
335
+ <%= line_chart data, empty: "No data" %>
275
336
  ```
276
337
 
277
338
  Refresh data from a remote source every `n` seconds
@@ -310,7 +371,7 @@ Chartkick.options = {
310
371
  Customize the html
311
372
 
312
373
  ```ruby
313
- Chartkick.options[:html] = '<div id="%{id}" style="height: %{height};">Loading...</div>'
374
+ Chartkick.options[:html] = '<div id="%{id}" style="height: %{height};">%{loading}</div>'
314
375
  ```
315
376
 
316
377
  You capture the JavaScript in a content block with:
@@ -329,30 +390,6 @@ For Padrino, use `yield_content` instead of `yield`.
329
390
 
330
391
  This is great for including all of your JavaScript at the bottom of the page.
331
392
 
332
- ### Data
333
-
334
- Pass data as a hash or array
335
-
336
- ```erb
337
- <%= pie_chart({"Football" => 10, "Basketball" => 5}) %>
338
- <%= pie_chart [["Football", 10], ["Basketball", 5]] %>
339
- ```
340
-
341
- For multiple series, use the format
342
-
343
- ```erb
344
- <%= line_chart [
345
- {name: "Series A", data: series_a},
346
- {name: "Series B", data: series_b}
347
- ] %>
348
- ```
349
-
350
- Times can be a time or a string (strings are parsed)
351
-
352
- ```erb
353
- <%= line_chart({20.day.ago => 5, "2013-05-07 00:00:00 UTC" => 7}) %>
354
- ```
355
-
356
393
  ### Multiple Series
357
394
 
358
395
  You can pass a few options with a series:
@@ -414,14 +451,26 @@ gem "chartkick"
414
451
 
415
452
  Next, choose your charting library.
416
453
 
417
- - [Chart.js](#chart-js)
454
+ - [Chart.js](#chartjs)
418
455
  - [Google Charts](#google-charts)
419
456
  - [Highcharts](#highcharts)
420
457
 
421
- In the instructions below, `application.js` must be included **before** the charts in your views, unless using the `:content_for` option.
422
-
423
458
  ### Chart.js
424
459
 
460
+ For Rails 7 / Importmap, in `config/importmap.rb`, add:
461
+
462
+ ```ruby
463
+ pin "chartkick", to: "chartkick.js"
464
+ pin "Chart.bundle", to: "Chart.bundle.js"
465
+ ```
466
+
467
+ And in `app/javascript/application.js`, add:
468
+
469
+ ```js
470
+ import "chartkick"
471
+ import "Chart.bundle"
472
+ ```
473
+
425
474
  For Rails 6 / Webpacker, run:
426
475
 
427
476
  ```sh
@@ -431,8 +480,7 @@ yarn add chartkick chart.js
431
480
  And in `app/javascript/packs/application.js`, add:
432
481
 
433
482
  ```js
434
- require("chartkick")
435
- require("chart.js")
483
+ import "chartkick/chart.js"
436
484
  ```
437
485
 
438
486
  For Rails 5 / Sprockets, in `app/assets/javascripts/application.js`, add:
@@ -450,6 +498,18 @@ In your layout or views, add:
450
498
  <%= javascript_include_tag "https://www.gstatic.com/charts/loader.js" %>
451
499
  ```
452
500
 
501
+ For Rails 7 / Importmap, in `config/importmap.rb`, add:
502
+
503
+ ```ruby
504
+ pin "chartkick", to: "chartkick.js"
505
+ ```
506
+
507
+ And in `app/javascript/application.js`, add:
508
+
509
+ ```js
510
+ import "chartkick"
511
+ ```
512
+
453
513
  For Rails 6 / Webpacker, run:
454
514
 
455
515
  ```sh
@@ -459,7 +519,7 @@ yarn add chartkick
459
519
  And in `app/javascript/packs/application.js`, add:
460
520
 
461
521
  ```js
462
- require("chartkick")
522
+ import "chartkick"
463
523
  ```
464
524
 
465
525
  For Rails 5 / Sprockets, in `app/assets/javascripts/application.js`, add:
@@ -478,6 +538,27 @@ before your charts.
478
538
 
479
539
  ### Highcharts
480
540
 
541
+ For Rails 7 / Importmap, run:
542
+
543
+ ```sh
544
+ bin/importmap pin highcharts --download
545
+ ```
546
+
547
+ And in `config/importmap.rb`, add:
548
+
549
+ ```ruby
550
+ pin "chartkick", to: "chartkick.js"
551
+ ```
552
+
553
+ And in `app/javascript/application.js`, add:
554
+
555
+ ```js
556
+ import "chartkick"
557
+ import Highcharts from "highcharts"
558
+
559
+ window.Highcharts = Highcharts
560
+ ```
561
+
481
562
  For Rails 6 / Webpacker, run:
482
563
 
483
564
  ```sh
@@ -487,7 +568,7 @@ yarn add chartkick highcharts
487
568
  And in `app/javascript/packs/application.js`, add:
488
569
 
489
570
  ```js
490
- require("chartkick").use(require("highcharts"))
571
+ import "chartkick/highcharts"
491
572
  ```
492
573
 
493
574
  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 +586,27 @@ Download [chartkick.js](https://raw.githubusercontent.com/ankane/chartkick/maste
505
586
  <script src="chartkick.js"></script>
506
587
  ```
507
588
 
589
+ Then include the charting library.
590
+
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)
592
+
593
+ ```html
594
+ <script src="chart.js"></script>
595
+ <script src="chartjs-adapter-date-fns.bundle.js"></script>
596
+ ```
597
+
598
+ Google Charts
599
+
600
+ ```html
601
+ <script src="https://www.gstatic.com/charts/loader.js"></script>
602
+ ```
603
+
604
+ Highcharts - download [highcharts.js](https://code.highcharts.com/highcharts.js)
605
+
606
+ ```html
607
+ <script src="highcharts.js"></script>
608
+ ```
609
+
508
610
  ### Multiple Libraries
509
611
 
510
612
  If more than one charting library is loaded, choose between them with:
@@ -593,19 +695,21 @@ Check out [chartkick.js](https://github.com/ankane/chartkick.js)
593
695
 
594
696
  ## Upgrading
595
697
 
596
- ### 3.0
698
+ ### 5.0
597
699
 
598
- Breaking changes
700
+ If you use Importmaps or Sprockets, update the gem and you’re good to go!
599
701
 
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
702
+ If you use esbuild, Webpack, or Webpacker, run:
605
703
 
606
- ## Credits
704
+ ```sh
705
+ yarn upgrade chartkick --latest
706
+ ```
607
707
 
608
- Chartkick uses [iso8601.js](https://github.com/Do/iso8601.js) to parse dates and times.
708
+ If you use Chart.js with esbuild, Webpack, or Webpacker, also run:
709
+
710
+ ```sh
711
+ yarn upgrade chart.js --latest
712
+ ```
609
713
 
610
714
  ## History
611
715
 
@@ -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,5 +1,13 @@
1
1
  module Chartkick
2
2
  class Engine < ::Rails::Engine
3
3
  # for assets
4
+
5
+ # for importmap
6
+ initializer "chartkick.importmap" do |app|
7
+ if defined?(Importmap)
8
+ app.config.assets.precompile << "chartkick.js"
9
+ app.config.assets.precompile << "Chart.bundle.js"
10
+ end
11
+ end
4
12
  end
5
13
  end