Skip to content

Commit 87de801

Browse files
authored
Add defined and loaded elements to annotation context (#869)
* Add loaded elements to annotation context * added types and doc * read only elements * added test cases modifying the annotations
1 parent 04fcde5 commit 87de801

File tree

4 files changed

+98
-2
lines changed

4 files changed

+98
-2
lines changed

docs/guide/options.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ In addition to [chart](#chart)
116116

117117
* `id`: the annotation id
118118
* `element`: the annotation element
119+
* `elements`: the array which contains the already created annotation elements.
119120
* `type`: `'annotation'`
120121

121122
The [annotation](#annotation) option context is passed to scriptable options in all other cases, except when resolving `id`, `type` or adjusting scale ranges. The same values resolved in `afterDataLimits` with [chart](#chart) context are again evaluated in `afterUpdate` with [annotation](#annotation) context.

src/elements.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export function updateElements(chart, state, options, mode) {
5252
for (let i = 0; i < annotations.length; i++) {
5353
const annotationOptions = annotations[i];
5454
const element = getOrCreateElement(elements, i, annotationOptions.type);
55-
const resolver = annotationOptions.setContext(getContext(chart, element, annotationOptions));
55+
const resolver = annotationOptions.setContext(getContext(chart, element, elements, annotationOptions));
5656
const properties = element.resolveElementProperties(chart, resolver);
5757

5858
properties.skip = toSkip(properties);
@@ -142,9 +142,12 @@ function resolveObj(resolver, defs) {
142142
return result;
143143
}
144144

145-
function getContext(chart, element, annotation) {
145+
function getContext(chart, element, elements, annotation) {
146146
return element.$context || (element.$context = Object.assign(Object.create(chart.getContext()), {
147147
element,
148+
get elements() {
149+
return elements.filter((el) => el && el.options);
150+
},
148151
id: annotation.id,
149152
type: 'annotation'
150153
}));

test/specs/annotation.spec.js

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,4 +211,94 @@ describe('Annotation plugin', function() {
211211
expect(element.options.drawTime).toBe(chart.options.plugins.annotation.annotations.label.drawTime);
212212
});
213213
});
214+
215+
describe('context', function() {
216+
it('should contain the loaded elements', function() {
217+
const counts = [];
218+
const annotations = [];
219+
for (let i = 0; i < 5; i++) {
220+
annotations.push({
221+
type: 'label',
222+
content: 'test',
223+
display(context) {
224+
expect(context.elements.length).toBe(i);
225+
counts.push(i);
226+
}
227+
});
228+
}
229+
acquireChart({
230+
type: 'line',
231+
options: {
232+
plugins: {
233+
annotation: {
234+
annotations
235+
}
236+
}
237+
}
238+
});
239+
expect(counts).toEqual([0, 1, 2, 3, 4]);
240+
});
241+
it('should contain the loaded elements after update', function() {
242+
const counts = [];
243+
const annotations = [];
244+
for (let i = 0; i < 5; i++) {
245+
annotations.push({
246+
type: 'label',
247+
content: 'test',
248+
display(context) {
249+
expect(context.elements.length).toBe(i);
250+
counts.push(i);
251+
}
252+
});
253+
}
254+
const chart = acquireChart({
255+
type: 'line',
256+
options: {
257+
plugins: {
258+
annotation: {
259+
annotations
260+
}
261+
}
262+
}
263+
});
264+
counts.splice(0, counts.length);
265+
chart.update();
266+
expect(counts).toEqual([0, 1, 2, 3, 4]);
267+
});
268+
269+
it('should contain the loaded elements after reducing annotations and chart update', function() {
270+
const counts = [];
271+
const annotations = [];
272+
for (let i = 0; i < 5; i++) {
273+
annotations.push({
274+
type: 'label',
275+
content: 'test',
276+
display(context) {
277+
const check = context.chart.options.plugins.annotation.annotations.length < 5;
278+
if (check) {
279+
expect(context.elements.length).toBe(i - 2);
280+
counts.push(i);
281+
}
282+
}
283+
});
284+
}
285+
const chart = acquireChart({
286+
type: 'line',
287+
options: {
288+
plugins: {
289+
annotation: {
290+
annotations
291+
}
292+
}
293+
}
294+
});
295+
counts.splice(0, counts.length);
296+
chart.update();
297+
counts.splice(0, counts.length);
298+
chart.options.plugins.annotation.annotations = annotations.slice(2);
299+
chart.update();
300+
expect(counts).toEqual([2, 3, 4]);
301+
});
302+
303+
});
214304
});

types/events.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { AnnotationElement } from './element';
44
export interface EventContext {
55
chart: Chart,
66
element: AnnotationElement,
7+
elements: AnnotationElement[],
78
id: string,
89
type: string
910
}
@@ -15,6 +16,7 @@ export interface EventContext {
1516
export interface PartialEventContext {
1617
chart: Chart,
1718
element?: Partial<AnnotationElement>,
19+
elements?: AnnotationElement[],
1820
id?: string,
1921
type?: string
2022
}

0 commit comments

Comments
 (0)