| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- 'use strict';
- import Chart from 'chart.js';
- var helpers = Chart.helpers;
- var utils = {
- // @todo move this in Chart.helpers.toTextLines
- toTextLines: function(inputs) {
- var lines = [];
- var input;
- inputs = [].concat(inputs);
- while (inputs.length) {
- input = inputs.pop();
- if (typeof input === 'string') {
- lines.unshift.apply(lines, input.split('\n'));
- } else if (Array.isArray(input)) {
- inputs.push.apply(inputs, input);
- } else if (!helpers.isNullOrUndef(inputs)) {
- lines.unshift('' + input);
- }
- }
- return lines;
- },
- // @todo move this method in Chart.helpers.canvas.toFont (deprecates helpers.fontString)
- // @see https://developer.mozilla.org/en-US/docs/Web/CSS/font
- toFontString: function(font) {
- if (!font || helpers.isNullOrUndef(font.size) || helpers.isNullOrUndef(font.family)) {
- return null;
- }
- return (font.style ? font.style + ' ' : '')
- + (font.weight ? font.weight + ' ' : '')
- + font.size + 'px '
- + font.family;
- },
- // @todo move this in Chart.helpers.canvas.textSize
- // @todo cache calls of measureText if font doesn't change?!
- textSize: function(ctx, lines, font) {
- var items = [].concat(lines);
- var ilen = items.length;
- var prev = ctx.font;
- var width = 0;
- var i;
- ctx.font = font.string;
- for (i = 0; i < ilen; ++i) {
- width = Math.max(ctx.measureText(items[i]).width, width);
- }
- ctx.font = prev;
- return {
- height: ilen * font.lineHeight,
- width: width
- };
- },
- // @todo move this method in Chart.helpers.options.toFont
- parseFont: function(value) {
- var global = Chart.defaults.global;
- var size = helpers.valueOrDefault(value.size, global.defaultFontSize);
- var font = {
- family: helpers.valueOrDefault(value.family, global.defaultFontFamily),
- lineHeight: helpers.options.toLineHeight(value.lineHeight, size),
- size: size,
- style: helpers.valueOrDefault(value.style, global.defaultFontStyle),
- weight: helpers.valueOrDefault(value.weight, null),
- string: ''
- };
- font.string = utils.toFontString(font);
- return font;
- },
- /**
- * Returns value bounded by min and max. This is equivalent to max(min, min(value, max)).
- * @todo move this method in Chart.helpers.bound
- * https://doc.qt.io/qt-5/qtglobal.html#qBound
- */
- bound: function(min, value, max) {
- return Math.max(min, Math.min(value, max));
- },
- /**
- * Returns an array of pair [value, state] where state is:
- * * -1: value is only in a0 (removed)
- * * 1: value is only in a1 (added)
- */
- arrayDiff: function(a0, a1) {
- var prev = a0.slice();
- var updates = [];
- var i, j, ilen, v;
- for (i = 0, ilen = a1.length; i < ilen; ++i) {
- v = a1[i];
- j = prev.indexOf(v);
- if (j === -1) {
- updates.push([v, 1]);
- } else {
- prev.splice(j, 1);
- }
- }
- for (i = 0, ilen = prev.length; i < ilen; ++i) {
- updates.push([prev[i], -1]);
- }
- return updates;
- }
- };
- export default utils;
|