chartkick 3.2.2 → 3.3.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


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

@@ -2,7 +2,7 @@
2
2
  * Chartkick.js
3
3
  * Create beautiful charts with one line of JavaScript
4
4
  * https://github.com/ankane/chartkick.js
5
- * v3.1.3
5
+ * v3.2.0
6
6
  * MIT License
7
7
  */
8
8
 
@@ -10,7 +10,7 @@
10
10
  typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
11
11
  typeof define === 'function' && define.amd ? define(factory) :
12
12
  (global = global || self, global.Chartkick = factory());
13
- }(this, function () { 'use strict';
13
+ }(this, (function () { 'use strict';
14
14
 
15
15
  function isArray(variable) {
16
16
  return Object.prototype.toString.call(variable) === "[object Array]";
@@ -21,13 +21,17 @@
21
21
  }
22
22
 
23
23
  function isPlainObject(variable) {
24
- return Object.prototype.toString.call(variable) === "[object Object]";
24
+ // protect against prototype pollution, defense 2
25
+ return Object.prototype.toString.call(variable) === "[object Object]" && !isFunction(variable) && variable instanceof Object;
25
26
  }
26
27
 
27
28
  // https://github.com/madrobby/zepto/blob/master/src/zepto.js
28
29
  function extend(target, source) {
29
30
  var key;
30
31
  for (key in source) {
32
+ // protect against prototype pollution, defense 1
33
+ if (key === "__proto__") { continue; }
34
+
31
35
  if (isPlainObject(source[key]) || isArray(source[key])) {
32
36
  if (isPlainObject(source[key]) && !isPlainObject(target[key])) {
33
37
  target[key] = {};
@@ -237,7 +241,7 @@
237
241
  return typeof obj === "number";
238
242
  }
239
243
 
240
- function formatValue(pre, value, options) {
244
+ function formatValue(pre, value, options, axis) {
241
245
  pre = pre || "";
242
246
  if (options.prefix) {
243
247
  if (value < 0) {
@@ -247,6 +251,58 @@
247
251
  pre += options.prefix;
248
252
  }
249
253
 
254
+ var suffix = options.suffix || "";
255
+ var precision = options.precision;
256
+ var round = options.round;
257
+
258
+ if (options.byteScale) {
259
+ var baseValue = axis ? options.byteScale : value;
260
+ if (baseValue >= 1099511627776) {
261
+ value /= 1099511627776;
262
+ suffix = " TB";
263
+ } else if (baseValue >= 1073741824) {
264
+ value /= 1073741824;
265
+ suffix = " GB";
266
+ } else if (baseValue >= 1048576) {
267
+ value /= 1048576;
268
+ suffix = " MB";
269
+ } else if (baseValue >= 1024) {
270
+ value /= 1024;
271
+ suffix = " KB";
272
+ } else {
273
+ suffix = " bytes";
274
+ }
275
+
276
+ if (precision === undefined && round === undefined) {
277
+ precision = 3;
278
+ }
279
+ }
280
+
281
+ if (precision !== undefined && round !== undefined) {
282
+ throw Error("Use either round or precision, not both");
283
+ }
284
+
285
+ if (!axis) {
286
+ if (precision !== undefined) {
287
+ value = value.toPrecision(precision);
288
+ if (!options.zeros) {
289
+ value = parseFloat(value);
290
+ }
291
+ }
292
+
293
+ if (round !== undefined) {
294
+ if (round < 0) {
295
+ var num = Math.pow(10, -1 * round);
296
+ value = parseInt((1.0 * value / num).toFixed(0)) * num;
297
+ } else {
298
+ value = value.toFixed(round);
299
+ if (!options.zeros) {
300
+ value = parseFloat(value);
301
+ }
302
+ }
303
+ }
304
+ }
305
+
250
306
  if (options.thousands || options.decimal) {
251
307
  value = toStr(value);
252
308
  var parts = value.split(".");
@@ -259,7 +315,7 @@
259
315
  }
260
316
  }
261
317
 
262
- return pre + value + (options.suffix || "");
318
+ return pre + value + suffix;
263
319
  }
264
320
 
265
321
  function seriesOption(chart, series, option) {
@@ -420,18 +476,58 @@
420
476
  prefix: chart.options.prefix,
421
477
  suffix: chart.options.suffix,
422
478
  thousands: chart.options.thousands,
423
- decimal: chart.options.decimal
479
+ decimal: chart.options.decimal,
480
+ precision: chart.options.precision,
481
+ round: chart.options.round,
482
+ zeros: chart.options.zeros
424
483
  };
425
484
 
485
+ if (chart.options.bytes) {
486
+ var series = chart.data;
487
+ if (chartType === "pie") {
488
+ series = [{data: series}];
489
+ }
490
+
491
+ // calculate max
492
+ var max = 0;
493
+ for (var i = 0; i < series.length; i++) {
494
+ var s = series[i];
495
+ for (var j = 0; j < s.data.length; j++) {
496
+ if (s.data[j][1] > max) {
497
+ max = s.data[j][1];
498
+ }
499
+ }
500
+ }
501
+
502
+ // calculate scale
503
+ var scale = 1;
504
+ while (max >= 1024) {
505
+ scale *= 1024;
506
+ max /= 1024;
507
+ }
508
+
509
+ // set step size
510
+ formatOptions.byteScale = scale;
511
+ }
512
+
426
513
  if (chartType !== "pie") {
427
514
  var myAxes = options.scales.yAxes;
428
515
  if (chartType === "bar") {
429
516
  myAxes = options.scales.xAxes;
430
517
  }
431
518
 
519
+ if (formatOptions.byteScale) {
520
+ if (!myAxes[0].ticks.stepSize) {
521
+ myAxes[0].ticks.stepSize = formatOptions.byteScale / 2;
522
+ }
523
+ if (!myAxes[0].ticks.maxTicksLimit) {
524
+ myAxes[0].ticks.maxTicksLimit = 4;
525
+ }
526
+ }
527
+
432
528
  if (!myAxes[0].ticks.callback) {
433
529
  myAxes[0].ticks.callback = function (value) {
434
- return formatValue("", value, formatOptions);
530
+ return formatValue("", value, formatOptions, true);
435
531
  };
436
532
  }
437
533
  }
@@ -948,7 +1044,10 @@
948
1044
  prefix: chart.options.prefix,
949
1045
  suffix: chart.options.suffix,
950
1046
  thousands: chart.options.thousands,
951
- decimal: chart.options.decimal
1047
+ decimal: chart.options.decimal,
1048
+ precision: chart.options.precision,
1049
+ round: chart.options.round,
1050
+ zeros: chart.options.zeros
952
1051
  };
953
1052
 
954
1053
  if (chartType !== "pie" && !options.yAxis.labels.formatter) {
@@ -2316,4 +2415,4 @@
2316
2415
 
2317
2416
  return Chartkick;
2318
2417
 
2319
- }));
2418
+ })));
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: chartkick
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.2.2
4
+ version: 3.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Kane
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-10-28 00:00:00.000000000 Z
11
+ date: 2019-11-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler