chartkick 2.3.5 → 3.3.0

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.

Potentially problematic release.


This version of chartkick might be problematic. Click here for more details.

checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d37a995b1d870a7e93be5d5eb338d163e1b42653304baab7bad889aff6f655fe
4
- data.tar.gz: 9c5495a74d4e6c03b199a263556d9dcee91ee4e0c87fb7694851971068a817e2
3
+ metadata.gz: c0a077be384be822d5ee216171c64d1961de9171b979bd36028c65c4eff5d467
4
+ data.tar.gz: b6015bf55a19f58775c94043fcff1217c30618140012d5402ccebf47a9e5c824
5
5
  SHA512:
6
- metadata.gz: e2e350cf8f342a7d55bfbc0a6581c92e25b4a59442719ef12aa32400ba9610b137360a10535a96afa049025a739235f28b00ade4299ad1cad5d2f9525ea1517d
7
- data.tar.gz: 3de86761cb1f9e63d71589640827cc8391c668b398f9e49cdf4f4029351850181cc4e8197d8c6c341b6e7d79147ee01d96be6cf72b55ccca1f1395f4504d951e
6
+ metadata.gz: 0cade10faad8ea673f9201f63530c178a1b10ffacc67f8c9a9a8fd6f3d56e8911c1df67ad07fa304ca1202dc8509763e56ccdf596ba918ba1c3a1581eea36ff6
7
+ data.tar.gz: d59492c1e444735672121c686a1c355d92067813e8c80d79c2f93737174d3766d7d1d1a272a0c1afb7b7afd3ab7c9dd71695b8a9b278c7be8528f326a8c7ef87
data/CHANGELOG.md CHANGED
@@ -1,3 +1,50 @@
1
+ ## 3.3.0
2
+
3
+ - Updated Chartkick.js to 3.2.0
4
+ - Rolled back Chart.js to 2.8.0 due to legend change
5
+
6
+ ## 3.2.2
7
+
8
+ - Updated Chartkick.js to 3.1.3
9
+ - Updated Chart.js to 2.9.1
10
+
11
+ ## 3.2.1
12
+
13
+ - Updated Chartkick.js to 3.1.1
14
+
15
+ ## 3.2.0
16
+
17
+ - Fixed XSS vulnerability - see [#488](https://github.com/ankane/chartkick/issues/488)
18
+
19
+ ## 3.1.0
20
+
21
+ - Updated Chartkick.js to 3.1.0
22
+ - Updated Chart.js to 2.8.0
23
+
24
+ ## 3.0.2
25
+
26
+ - Fixed error with `nonce` option with Secure Headers and Rails < 5.2
27
+ - Updated Chartkick.js to 3.0.2
28
+ - Updated Chart.js to 2.7.3
29
+
30
+ ## 3.0.1
31
+
32
+ - Updated Chartkick.js to 3.0.1
33
+
34
+ ## 3.0.0
35
+
36
+ - Updated Chartkick.js to 3.0.0
37
+ - Added `code` option
38
+ - Added support for `nonce: true`
39
+
40
+ Breaking changes
41
+
42
+ - Removed support for Rails < 4.2
43
+ - Removed chartkick.js from asset precompile (no longer needed)
44
+ - Removed `xtype` option - numeric axes are automatically detected
45
+ - Removed `window.Chartkick = {...}` way to set config - use `Chartkick.configure` instead
46
+ - Removed support for the Google Charts jsapi loader - use loader.js instead
47
+
1
48
  ## 2.3.5
2
49
 
3
50
  - Updated Chartkick.js to 2.3.6
data/CONTRIBUTING.md CHANGED
@@ -2,17 +2,15 @@
2
2
 
3
3
  First, thanks for wanting to contribute. You’re awesome! :heart:
4
4
 
5
- ## Questions
5
+ ## Help
6
6
 
7
- Use [Stack Overflow](https://stackoverflow.com/) with the tag `chartkick`.
7
+ We’re not able to provide support through GitHub Issues. If you’re looking for help with your code, try posting on [Stack Overflow](https://stackoverflow.com/).
8
8
 
9
- ## Feature Requests
9
+ All features should be documented. If you don’t see a feature in the docs, assume it doesn’t exist.
10
10
 
11
- Create an issue. Start the title with `[Idea]`.
11
+ ## Bugs
12
12
 
13
- ## Issues
14
-
15
- Think you’ve discovered an issue?
13
+ Think you’ve discovered a bug?
16
14
 
17
15
  1. Search existing issues to see if it’s been reported.
18
16
  2. Try the `master` branch to make sure it hasn’t been fixed.
@@ -27,10 +25,15 @@ If the above steps don’t help, create an issue. Include:
27
25
  - JavaScript rendered by Chartkick
28
26
  - Complete backtraces for exceptions
29
27
 
28
+ ## New Features
29
+
30
+ If you’d like to discuss a new feature, create an issue and start the title with `[Idea]`.
31
+
30
32
  ## Pull Requests
31
33
 
32
34
  Fork the project and create a pull request. A few tips:
33
35
 
36
+ - Submit JavaScript changes to the [Chartkick.js](https://github.com/ankane/chartkick.js) repo.
34
37
  - Keep changes to a minimum. If you have multiple features or fixes, submit multiple pull requests.
35
38
  - Follow the existing style. The code should read like it’s written by a single person.
36
39
 
data/LICENSE.txt CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2013-2018 Andrew Kane
1
+ Copyright (c) 2013-2019 Andrew Kane
2
2
 
3
3
  MIT License
4
4
 
data/README.md CHANGED
@@ -2,12 +2,42 @@
2
2
 
3
3
  Create beautiful JavaScript charts with one line of Ruby. No more fighting with charting libraries!
4
4
 
5
- [See it in action](https://www.chartkick.com)
5
+ [See it in action](https://chartkick.com)
6
6
 
7
7
  :fire: For admin charts and dashboards, check out [Blazer](https://github.com/ankane/blazer/)
8
8
 
9
9
  :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
10
 
11
+ ## Quick Start
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem "chartkick"
17
+ ```
18
+
19
+ For Rails 6 / Webpacker, run:
20
+
21
+ ```sh
22
+ yarn add chartkick chart.js
23
+ ```
24
+
25
+ And in `app/javascript/packs/application.js`, add:
26
+
27
+ ```js
28
+ require("chartkick")
29
+ require("chart.js")
30
+ ```
31
+
32
+ For Rails 5 / Sprockets, in `app/assets/javascripts/application.js`, add:
33
+
34
+ ```js
35
+ //= require chartkick
36
+ //= require Chart.bundle
37
+ ```
38
+
39
+ This sets up Chartkick with Chart.js. For other charting libraries, see [detailed instructions](#installation).
40
+
11
41
  ## Charts
12
42
 
13
43
  Line chart
@@ -116,6 +146,12 @@ Min and max values
116
146
 
117
147
  `min` defaults to 0 for charts with non-negative values. Use `nil` to let the charting library decide.
118
148
 
149
+ Min and max for x-axis - *Chart.js*
150
+
151
+ ```erb
152
+ <%= line_chart data, xmin: "2018-01-01", xmax: "2019-01-01" %>
153
+ ```
154
+
119
155
  Colors
120
156
 
121
157
  ```erb
@@ -206,6 +242,30 @@ Set a decimal separator - *Chart.js, Highcharts*
206
242
  <%= line_chart data, decimal: "," %>
207
243
  ```
208
244
 
245
+ Set significant digits - *Chart.js, Highcharts*
246
+
247
+ ```erb
248
+ <%= line_chart data, precision: 3 %>
249
+ ```
250
+
251
+ Set rounding - *Chart.js, Highcharts*
252
+
253
+ ```erb
254
+ <%= line_chart data, round: 2 %>
255
+ ```
256
+
257
+ Show insignificant zeros, useful for currency - *Chart.js, Highcharts*
258
+
259
+ ```erb
260
+ <%= line_chart data, round: 2, zeros: true %>
261
+ ```
262
+
263
+ Friendly file sizes - *Chart.js 2.8+*
264
+
265
+ ```erb
266
+ <%= line_chart data, bytes: true %>
267
+ ```
268
+
209
269
  Show a message when data is empty
210
270
 
211
271
  ```erb
@@ -257,18 +317,19 @@ You capture the JavaScript in a content block with:
257
317
  Chartkick.options[:content_for] = :charts_js
258
318
  ```
259
319
 
260
- Then, in your layout:
320
+ Then, in your layout, use:
261
321
 
262
322
  ```erb
263
- <%= yield :charts_js %> <!-- Rails -->
264
- <%= yield_content :charts_js %> <!-- Padrino -->
323
+ <%= yield :charts_js %>
265
324
  ```
266
325
 
326
+ > For Padrino, use `yield_content` instead of `yield`
327
+
267
328
  This is great for including all of your JavaScript at the bottom of the page.
268
329
 
269
330
  ### Data
270
331
 
271
- Pass data as a Hash or Array
332
+ Pass data as a hash or array
272
333
 
273
334
  ```erb
274
335
  <%= pie_chart({"Football" => 10, "Basketball" => 5}) %>
@@ -284,12 +345,35 @@ For multiple series, use the format
284
345
  ] %>
285
346
  ```
286
347
 
287
- Times can be a time, a timestamp, or a string (strings are parsed)
348
+ Times can be a time or a string (strings are parsed)
349
+
350
+ ```erb
351
+ <%= line_chart({20.day.ago => 5, "2013-05-07 00:00:00 UTC" => 7}) %>
352
+ ```
353
+
354
+ ### Multiple Series
355
+
356
+ You can pass a few options with a series:
357
+
358
+ - `name`
359
+ - `data`
360
+ - `color`
361
+ - `dataset` - *Chart.js only*
362
+ - `points` - *Chart.js only*
363
+ - `curve` - *Chart.js only*
364
+
365
+ ### Code
366
+
367
+ If you want to use the charting library directly, get the code with:
288
368
 
289
369
  ```erb
290
- <%= line_chart({20.day.ago => 5, 1368174456 => 4, "2013-05-07 00:00:00 UTC" => 7}) %>
370
+ <%= line_chart data, code: true %>
291
371
  ```
292
372
 
373
+ The code will be logged to the JavaScript console.
374
+
375
+ > JavaScript functions cannot be logged, so it may not be identical.
376
+
293
377
  ### Download Charts
294
378
 
295
379
  *Chart.js only*
@@ -300,13 +384,25 @@ Give users the ability to download charts. It all happens in the browser - no se
300
384
  <%= line_chart data, download: true %>
301
385
  ```
302
386
 
387
+ > Safari will open the image in a new window instead of downloading.
388
+
303
389
  Set the filename
304
390
 
305
391
  ```erb
306
- <%= line_chart data, download: "boom" %>
392
+ <%= line_chart data, download: {filename: "boom"} %>
307
393
  ```
308
394
 
309
- **Note:** Safari will open the image in a new window instead of downloading.
395
+ Set the background color
396
+
397
+ ```erb
398
+ <%= line_chart data, download: {background: "#ffffff"} %>
399
+ ```
400
+
401
+ Set title
402
+
403
+ ```erb
404
+ <%= line_chart data, title: "Awesome chart" %>
405
+ ```
310
406
 
311
407
  ## Installation
312
408
 
@@ -318,92 +414,97 @@ gem "chartkick"
318
414
 
319
415
  Next, choose your charting library.
320
416
 
321
- ### Charting Libraries
417
+ - [Chart.js](#chart-js)
418
+ - [Google Charts](#google-charts)
419
+ - [Highcharts](#highcharts)
322
420
 
323
- **Note:** In the instructions below, `application.js` must be included **before** the charts in your views, unless using the `:content_for` option.
421
+ > In the instructions below, `application.js` must be included **before** the charts in your views, unless using the `:content_for` option.
324
422
 
325
- #### Chart.js
423
+ ### Chart.js
326
424
 
327
- In `application.js`, add:
425
+ For Rails 6 / Webpacker, run:
328
426
 
329
- ```js
330
- //= require Chart.bundle
331
- //= require chartkick
427
+ ```sh
428
+ yarn add chartkick chart.js
332
429
  ```
333
430
 
334
- #### Google Charts
431
+ And in `app/javascript/packs/application.js`, add:
432
+
433
+ ```js
434
+ require("chartkick")
435
+ require("chart.js")
436
+ ```
335
437
 
336
- In `application.js`, add:
438
+ For Rails 5 / Sprockets, in `app/assets/javascripts/application.js`, add:
337
439
 
338
440
  ```js
339
441
  //= require chartkick
442
+ //= require Chart.bundle
340
443
  ```
341
444
 
342
- In your views, before `application.js`, add:
445
+ ### Google Charts
446
+
447
+ In your layout or views, add:
343
448
 
344
449
  ```erb
345
450
  <%= javascript_include_tag "https://www.gstatic.com/charts/loader.js" %>
346
451
  ```
347
452
 
348
- #### Highcharts
453
+ For Rails 6 / Webpacker, run:
349
454
 
350
- Download [highcharts.js](https://code.highcharts.com/highcharts.js) into `vendor/assets/javascripts` (or use `yarn add highcharts` in Rails 5.1+).
455
+ ```sh
456
+ yarn add chartkick
457
+ ```
351
458
 
352
- In `application.js`, add:
459
+ And in `app/javascript/packs/application.js`, add:
353
460
 
354
461
  ```js
355
- //= require highcharts
356
- //= require chartkick
462
+ require("chartkick")
357
463
  ```
358
464
 
359
- Works with Highcharts 2.1+
465
+ For Rails 5 / Sprockets, in `app/assets/javascripts/application.js`, add:
360
466
 
361
- ### Webpacker
467
+ ```js
468
+ //= require chartkick
469
+ ```
362
470
 
363
- For Webpacker, use Yarn to install the JavaScript libraries:
471
+ To specify a language or Google Maps API key, use:
364
472
 
365
- ```sh
366
- yarn add chartkick chart.js # or highcharts
473
+ ```js
474
+ Chartkick.configure({language: "de", mapsApiKey: "..."})
367
475
  ```
368
476
 
369
- Then include them in your pack.
477
+ before your charts.
370
478
 
371
- ```es6
372
- import Chartkick from "chartkick";
373
- window.Chartkick = Chartkick;
479
+ ### Highcharts
374
480
 
375
- // for Chart.js
376
- import Chart from "chart.js";
377
- Chartkick.addAdapter(Chart);
481
+ For Rails 6 / Webpacker, run:
378
482
 
379
- // for Highcharts
380
- import Highcharts from "highcharts";
381
- Chartkick.addAdapter(Highcharts);
382
-
383
- // for Google Charts
384
- // just include https://www.gstatic.com/charts/loader.js in your views
483
+ ```sh
484
+ yarn add chartkick highcharts
385
485
  ```
386
486
 
387
- You pack must be included **before** the charts in your views, unless using the `:content_for` option.
487
+ And in `app/javascript/packs/application.js`, add:
388
488
 
389
- ### Sinatra and Padrino
489
+ ```js
490
+ require("chartkick").use(require("highcharts"))
491
+ ```
390
492
 
391
- You must include `chartkick.js` manually. [Download it here](https://raw.githubusercontent.com/ankane/chartkick/master/vendor/assets/javascripts/chartkick.js)
493
+ 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:
392
494
 
393
- ```html
394
- <script src="chartkick.js"></script>
495
+ ```js
496
+ //= require chartkick
497
+ //= require highcharts
395
498
  ```
396
499
 
397
- ### Localization
500
+ ### Sinatra and Padrino
398
501
 
399
- To specify a language for Google Charts, add:
502
+ Download [chartkick.js](https://raw.githubusercontent.com/ankane/chartkick/master/vendor/assets/javascripts/chartkick.js) and include it manually.
400
503
 
401
- ```javascript
402
- Chartkick.configure({language: "de"});
504
+ ```html
505
+ <script src="chartkick.js"></script>
403
506
  ```
404
507
 
405
- after the JavaScript files and before your charts.
406
-
407
508
  ### Multiple Libraries
408
509
 
409
510
  If more than one charting library is loaded, choose between them with:
@@ -469,6 +570,10 @@ Chartkick.eachChart( function(chart) {
469
570
  })
470
571
  ```
471
572
 
573
+ ## Content Security Policy (CSP)
574
+
575
+ Check out [how to configure CSP](https://github.com/ankane/chartkick/blob/master/guides/Content-Security-Policy.md)
576
+
472
577
  ## No Ruby? No Problem
473
578
 
474
579
  Check out [chartkick.js](https://github.com/ankane/chartkick.js)
@@ -476,11 +581,22 @@ Check out [chartkick.js](https://github.com/ankane/chartkick.js)
476
581
  ## Tutorials
477
582
 
478
583
  - [Charts with Chartkick and Groupdate](https://gorails.com/episodes/charts-with-chartkick-and-groupdate)
584
+ - [Creando gráficos en Ruby on Rails con Chartkick y Chart.js](https://www.youtube.com/watch?v=W92AlkwQn3M)
479
585
  - [Make Easy Graphs and Charts on Rails with Chartkick](https://www.sitepoint.com/make-easy-graphs-and-charts-on-rails-with-chartkick/)
480
586
  - [Practical Graphs on Rails: Chartkick in Practice](https://www.sitepoint.com/graphs-on-rails-chartkick-in-practice/)
481
587
 
482
588
  ## Upgrading
483
589
 
590
+ ### 3.0
591
+
592
+ Breaking changes
593
+
594
+ - Removed support for Rails < 4.2
595
+ - Removed chartkick.js from asset precompile (no longer needed)
596
+ - Removed `xtype` option - numeric axes are automatically detected
597
+ - Removed `window.Chartkick = {...}` way to set config - use `Chartkick.configure` instead
598
+ - Removed support for the Google Charts jsapi loader - use loader.js instead
599
+
484
600
  ### 2.0
485
601
 
486
602
  Breaking changes
@@ -1,20 +1,5 @@
1
1
  module Chartkick
2
2
  class Engine < ::Rails::Engine
3
- initializer "precompile", group: :all do |app|
4
- if app.config.respond_to?(:assets)
5
- if defined?(Sprockets) && Gem::Version.new(Sprockets::VERSION) >= Gem::Version.new("4.0.0.beta1")
6
- app.config.assets.precompile << "chartkick.js"
7
- else
8
- # use a proc instead of a string
9
- app.config.assets.precompile << proc { |path| path == "chartkick.js" }
10
- end
11
- end
12
- end
13
-
14
- initializer "helper" do
15
- ActiveSupport.on_load(:action_view) do
16
- include Helper
17
- end
18
- end
3
+ # for assets
19
4
  end
20
5
  end
@@ -3,41 +3,41 @@ require "erb"
3
3
 
4
4
  module Chartkick
5
5
  module Helper
6
- def line_chart(data_source, options = {})
6
+ def line_chart(data_source, **options)
7
7
  chartkick_chart "LineChart", data_source, options
8
8
  end
9
9
 
10
- def pie_chart(data_source, options = {})
10
+ def pie_chart(data_source, **options)
11
11
  chartkick_chart "PieChart", data_source, options
12
12
  end
13
13
 
14
- def column_chart(data_source, options = {})
14
+ def column_chart(data_source, **options)
15
15
  chartkick_chart "ColumnChart", data_source, options
16
16
  end
17
17
 
18
- def bar_chart(data_source, options = {})
18
+ def bar_chart(data_source, **options)
19
19
  chartkick_chart "BarChart", data_source, options
20
20
  end
21
21
 
22
- def area_chart(data_source, options = {})
22
+ def area_chart(data_source, **options)
23
23
  chartkick_chart "AreaChart", data_source, options
24
24
  end
25
25
 
26
- def scatter_chart(data_source, options = {})
26
+ def scatter_chart(data_source, **options)
27
27
  chartkick_chart "ScatterChart", data_source, options
28
28
  end
29
29
 
30
- def geo_chart(data_source, options = {})
30
+ def geo_chart(data_source, **options)
31
31
  chartkick_chart "GeoChart", data_source, options
32
32
  end
33
33
 
34
- def timeline(data_source, options = {})
34
+ def timeline(data_source, **options)
35
35
  chartkick_chart "Timeline", data_source, options
36
36
  end
37
37
 
38
38
  private
39
39
 
40
- def chartkick_chart(klass, data_source, options)
40
+ def chartkick_chart(klass, data_source, **options)
41
41
  @chartkick_chart_id ||= 0
42
42
  options = chartkick_deep_merge(Chartkick.options, options)
43
43
  element_id = options.delete(:id) || "chart-#{@chartkick_chart_id += 1}"
@@ -46,13 +46,47 @@ module Chartkick
46
46
  defer = !!options.delete(:defer)
47
47
  # content_for: nil must override default
48
48
  content_for = options.key?(:content_for) ? options.delete(:content_for) : Chartkick.content_for
49
- nonce = options.key?(:nonce) ? " nonce=\"#{ERB::Util.html_escape(options.delete(:nonce))}\"" : nil
50
- 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>)) % {id: ERB::Util.html_escape(element_id), height: ERB::Util.html_escape(height), width: ERB::Util.html_escape(width)}
51
49
 
52
- createjs = "new Chartkick.#{klass}(#{element_id.to_json}, #{data_source.respond_to?(:chart_json) ? data_source.chart_json : data_source.to_json}, #{options.to_json});"
50
+ nonce = options.delete(:nonce)
51
+ if nonce == true
52
+ # Secure Headers also defines content_security_policy_nonce but it takes an argument
53
+ # Rails 5.2 overrides this method, but earlier versions do not
54
+ if respond_to?(:content_security_policy_nonce) && (content_security_policy_nonce rescue nil)
55
+ # Rails 5.2
56
+ nonce = content_security_policy_nonce
57
+ elsif respond_to?(:content_security_policy_script_nonce)
58
+ # Secure Headers
59
+ nonce = content_security_policy_script_nonce
60
+ end
61
+ end
62
+ nonce_html = nonce ? " nonce=\"#{ERB::Util.html_escape(nonce)}\"" : nil
63
+
64
+ # html vars
65
+ html_vars = {
66
+ id: element_id,
67
+ height: height,
68
+ width: width
69
+ }
70
+ html_vars.each_key do |k|
71
+ html_vars[k] = ERB::Util.html_escape(html_vars[k])
72
+ end
73
+ 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
74
+
75
+ # js vars
76
+ js_vars = {
77
+ type: klass.to_json,
78
+ id: element_id.to_json,
79
+ data: data_source.respond_to?(:chart_json) ? data_source.chart_json : data_source.to_json,
80
+ options: options.to_json
81
+ }
82
+ js_vars.each_key do |k|
83
+ js_vars[k] = chartkick_json_escape(js_vars[k])
84
+ end
85
+ createjs = "new Chartkick[%{type}](%{id}, %{data}, %{options});" % js_vars
86
+
53
87
  if defer
54
88
  js = <<JS
55
- <script type="text/javascript"#{nonce}>
89
+ <script type="text/javascript"#{nonce_html}>
56
90
  (function() {
57
91
  var createChart = function() { #{createjs} };
58
92
  if (window.addEventListener) {
@@ -67,7 +101,7 @@ module Chartkick
67
101
  JS
68
102
  else
69
103
  js = <<JS
70
- <script type="text/javascript"#{nonce}>
104
+ <script type="text/javascript"#{nonce_html}>
71
105
  #{createjs}
72
106
  </script>
73
107
  JS
@@ -91,5 +125,16 @@ JS
91
125
  end
92
126
  hash_a
93
127
  end
128
+
129
+ # from https://github.com/rails/rails/blob/master/activesupport/lib/active_support/core_ext/string/output_safety.rb
130
+ JSON_ESCAPE = { "&" => '\u0026', ">" => '\u003e', "<" => '\u003c', "\u2028" => '\u2028', "\u2029" => '\u2029' }
131
+ JSON_ESCAPE_REGEXP = /[\u2028\u2029&><]/u
132
+ def chartkick_json_escape(s)
133
+ if ERB::Util.respond_to?(:json_escape)
134
+ ERB::Util.json_escape(s)
135
+ else
136
+ s.to_s.gsub(JSON_ESCAPE_REGEXP, JSON_ESCAPE)
137
+ end
138
+ end
94
139
  end
95
140
  end
@@ -1,3 +1,3 @@
1
1
  module Chartkick
2
- VERSION = "2.3.5"
2
+ VERSION = "3.3.0"
3
3
  end
data/lib/chartkick.rb CHANGED
@@ -1,8 +1,16 @@
1
- require "chartkick/version"
2
1
  require "chartkick/helper"
3
- require "chartkick/rails" if defined?(Rails)
2
+ require "chartkick/version"
3
+
4
+ # integrations
5
+ require "chartkick/engine" if defined?(Rails)
4
6
  require "chartkick/sinatra" if defined?(Sinatra)
5
7
 
8
+ if defined?(ActiveSupport.on_load)
9
+ ActiveSupport.on_load(:action_view) do
10
+ include Chartkick::Helper
11
+ end
12
+ end
13
+
6
14
  module Chartkick
7
15
  class << self
8
16
  attr_accessor :content_for
@@ -15,9 +23,21 @@ end
15
23
  # use Enumerable so it can be called on arrays
16
24
  module Enumerable
17
25
  def chart_json
18
- if is_a?(Hash) && (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] }}
26
+ if is_a?(Hash)
27
+ if (key = keys.first) && key.is_a?(Array) && key.size == 2
28
+ group_by { |k, _v| k[0] }.map do |name, data|
29
+ {name: name, data: data.map { |k, v| [k[1], v] }}
30
+ end
31
+ else
32
+ to_a
33
+ end
34
+ elsif is_a?(Array)
35
+ map do |v|
36
+ if v.is_a?(Hash) && v[:data].is_a?(Hash)
37
+ v = v.dup
38
+ v[:data] = v[:data].to_a
39
+ end
40
+ v
21
41
  end
22
42
  else
23
43
  self