-
Notifications
You must be signed in to change notification settings - Fork 320
/
utils.js
82 lines (74 loc) · 2.9 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import {each} from 'chart.js/helpers';
export const getModifierKey = opts => opts && opts.enabled && opts.modifierKey;
export const keyPressed = (key, event) => key && event[key + 'Key'];
export const keyNotPressed = (key, event) => key && !event[key + 'Key'];
/**
* @param {string|function} mode can be 'x', 'y' or 'xy'
* @param {string} dir can be 'x' or 'y'
* @param {import('chart.js').Chart} chart instance of the chart in question
* @returns {boolean}
*/
export function directionEnabled(mode, dir, chart) {
if (mode === undefined) {
return true;
} else if (typeof mode === 'string') {
return mode.indexOf(dir) !== -1;
} else if (typeof mode === 'function') {
return mode({chart}).indexOf(dir) !== -1;
}
return false;
}
/**
* Debounces calling `fn` for `delay` ms
* @param {function} fn - Function to call. No arguments are passed.
* @param {number} delay - Delay in ms. 0 = immediate invocation.
* @returns {function}
*/
export function debounce(fn, delay) {
let timeout;
return function() {
clearTimeout(timeout);
timeout = setTimeout(fn, delay);
return delay;
};
}
/** This function use for check what axis now under mouse cursor.
* @param {{x: number, y: number}} point - the mouse location
* @param {import('chart.js').Chart} [chart] instance of the chart in question
* @return {import('chart.js').Scale}
*/
function getScaleUnderPoint({x, y}, chart) {
const scales = chart.scales;
const scaleIds = Object.keys(scales);
for (let i = 0; i < scaleIds.length; i++) {
const scale = scales[scaleIds[i]];
if (y >= scale.top && y <= scale.bottom && x >= scale.left && x <= scale.right) {
return scale;
}
}
return null;
}
/** This function return only one scale whose position is under mouse cursor and which direction is enabled.
* If under mouse hasn't scale, then return all other scales which 'mode' is diffrent with overScaleMode.
* So 'overScaleMode' works as a limiter to scale the user-selected scale (in 'mode') only when the cursor is under the scale,
* and other directions in 'mode' works as before.
* Example: mode = 'xy', overScaleMode = 'y' -> it's means 'x' - works as before, and 'y' only works for one scale when cursor is under it.
* options.overScaleMode can be a function if user want zoom only one scale of many for example.
* @param {string} mode - 'xy', 'x' or 'y'
* @param {{x: number, y: number}} point - the mouse location
* @param {import('chart.js').Chart} [chart] instance of the chart in question
* @return {import('chart.js').Scale[]}
*/
export function getEnabledScalesByPoint(mode, point, chart) {
const scale = getScaleUnderPoint(point, chart);
if (scale && directionEnabled(mode, scale.axis, chart)) {
return [scale];
}
const enabledScales = [];
each(chart.scales, function(scaleItem) {
if (!directionEnabled(mode, scaleItem.axis, chart)) {
enabledScales.push(scaleItem);
}
});
return enabledScales;
}