c94c42d8999f29c309f36db80925a263a17f9130fe3ce441e1f96a8e9d8b7e90a77e70f9f42c79d64fdf85b8af5dca2288e19c73ec51c68e81faf559eb8479 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. 'use strict';
  2. import Chart from 'chart.js';
  3. var helpers = Chart.helpers;
  4. var utils = {
  5. // @todo move this in Chart.helpers.toTextLines
  6. toTextLines: function(inputs) {
  7. var lines = [];
  8. var input;
  9. inputs = [].concat(inputs);
  10. while (inputs.length) {
  11. input = inputs.pop();
  12. if (typeof input === 'string') {
  13. lines.unshift.apply(lines, input.split('\n'));
  14. } else if (Array.isArray(input)) {
  15. inputs.push.apply(inputs, input);
  16. } else if (!helpers.isNullOrUndef(inputs)) {
  17. lines.unshift('' + input);
  18. }
  19. }
  20. return lines;
  21. },
  22. // @todo move this method in Chart.helpers.canvas.toFont (deprecates helpers.fontString)
  23. // @see https://developer.mozilla.org/en-US/docs/Web/CSS/font
  24. toFontString: function(font) {
  25. if (!font || helpers.isNullOrUndef(font.size) || helpers.isNullOrUndef(font.family)) {
  26. return null;
  27. }
  28. return (font.style ? font.style + ' ' : '')
  29. + (font.weight ? font.weight + ' ' : '')
  30. + font.size + 'px '
  31. + font.family;
  32. },
  33. // @todo move this in Chart.helpers.canvas.textSize
  34. // @todo cache calls of measureText if font doesn't change?!
  35. textSize: function(ctx, lines, font) {
  36. var items = [].concat(lines);
  37. var ilen = items.length;
  38. var prev = ctx.font;
  39. var width = 0;
  40. var i;
  41. ctx.font = font.string;
  42. for (i = 0; i < ilen; ++i) {
  43. width = Math.max(ctx.measureText(items[i]).width, width);
  44. }
  45. ctx.font = prev;
  46. return {
  47. height: ilen * font.lineHeight,
  48. width: width
  49. };
  50. },
  51. // @todo move this method in Chart.helpers.options.toFont
  52. parseFont: function(value) {
  53. var global = Chart.defaults.global;
  54. var size = helpers.valueOrDefault(value.size, global.defaultFontSize);
  55. var font = {
  56. family: helpers.valueOrDefault(value.family, global.defaultFontFamily),
  57. lineHeight: helpers.options.toLineHeight(value.lineHeight, size),
  58. size: size,
  59. style: helpers.valueOrDefault(value.style, global.defaultFontStyle),
  60. weight: helpers.valueOrDefault(value.weight, null),
  61. string: ''
  62. };
  63. font.string = utils.toFontString(font);
  64. return font;
  65. },
  66. /**
  67. * Returns value bounded by min and max. This is equivalent to max(min, min(value, max)).
  68. * @todo move this method in Chart.helpers.bound
  69. * https://doc.qt.io/qt-5/qtglobal.html#qBound
  70. */
  71. bound: function(min, value, max) {
  72. return Math.max(min, Math.min(value, max));
  73. },
  74. /**
  75. * Returns an array of pair [value, state] where state is:
  76. * * -1: value is only in a0 (removed)
  77. * * 1: value is only in a1 (added)
  78. */
  79. arrayDiff: function(a0, a1) {
  80. var prev = a0.slice();
  81. var updates = [];
  82. var i, j, ilen, v;
  83. for (i = 0, ilen = a1.length; i < ilen; ++i) {
  84. v = a1[i];
  85. j = prev.indexOf(v);
  86. if (j === -1) {
  87. updates.push([v, 1]);
  88. } else {
  89. prev.splice(j, 1);
  90. }
  91. }
  92. for (i = 0, ilen = prev.length; i < ilen; ++i) {
  93. updates.push([prev[i], -1]);
  94. }
  95. return updates;
  96. }
  97. };
  98. export default utils;