use direct imports instead of generic helpers import (#7385)

Split all helper functions in different files and import them directly where used
This commit is contained in:
Samuel Gratzl
2020-05-21 22:41:56 +02:00
committed by Evert Timberg
parent f9a34d54bd
commit c796757d3e
11 changed files with 140 additions and 136 deletions

View File

@@ -1,6 +1,6 @@
import helpers from '../helpers/index';
import effects from '../helpers/helpers.easing';
import {resolve} from '../helpers/helpers.options';
import {color as helpersColor} from '../helpers/helpers.color';
const transparent = 'transparent';
const interpolators = {
@@ -8,8 +8,8 @@ const interpolators = {
return factor > 0.5 ? to : from;
},
color(from, to, factor) {
const c0 = helpers.color(from || transparent);
const c1 = c0.valid && helpers.color(to || transparent);
const c0 = helpersColor(from || transparent);
const c1 = c0.valid && helpersColor(to || transparent);
return c1 && c1.valid
? c1.mix(c0, factor).hexString()
: to;

View File

@@ -1,4 +1,4 @@
import helpers from '../helpers/index';
import {requestAnimFrame} from '../helpers/helpers.extras';
/**
* @typedef { import("./core.controller").default } Chart
@@ -54,7 +54,7 @@ export class Animator {
}
me._running = true;
me._request = helpers.requestAnimFrame.call(window, () => {
me._request = requestAnimFrame.call(window, () => {
me._update();
me._request = null;

View File

@@ -1,13 +1,14 @@
import Animator from './core.animator';
import controllers from '../controllers/index';
import defaults from './core.defaults';
import helpers from '../helpers/index';
import Interaction from './core.interaction';
import layouts from './core.layouts';
import {BasicPlatform, DomPlatform} from '../platform';
import plugins from './core.plugins';
import scaleService from './core.scaleService';
import {getMaximumWidth, getMaximumHeight} from '../helpers/helpers.dom';
import {getMaximumWidth, getMaximumHeight, retinaScale} from '../helpers/helpers.dom';
import {mergeIf, merge, _merger, each, callback as callCallback, uid, valueOrDefault, _elementsEqual} from '../helpers/helpers.core';
import {clear as canvasClear, clipArea, unclipArea, _isPointInArea} from '../helpers/helpers.canvas';
// @ts-ignore
import {version} from '../../package.json';
@@ -15,7 +16,6 @@ import {version} from '../../package.json';
* @typedef { import("../platform/platform.base").IEvent } IEvent
*/
const valueOrDefault = helpers.valueOrDefault;
function mergeScaleConfig(config, options) {
options = options || {};
@@ -29,12 +29,12 @@ function mergeScaleConfig(config, options) {
Object.keys(configScales).forEach(id => {
const axis = id[0];
firstIDs[axis] = firstIDs[axis] || id;
scales[id] = helpers.mergeIf({}, [configScales[id], chartDefaults.scales[axis]]);
scales[id] = mergeIf({}, [configScales[id], chartDefaults.scales[axis]]);
});
// Backward compatibility
if (options.scale) {
scales[options.scale.id || 'r'] = helpers.mergeIf({}, [options.scale, chartDefaults.scales.r]);
scales[options.scale.id || 'r'] = mergeIf({}, [options.scale, chartDefaults.scales.r]);
firstIDs.r = firstIDs.r || options.scale.id || 'r';
}
@@ -45,7 +45,7 @@ function mergeScaleConfig(config, options) {
Object.keys(defaultScaleOptions).forEach(defaultID => {
const id = dataset[defaultID + 'AxisID'] || firstIDs[defaultID] || defaultID;
scales[id] = scales[id] || {};
helpers.mergeIf(scales[id], [
mergeIf(scales[id], [
configScales[id],
defaultScaleOptions[defaultID]
]);
@@ -55,7 +55,7 @@ function mergeScaleConfig(config, options) {
// apply scale defaults, if not overridden by dataset defaults
Object.keys(scales).forEach(key => {
const scale = scales[key];
helpers.mergeIf(scale, scaleService.getScaleDefaults(scale.type));
mergeIf(scale, scaleService.getScaleDefaults(scale.type));
});
return scales;
@@ -67,10 +67,10 @@ function mergeScaleConfig(config, options) {
* a deep copy of the result, thus doesn't alter inputs.
*/
function mergeConfig(...args/* config objects ... */) {
return helpers.merge({}, args, {
return merge({}, args, {
merger(key, target, source, options) {
if (key !== 'scales' && key !== 'scale') {
helpers._merger(key, target, source, options);
_merger(key, target, source, options);
}
}
});
@@ -104,7 +104,7 @@ function isAnimationDisabled(config) {
function updateConfig(chart) {
let newOptions = chart.options;
helpers.each(chart.scales, (scale) => {
each(chart.scales, (scale) => {
layouts.removeBox(chart, scale);
});
@@ -139,13 +139,13 @@ function onAnimationsComplete(ctx) {
const animationOptions = chart.options.animation;
plugins.notify(chart, 'afterRender');
helpers.callback(animationOptions && animationOptions.onComplete, [ctx], chart);
callCallback(animationOptions && animationOptions.onComplete, [ctx], chart);
}
function onAnimationProgress(ctx) {
const chart = ctx.chart;
const animationOptions = chart.options.animation;
helpers.callback(animationOptions && animationOptions.onProgress, [ctx], chart);
callCallback(animationOptions && animationOptions.onProgress, [ctx], chart);
}
function isDomSupported() {
@@ -194,7 +194,7 @@ export default class Chart {
const height = canvas && canvas.height;
const width = canvas && canvas.width;
this.id = helpers.uid();
this.id = uid();
this.ctx = context;
this.canvas = canvas;
this.config = config;
@@ -267,7 +267,7 @@ export default class Chart {
// Initial resize before chart draws (must be silent to preserve initial animations).
me.resize(true);
} else {
helpers.dom.retinaScale(me, me.options.devicePixelRatio);
retinaScale(me, me.options.devicePixelRatio);
}
me.bindEvents();
@@ -291,7 +291,7 @@ export default class Chart {
}
clear() {
helpers.canvas.clear(this);
canvasClear(this);
return this;
}
@@ -332,7 +332,7 @@ export default class Chart {
canvas.style.height = newHeight + 'px';
}
helpers.dom.retinaScale(me, newRatio);
retinaScale(me, newRatio);
if (!silent) {
// Notify any plugins about the resize
@@ -354,7 +354,7 @@ export default class Chart {
const scalesOptions = options.scales || {};
const scaleOptions = options.scale;
helpers.each(scalesOptions, (axisOptions, axisID) => {
each(scalesOptions, (axisOptions, axisID) => {
axisOptions.id = axisID;
});
@@ -392,7 +392,7 @@ export default class Chart {
);
}
helpers.each(items, (item) => {
each(items, (item) => {
const scaleOptions = item.options;
const id = scaleOptions.id;
const scaleType = valueOrDefault(scaleOptions.type, item.dtype);
@@ -429,7 +429,7 @@ export default class Chart {
}
});
// clear up discarded scales
helpers.each(updated, (hasUpdated, id) => {
each(updated, (hasUpdated, id) => {
if (!hasUpdated) {
delete scales[id];
}
@@ -520,7 +520,7 @@ export default class Chart {
*/
_resetElements() {
const me = this;
helpers.each(me.data.datasets, (dataset, datasetIndex) => {
each(me.data.datasets, (dataset, datasetIndex) => {
me.getDatasetMeta(datasetIndex).controller.reset();
}, me);
}
@@ -564,7 +564,7 @@ export default class Chart {
// Can only reset the new controllers after the scales have been updated
if (me.options.animation) {
helpers.each(newControllers, (controller) => {
each(newControllers, (controller) => {
controller.reset();
});
}
@@ -601,7 +601,7 @@ export default class Chart {
layouts.update(me, me.width, me.height);
me._layers = [];
helpers.each(me.boxes, (box) => {
each(me.boxes, (box) => {
// configure is called twice, once in core.scale.update and once here.
// Here the boxes are fully updated and at their final positions.
if (box.configure) {
@@ -664,7 +664,7 @@ export default class Chart {
}
const onComplete = function() {
plugins.notify(me, 'afterRender');
helpers.callback(animationOptions && animationOptions.onComplete, [], me);
callCallback(animationOptions && animationOptions.onComplete, [], me);
};
if (Animator.has(me)) {
@@ -775,7 +775,7 @@ export default class Chart {
return;
}
helpers.canvas.clipArea(ctx, {
clipArea(ctx, {
left: clip.left === false ? 0 : area.left - clip.left,
right: clip.right === false ? me.width : area.right + clip.right,
top: clip.top === false ? 0 : area.top - clip.top,
@@ -784,7 +784,7 @@ export default class Chart {
meta.controller.draw();
helpers.canvas.unclipArea(ctx);
unclipArea(ctx);
plugins.notify(me, 'afterDatasetDraw', [args]);
}
@@ -921,7 +921,7 @@ export default class Chart {
if (canvas) {
me.unbindEvents();
helpers.canvas.clear(me);
canvasClear(me);
me.platform.releaseContext(me.ctx);
me.canvas = null;
me.ctx = null;
@@ -959,7 +959,7 @@ export default class Chart {
me._eventHandler(e);
};
helpers.each(me.options.events, (type) => _add(type, listener));
each(me.options.events, (type) => _add(type, listener));
if (me.options.responsive) {
listener = (width, height) => {
@@ -1007,7 +1007,7 @@ export default class Chart {
}
delete me._listeners;
helpers.each(listeners, (listener, type) => {
each(listeners, (listener, type) => {
me.platform.removeEventListener(me, type, listener);
});
}
@@ -1107,16 +1107,16 @@ export default class Chart {
// Invoke onHover hook
// Need to call with native event here to not break backwards compatibility
helpers.callback(options.onHover || options.hover.onHover, [e.native, me.active], me);
callCallback(options.onHover || options.hover.onHover, [e.native, me.active], me);
if (e.type === 'mouseup' || e.type === 'click') {
if (options.onClick && helpers.canvas._isPointInArea(e, me.chartArea)) {
if (options.onClick && _isPointInArea(e, me.chartArea)) {
// Use e.native here for backwards compatibility
options.onClick.call(me, e.native, me.active);
}
}
changed = !helpers._elementsEqual(me.active, me.lastActive);
changed = !_elementsEqual(me.active, me.lastActive);
if (changed || replay) {
me._updateHoverStyles();
}

View File

@@ -1,13 +1,14 @@
import helpers from '../helpers/index';
import Animations from './core.animations';
import {isObject, inherits, merge, _merger, isArray, valueOrDefault, mergeIf, arrayEquals} from '../helpers/helpers.core';
import {resolve} from '../helpers/helpers.options';
import {getHoverColor} from '../helpers/helpers.color';
import {sign} from '../helpers/helpers.math';
/**
* @typedef { import("./core.controller").default } Chart
* @typedef { import("./core.scale").default } Scale
*/
const resolve = helpers.options.resolve;
const arrayEvents = ['push', 'pop', 'shift', 'splice', 'unshift'];
/**
@@ -80,7 +81,7 @@ function defaultClip(xScale, yScale, allowedOverflow) {
function toClip(value) {
let t, r, b, l;
if (helpers.isObject(value)) {
if (isObject(value)) {
t = value.top;
r = value.right;
b = value.bottom;
@@ -148,7 +149,7 @@ function applyStack(stack, value, dsIndex, allOther) {
break;
}
otherValue = stack.values[datasetIndex];
if (!isNaN(otherValue) && (value === 0 || helpers.math.sign(value) === helpers.math.sign(otherValue))) {
if (!isNaN(otherValue) && (value === 0 || sign(value) === sign(otherValue))) {
value += otherValue;
}
}
@@ -217,7 +218,7 @@ function getFirstScaleId(chart, axis) {
export default class DatasetController {
static extend = helpers.inherits;
static extend = inherits;
/**
* @param {Chart} chart
@@ -352,7 +353,7 @@ export default class DatasetController {
// real-time charts), we need to monitor these data modifications and synchronize
// the internal meta data accordingly.
if (helpers.isObject(data)) {
if (isObject(data)) {
// Object data is currently monitored for replacement only
if (me._objectData === data) {
return false;
@@ -360,7 +361,7 @@ export default class DatasetController {
me._data = convertObjectDataToArray(data);
me._objectData = data;
} else {
if (me._data === data && !me._dataModified && helpers.arrayEquals(data, me._dataCopy)) {
if (me._data === data && !me._dataModified && arrayEquals(data, me._dataCopy)) {
return false;
}
@@ -455,13 +456,13 @@ export default class DatasetController {
*/
configure() {
const me = this;
me._config = helpers.merge({}, [
me._config = merge({}, [
me.chart.options[me._type].datasets,
me.getDataset(),
], {
merger(key, target, source) {
if (key !== 'data') {
helpers._merger(key, target, source);
_merger(key, target, source);
}
}
});
@@ -489,9 +490,9 @@ export default class DatasetController {
meta._parsed = data;
meta._sorted = true;
} else {
if (helpers.isArray(data[start])) {
if (isArray(data[start])) {
parsed = me.parseArrayData(meta, data, start, count);
} else if (helpers.isObject(data[start])) {
} else if (isObject(data[start])) {
parsed = me.parseObjectData(meta, data, start, count);
} else {
parsed = me.parsePrimitiveData(meta, data, start, count);
@@ -757,7 +758,7 @@ export default class DatasetController {
me._cachedAnimations = {};
me._cachedDataOpts = {};
me.update(mode);
meta._clip = toClip(helpers.valueOrDefault(me._config.clip, defaultClip(meta.xScale, meta.yScale, me.getMaxOverflow())));
meta._clip = toClip(valueOrDefault(me._config.clip, defaultClip(meta.xScale, meta.yScale, me.getMaxOverflow())));
me._cacheScaleStackStatus();
}
@@ -787,7 +788,6 @@ export default class DatasetController {
*/
_addAutomaticHoverColors(index, options) {
const me = this;
const getHoverColor = helpers.getHoverColor;
const normalOptions = me.getStyle(index);
const missingColors = Object.keys(normalOptions).filter(key => key.indexOf('Color') !== -1 && !(key in options));
let i = missingColors.length - 1;
@@ -888,7 +888,7 @@ export default class DatasetController {
const info = {cacheable: !active};
let keys, i, ilen, key, value, readKey;
if (helpers.isArray(elementOptions)) {
if (isArray(elementOptions)) {
for (i = 0, ilen = elementOptions.length; i < ilen; ++i) {
key = elementOptions[i];
readKey = active ? 'hover' + key.charAt(0).toUpperCase() + key.slice(1) : key;
@@ -947,7 +947,7 @@ export default class DatasetController {
const context = me._getContext(index, active);
const datasetAnim = resolve([me._config.animation], context, index, info);
const chartAnim = resolve([chart.options.animation], context, index, info);
let config = helpers.mergeIf({}, [datasetAnim, chartAnim]);
let config = mergeIf({}, [datasetAnim, chartAnim]);
if (config[mode]) {
config = Object.assign({}, config, config[mode]);

View File

@@ -1,6 +1,6 @@
import helpers from '../helpers/index';
import {_isPointInArea} from '../helpers/helpers.canvas';
import {_lookupByKey, _rlookupByKey} from '../helpers/helpers.collection';
import {getRelativePosition as helpersGetRelativePosition} from '../helpers/helpers.dom';
/**
* @typedef { import("./core.controller").default } Chart
@@ -23,7 +23,7 @@ function getRelativePosition(e, chart) {
};
}
return helpers.dom.getRelativePosition(e, chart);
return helpersGetRelativePosition(e, chart);
}
/**

View File

@@ -0,0 +1,15 @@
export function fontString(pixelSize, fontStyle, fontFamily) {
return fontStyle + ' ' + pixelSize + 'px ' + fontFamily;
}
/**
* Request animation polyfill
*/
export const requestAnimFrame = (function() {
if (typeof window === 'undefined') {
return function(callback) {
return callback();
};
}
return window.requestAnimationFrame;
}());

View File

@@ -10,6 +10,7 @@ import * as math from './helpers.math';
import * as rtl from './helpers.rtl';
import {color, getHoverColor} from './helpers.color';
import {requestAnimFrame, fontString} from './helpers.extras';
export default {
...coreHelpers,
@@ -21,21 +22,9 @@ export default {
math,
rtl,
/**
* Request animation polyfill
*/
requestAnimFrame: (function() {
if (typeof window === 'undefined') {
return function(callback) {
return callback();
};
}
return window.requestAnimationFrame;
}()),
requestAnimFrame,
// -- Canvas methods
fontString(pixelSize, fontStyle, fontFamily) {
return fontStyle + ' ' + pixelSize + 'px ' + fontFamily;
},
fontString,
color,
getHoverColor
};

View File

@@ -2,9 +2,10 @@
* Chart.Platform implementation for targeting a web browser
*/
import helpers from '../helpers/index';
import BasePlatform from './platform.base';
import {_getParentNode} from '../helpers/helpers.dom';
import {_getParentNode, getStyle, getRelativePosition} from '../helpers/helpers.dom';
import {requestAnimFrame} from '../helpers/helpers.extras';
import {isNullOrUndef} from '../helpers/helpers.core';
/**
* @typedef { import("../core/core.controller").default } Chart
@@ -39,7 +40,7 @@ const EVENT_TYPES = {
* @returns {number=} Size in pixels or undefined if unknown.
*/
function readUsedSize(element, property) {
const value = helpers.dom.getStyle(element, property);
const value = getStyle(element, property);
const matches = value && value.match(/^(\d+)(\.\d+)?px$/);
return matches ? +matches[1] : undefined;
}
@@ -151,7 +152,7 @@ function createEvent(type, chart, x, y, nativeEvent) {
function fromNativeEvent(event, chart) {
const type = EVENT_TYPES[event.type] || event.type;
const pos = helpers.dom.getRelativePosition(event, chart);
const pos = getRelativePosition(event, chart);
return createEvent(type, chart, pos.x, pos.y, event);
}
@@ -164,7 +165,7 @@ function throttled(fn, thisArg) {
if (!ticking) {
ticking = true;
helpers.requestAnimFrame.call(window, () => {
requestAnimFrame.call(window, () => {
ticking = false;
fn.apply(thisArg, args);
});
@@ -309,7 +310,7 @@ export default class DomPlatform extends BasePlatform {
const initial = canvas[EXPANDO_KEY].initial;
['height', 'width'].forEach((prop) => {
const value = initial[prop];
if (helpers.isNullOrUndef(value)) {
if (isNullOrUndef(value)) {
canvas.removeAttribute(prop);
} else {
canvas.setAttribute(prop, value);

View File

@@ -1,7 +1,8 @@
import defaults from '../core/core.defaults';
import Element from '../core/core.element';
import helpers from '../helpers/index';
import layouts from '../core/core.layouts';
import {isArray, valueOrDefault, mergeIf} from '../helpers/helpers.core';
import {toPadding, _parseFont} from '../helpers/helpers.options';
defaults.set('title', {
align: 'center',
@@ -116,9 +117,9 @@ export class Title extends Element {
return;
}
const lineCount = helpers.isArray(opts.text) ? opts.text.length : 1;
me._padding = helpers.options.toPadding(opts.padding);
const textSize = lineCount * helpers.options._parseFont(opts).lineHeight + me._padding.height;
const lineCount = isArray(opts.text) ? opts.text.length : 1;
me._padding = toPadding(opts.padding);
const textSize = lineCount * _parseFont(opts).lineHeight + me._padding.height;
me.width = minSize.width = isHorizontal ? me.maxWidth : textSize;
me.height = minSize.height = isHorizontal ? textSize : me.maxHeight;
}
@@ -141,7 +142,7 @@ export class Title extends Element {
return;
}
const fontOpts = helpers.options._parseFont(opts);
const fontOpts = _parseFont(opts);
const lineHeight = fontOpts.lineHeight;
const offset = lineHeight / 2 + me._padding.top;
let rotation = 0;
@@ -194,7 +195,7 @@ export class Title extends Element {
ctx.save();
ctx.fillStyle = helpers.valueOrDefault(opts.fontColor, defaults.fontColor); // render in correct colour
ctx.fillStyle = valueOrDefault(opts.fontColor, defaults.fontColor); // render in correct colour
ctx.font = fontOpts.string;
ctx.translate(titleX, titleY);
@@ -203,7 +204,7 @@ export class Title extends Element {
ctx.textBaseline = 'middle';
const text = opts.text;
if (helpers.isArray(text)) {
if (isArray(text)) {
let y = 0;
for (let i = 0; i < text.length; ++i) {
ctx.fillText(text[i], 0, y, maxWidth);
@@ -254,7 +255,7 @@ export default {
const titleBlock = chart.titleBlock;
if (titleOpts) {
helpers.mergeIf(titleOpts, defaults.title);
mergeIf(titleOpts, defaults.title);
if (titleBlock) {
layouts.configure(chart, titleBlock, titleOpts);

View File

@@ -2,15 +2,15 @@ import Animations from '../core/core.animations';
import defaults from '../core/core.defaults';
import Element from '../core/core.element';
import plugins from '../core/core.plugins';
import helpers from '../helpers/index';
import {valueOrDefault, each, noop, isNullOrUndef, isArray, _elementsEqual} from '../helpers/helpers.core';
import {getRtlAdapter, overrideTextDirection, restoreTextDirection} from '../helpers/helpers.rtl';
import {fontString} from '../helpers/helpers.extras';
import {distanceBetweenPoints} from '../helpers/helpers.math';
/**
* @typedef { import("../platform/platform.base").IEvent } IEvent
*/
const valueOrDefault = helpers.valueOrDefault;
const getRtlHelper = helpers.rtl.getRtlAdapter;
defaults.set('tooltips', {
enabled: true,
custom: null,
@@ -54,7 +54,7 @@ defaults.set('tooltips', {
},
callbacks: {
// Args are: (tooltipItems, data)
beforeTitle: helpers.noop,
beforeTitle: noop,
title(tooltipItems, data) {
let title = '';
const labels = data.labels;
@@ -71,13 +71,13 @@ defaults.set('tooltips', {
return title;
},
afterTitle: helpers.noop,
afterTitle: noop,
// Args are: (tooltipItems, data)
beforeBody: helpers.noop,
beforeBody: noop,
// Args are: (tooltipItem, data)
beforeLabel: helpers.noop,
beforeLabel: noop,
label(tooltipItem, data) {
let label = data.datasets[tooltipItem.datasetIndex].label || '';
@@ -85,7 +85,7 @@ defaults.set('tooltips', {
label += ': ';
}
const value = tooltipItem.value;
if (!helpers.isNullOrUndef(value)) {
if (!isNullOrUndef(value)) {
label += value;
}
return label;
@@ -101,15 +101,15 @@ defaults.set('tooltips', {
labelTextColor() {
return this.options.bodyFontColor;
},
afterLabel: helpers.noop,
afterLabel: noop,
// Args are: (tooltipItems, data)
afterBody: helpers.noop,
afterBody: noop,
// Args are: (tooltipItems, data)
beforeFooter: helpers.noop,
footer: helpers.noop,
afterFooter: helpers.noop
beforeFooter: noop,
footer: noop,
afterFooter: noop
}
});
@@ -163,7 +163,7 @@ const positioners = {
const el = items[i].element;
if (el && el.hasValue()) {
const center = el.getCenterPoint();
const d = helpers.math.distanceBetweenPoints(eventPosition, center);
const d = distanceBetweenPoints(eventPosition, center);
if (d < minDistance) {
minDistance = d;
@@ -188,7 +188,7 @@ const positioners = {
// Helper to push or concat based on if the 2nd parameter is an array or not
function pushOrConcat(base, toPush) {
if (toPush) {
if (helpers.isArray(toPush)) {
if (isArray(toPush)) {
// base = base.concat(toPush);
Array.prototype.push.apply(base, toPush);
} else {
@@ -300,27 +300,27 @@ function getTooltipSize(tooltip) {
ctx.save();
ctx.font = helpers.fontString(titleFontSize, options.titleFontStyle, options.titleFontFamily);
helpers.each(tooltip.title, maxLineWidth);
ctx.font = fontString(titleFontSize, options.titleFontStyle, options.titleFontFamily);
each(tooltip.title, maxLineWidth);
// Body width
ctx.font = helpers.fontString(bodyFontSize, options.bodyFontStyle, options.bodyFontFamily);
helpers.each(tooltip.beforeBody.concat(tooltip.afterBody), maxLineWidth);
ctx.font = fontString(bodyFontSize, options.bodyFontStyle, options.bodyFontFamily);
each(tooltip.beforeBody.concat(tooltip.afterBody), maxLineWidth);
// Body lines may include some extra width due to the color box
widthPadding = options.displayColors ? (boxWidth + 2) : 0;
helpers.each(body, (bodyItem) => {
helpers.each(bodyItem.before, maxLineWidth);
helpers.each(bodyItem.lines, maxLineWidth);
helpers.each(bodyItem.after, maxLineWidth);
each(body, (bodyItem) => {
each(bodyItem.before, maxLineWidth);
each(bodyItem.lines, maxLineWidth);
each(bodyItem.after, maxLineWidth);
});
// Reset back to 0
widthPadding = 0;
// Footer width
ctx.font = helpers.fontString(footerFontSize, options.footerFontStyle, options.footerFontFamily);
helpers.each(tooltip.footer, maxLineWidth);
ctx.font = fontString(footerFontSize, options.footerFontStyle, options.footerFontFamily);
each(tooltip.footer, maxLineWidth);
ctx.restore();
@@ -546,7 +546,7 @@ export class Tooltip extends Element {
const callbacks = me.options.callbacks;
const bodyItems = [];
helpers.each(tooltipItems, (tooltipItem) => {
each(tooltipItems, (tooltipItem) => {
const bodyItem = {
before: [],
lines: [],
@@ -613,7 +613,7 @@ export class Tooltip extends Element {
}
// Determine colors for boxes
helpers.each(tooltipItems, (tooltipItem) => {
each(tooltipItems, (tooltipItem) => {
labelColors.push(options.callbacks.labelColor.call(me, tooltipItem, me._chart));
labelTextColors.push(options.callbacks.labelTextColor.call(me, tooltipItem, me._chart));
});
@@ -733,7 +733,7 @@ export class Tooltip extends Element {
let titleFontSize, titleSpacing, i;
if (length) {
const rtlHelper = getRtlHelper(options.rtl, me.x, me.width);
const rtlHelper = getRtlAdapter(options.rtl, me.x, me.width);
pt.x = getAlignedX(me, options.titleAlign);
@@ -744,7 +744,7 @@ export class Tooltip extends Element {
titleSpacing = options.titleSpacing;
ctx.fillStyle = options.titleFontColor;
ctx.font = helpers.fontString(titleFontSize, options.titleFontStyle, options.titleFontFamily);
ctx.font = fontString(titleFontSize, options.titleFontStyle, options.titleFontFamily);
for (i = 0; i < length; ++i) {
ctx.fillText(title[i], rtlHelper.x(pt.x), pt.y + titleFontSize / 2);
@@ -794,7 +794,7 @@ export class Tooltip extends Element {
let bodyLineHeight = bodyFontSize;
let xLinePadding = 0;
const rtlHelper = getRtlHelper(options.rtl, me.x, me.width);
const rtlHelper = getRtlAdapter(options.rtl, me.x, me.width);
const fillLineOfText = function(line) {
ctx.fillText(line, rtlHelper.x(pt.x + xLinePadding), pt.y + bodyLineHeight / 2);
@@ -806,13 +806,13 @@ export class Tooltip extends Element {
ctx.textAlign = bodyAlign;
ctx.textBaseline = 'middle';
ctx.font = helpers.fontString(bodyFontSize, options.bodyFontStyle, options.bodyFontFamily);
ctx.font = fontString(bodyFontSize, options.bodyFontStyle, options.bodyFontFamily);
pt.x = getAlignedX(me, bodyAlignForCalculation);
// Before body lines
ctx.fillStyle = options.bodyFontColor;
helpers.each(me.beforeBody, fillLineOfText);
each(me.beforeBody, fillLineOfText);
xLinePadding = displayColors && bodyAlignForCalculation !== 'right'
? bodyAlign === 'center' ? (boxWidth / 2 + 1) : (boxWidth + 2)
@@ -824,7 +824,7 @@ export class Tooltip extends Element {
textColor = me.labelTextColors[i];
ctx.fillStyle = textColor;
helpers.each(bodyItem.before, fillLineOfText);
each(bodyItem.before, fillLineOfText);
lines = bodyItem.lines;
// Draw Legend-like boxes if needed
@@ -839,7 +839,7 @@ export class Tooltip extends Element {
bodyLineHeight = bodyFontSize;
}
helpers.each(bodyItem.after, fillLineOfText);
each(bodyItem.after, fillLineOfText);
}
// Reset back to 0 for after body
@@ -847,7 +847,7 @@ export class Tooltip extends Element {
bodyLineHeight = bodyFontSize;
// After body lines
helpers.each(me.afterBody, fillLineOfText);
each(me.afterBody, fillLineOfText);
pt.y -= bodySpacing; // Remove last body spacing
}
@@ -859,7 +859,7 @@ export class Tooltip extends Element {
let footerFontSize, i;
if (length) {
const rtlHelper = getRtlHelper(options.rtl, me.x, me.width);
const rtlHelper = getRtlAdapter(options.rtl, me.x, me.width);
pt.x = getAlignedX(me, options.footerAlign);
pt.y += options.footerMarginTop;
@@ -870,7 +870,7 @@ export class Tooltip extends Element {
footerFontSize = options.footerFontSize;
ctx.fillStyle = options.footerFontColor;
ctx.font = helpers.fontString(footerFontSize, options.footerFontStyle, options.footerFontFamily);
ctx.font = fontString(footerFontSize, options.footerFontStyle, options.footerFontFamily);
for (i = 0; i < length; ++i) {
ctx.fillText(footer[i], rtlHelper.x(pt.x), pt.y + footerFontSize / 2);
@@ -985,7 +985,7 @@ export class Tooltip extends Element {
// Draw Background
me.drawBackground(pt, ctx, tooltipSize);
helpers.rtl.overrideTextDirection(ctx, options.textDirection);
overrideTextDirection(ctx, options.textDirection);
pt.y += options.yPadding;
@@ -998,7 +998,7 @@ export class Tooltip extends Element {
// Footer
me.drawFooter(pt, ctx);
helpers.rtl.restoreTextDirection(ctx, options.textDirection);
restoreTextDirection(ctx, options.textDirection);
ctx.restore();
}
@@ -1026,7 +1026,7 @@ export class Tooltip extends Element {
}
// Remember Last Actives
changed = replay || !helpers._elementsEqual(active, lastActive);
changed = replay || !_elementsEqual(active, lastActive);
// Only handle target event on tooltip change
if (changed) {

View File

@@ -1,13 +1,11 @@
import defaults from '../core/core.defaults';
import helpers from '../helpers/index';
import {_longestText} from '../helpers/helpers.canvas';
import {isNumber, toDegrees, toRadians, _normalizeAngle} from '../helpers/helpers.math';
import LinearScaleBase from './scale.linearbase';
import Ticks from '../core/core.ticks';
import {valueOrDefault, isArray, valueAtIndexOrDefault, isFinite, callback as callCallback, isNullOrUndef} from '../helpers/helpers.core';
import {_parseFont, resolve} from '../helpers/helpers.options';
const valueOrDefault = helpers.valueOrDefault;
const valueAtIndexOrDefault = helpers.valueAtIndexOrDefault;
const resolve = helpers.options.resolve;
const defaultConfig = {
display: true,
@@ -69,7 +67,7 @@ function getTickBackdropHeight(opts) {
}
function measureLabelSize(ctx, lineHeight, label) {
if (helpers.isArray(label)) {
if (isArray(label)) {
return {
w: _longestText(ctx, ctx.font, label),
h: label.length * lineHeight
@@ -132,7 +130,7 @@ function fitWithPointLabels(scale) {
//
// https://dl.dropboxusercontent.com/u/34601363/yeahscience.gif
const plFont = helpers.options._parseFont(scale.options.pointLabels);
const plFont = _parseFont(scale.options.pointLabels);
// Get maximum radius of the polygon. Either half the height (minus the text width) or half the width.
// Use this to calculate the offset + change. - Make sure L/R protrusion is at least 0 to stop issues with centre points
@@ -198,7 +196,7 @@ function fillText(ctx, text, position, lineHeight) {
let y = position.y + lineHeight / 2;
let i, ilen;
if (helpers.isArray(text)) {
if (isArray(text)) {
for (i = 0, ilen = text.length; i < ilen; ++i) {
ctx.fillText(text[i], position.x, y);
y += lineHeight;
@@ -222,7 +220,7 @@ function drawPointLabels(scale) {
const pointLabelOpts = opts.pointLabels;
const tickBackdropHeight = getTickBackdropHeight(opts);
const outerDistance = scale.getDistanceFromCenterForValue(opts.ticks.reverse ? scale.min : scale.max);
const plFont = helpers.options._parseFont(pointLabelOpts);
const plFont = _parseFont(pointLabelOpts);
ctx.save();
@@ -332,8 +330,8 @@ export default class RadialLinearScale extends LinearScaleBase {
const min = minmax.min;
const max = minmax.max;
me.min = helpers.isFinite(min) && !isNaN(min) ? min : 0;
me.max = helpers.isFinite(max) && !isNaN(max) ? max : 0;
me.min = isFinite(min) && !isNaN(min) ? min : 0;
me.max = isFinite(max) && !isNaN(max) ? max : 0;
// Common base implementation to handle min, max, beginAtZero
me.handleTickRangeOptions();
@@ -354,7 +352,7 @@ export default class RadialLinearScale extends LinearScaleBase {
// Point labels
me.pointLabels = me.chart.data.labels.map((value, index) => {
const label = helpers.callback(me.options.pointLabels.callback, [value, index], me);
const label = callCallback(me.options.pointLabels.callback, [value, index], me);
return label || label === 0 ? label : '';
});
}
@@ -415,7 +413,7 @@ export default class RadialLinearScale extends LinearScaleBase {
getDistanceFromCenterForValue(value) {
const me = this;
if (helpers.isNullOrUndef(value)) {
if (isNullOrUndef(value)) {
return NaN;
}
@@ -507,7 +505,7 @@ export default class RadialLinearScale extends LinearScaleBase {
}
const startAngle = me.getIndexAngle(0);
const tickFont = helpers.options._parseFont(tickOpts);
const tickFont = _parseFont(tickOpts);
const tickFontColor = valueOrDefault(tickOpts.fontColor, defaults.fontColor);
let offset, width;