-
Notifications
You must be signed in to change notification settings - Fork 319
/
annotation.js
190 lines (160 loc) · 4.67 KB
/
annotation.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import {Chart} from 'chart.js';
import {clipArea, unclipArea, isObject, isArray} from 'chart.js/helpers';
import {handleEvent, hooks, updateListeners} from './events';
import {adjustScaleRange, verifyScaleOptions} from './scale';
import {updateElements, resolveType} from './elements';
import {annotationTypes} from './types';
import {requireVersion} from './helpers';
import {name, version} from '../package.json';
const chartStates = new Map();
export default {
id: 'annotation',
version,
/* TODO: enable in v2
beforeRegister() {
requireVersion('chart.js', '3.7', Chart.version);
},
*/
afterRegister() {
Chart.register(annotationTypes);
// TODO: Remove this check, warning and workaround in v2
if (!requireVersion('chart.js', '3.7', Chart.version, false)) {
console.warn(`${name} has known issues with chart.js versions prior to 3.7, please consider upgrading.`);
// Workaround for https://github.com/chartjs/chartjs-plugin-annotation/issues/572
Chart.defaults.set('elements.lineAnnotation', {
callout: {},
font: {},
padding: 6
});
}
},
afterUnregister() {
Chart.unregister(annotationTypes);
},
beforeInit(chart) {
chartStates.set(chart, {
annotations: [],
elements: [],
visibleElements: [],
listeners: {},
listened: false,
moveListened: false,
hovered: []
});
},
beforeUpdate(chart, args, options) {
const state = chartStates.get(chart);
const annotations = state.annotations = [];
let annotationOptions = options.annotations;
if (isObject(annotationOptions)) {
Object.keys(annotationOptions).forEach(key => {
const value = annotationOptions[key];
if (isObject(value)) {
value.id = key;
annotations.push(value);
}
});
} else if (isArray(annotationOptions)) {
annotations.push(...annotationOptions);
}
verifyScaleOptions(annotations, chart.scales);
},
afterDataLimits(chart, args) {
const state = chartStates.get(chart);
adjustScaleRange(chart, args.scale, state.annotations.filter(a => a.display && a.adjustScaleRange));
},
afterUpdate(chart, args, options) {
const state = chartStates.get(chart);
updateListeners(chart, state, options);
updateElements(chart, state, options, args.mode);
state.visibleElements = state.elements.filter(el => !el.skip && el.options.display);
},
beforeDatasetsDraw(chart, _args, options) {
draw(chart, 'beforeDatasetsDraw', options.clip);
},
afterDatasetsDraw(chart, _args, options) {
draw(chart, 'afterDatasetsDraw', options.clip);
},
beforeDraw(chart, _args, options) {
draw(chart, 'beforeDraw', options.clip);
},
afterDraw(chart, _args, options) {
draw(chart, 'afterDraw', options.clip);
},
beforeEvent(chart, args, options) {
const state = chartStates.get(chart);
handleEvent(state, args.event, options);
},
destroy(chart) {
chartStates.delete(chart);
},
_getState(chart) {
return chartStates.get(chart);
},
defaults: {
animations: {
numbers: {
properties: ['x', 'y', 'x2', 'y2', 'width', 'height', 'pointX', 'pointY', 'labelX', 'labelY', 'labelWidth', 'labelHeight', 'radius'],
type: 'number'
},
},
clip: true,
dblClickSpeed: 350, // ms
drawTime: 'afterDatasetsDraw',
interaction: {
mode: undefined,
axis: undefined,
intersect: undefined
},
label: {
drawTime: null
}
},
descriptors: {
_indexable: false,
_scriptable: (prop) => !hooks.includes(prop),
annotations: {
_allKeys: false,
_fallback: (prop, opts) => `elements.${annotationTypes[resolveType(opts.type)].id}`,
},
interaction: {
_fallback: true,
}
},
additionalOptionScopes: ['']
};
function draw(chart, caller, clip) {
const {ctx, chartArea} = chart;
const {visibleElements} = chartStates.get(chart);
if (clip) {
clipArea(ctx, chartArea);
}
drawElements(ctx, visibleElements, caller);
drawSubElements(ctx, visibleElements, caller);
if (clip) {
unclipArea(ctx);
}
visibleElements.forEach(el => {
if (!('drawLabel' in el)) {
return;
}
const label = el.options.label;
if (label && label.enabled && label.content && (label.drawTime || el.options.drawTime) === caller) {
el.drawLabel(ctx, chartArea);
}
});
}
function drawElements(ctx, elements, caller) {
for (const el of elements) {
if (el.options.drawTime === caller) {
el.draw(ctx);
}
}
}
function drawSubElements(ctx, elements, caller) {
for (const el of elements) {
if (isArray(el.elements)) {
drawElements(ctx, el.elements, caller);
}
}
}