Skip to content

Commit c0a3f97

Browse files
author
xinming.lxj
committed
feat: 提供更新图标标注方法
1 parent 3d5ac77 commit c0a3f97

File tree

5 files changed

+71
-25
lines changed

5 files changed

+71
-25
lines changed

__tests__/unit/core/index-spec.ts

+27
Original file line numberDiff line numberDiff line change
@@ -318,4 +318,31 @@ describe('core', () => {
318318

319319
plot.destroy();
320320
});
321+
322+
it('update annotations', () => {
323+
const line = new Line(createDiv(), {
324+
data: partySupport.filter((o) => o.type === 'FF'),
325+
xField: 'date',
326+
yField: 'value',
327+
annotations: [
328+
{ type: 'line', start: ['min', 'median'], end: ['max', 'median'], id: 'ID' },
329+
{ type: 'line', start: ['min', 'median'], end: ['max', 'median'] },
330+
],
331+
});
332+
333+
line.render();
334+
335+
expect(line.chart.getController('annotation').getComponents().length).toBe(2);
336+
337+
line.updateAnnotation([{ type: 'image', start: ['min', 'median'], end: ['max', 'median'], src: 'xx', id: 'ID' }]);
338+
339+
const annotations = line.chart.getController('annotation').getComponents();
340+
expect(annotations.length).toBe(2);
341+
expect(annotations.find((co) => co.extra.id === 'ID').component.get('type')).toBe('image');
342+
343+
line.updateAnnotation([{ type: 'image', start: ['min', 'median'], end: ['max', 'median'], src: 'xx' }]);
344+
expect(line.chart.getController('annotation').getComponents().length).toBe(3);
345+
346+
line.destroy();
347+
});
321348
});

examples/component/annotation/demo/line-annotation-position.jsx

+14-23
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ const Plot = () => {
3131
annotations: [
3232
{
3333
type: 'line',
34+
id: 'line',
3435
/** 起始位置 */
3536
start: [startX, startY],
3637
/** 结束位置 */
@@ -58,34 +59,24 @@ const Plot = () => {
5859

5960
useEffect(() => {
6061
if (plotRef?.current) {
61-
// fixme 提供更新 annotation 的方法
62-
const annotationController = plotRef.current.chart.getController('annotation');
63-
annotationController.clear(true);
6462
let label = '辅助线';
6563
if (startY === endY) {
66-
label = options.find(opt => opt.value === startY)?.label || label;
64+
label = options.find((opt) => opt.value === startY)?.label || label;
6765
}
68-
annotationController.annotation({
69-
type: 'line',
70-
/** 起始位置 */
71-
start: [startX, startY],
72-
/** 结束位置 */
73-
end: [endX, endY],
74-
text: {
75-
content: label,
76-
position: 'right',
77-
offsetY: 18,
78-
style: {
79-
textAlign: 'right',
66+
67+
plotRef.current.updateAnnotation([
68+
{
69+
type: 'line',
70+
id: 'line',
71+
/** 起始位置 */
72+
start: [startX, startY],
73+
/** 结束位置 */
74+
end: [endX, endY],
75+
text: {
76+
content: label,
8077
},
8178
},
82-
style: {
83-
lineWidth: 0.5,
84-
lineDash: [4, 4],
85-
},
86-
});
87-
88-
plotRef.current.chart.render(true);
79+
]);
8980
}
9081
}, [startX, startY, endX, endY]);
9182

src/core/plot.ts

+26-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Chart, Event, Element } from '@antv/g2';
22
import { each } from '@antv/util';
33
import EE from '@antv/event-emitter';
44
import { bind } from 'size-sensor';
5-
import { Options, StateName, StateCondition, Size, StateObject } from '../types';
5+
import { Options, StateName, StateCondition, Size, StateObject, Annotation } from '../types';
66
import { getContainerSize, getAllElementsRecursively, deepAssign, pick } from '../utils';
77
import { Adaptor } from './adaptor';
88

@@ -236,6 +236,31 @@ export abstract class Plot<O extends PickOptions> extends EE {
236236
this.chart.changeSize(width, height);
237237
}
238238

239+
/**
240+
* 更新图表标注, 通过 id 标识
241+
*/
242+
public updateAnnotation(annotations: Annotation[]): void {
243+
const incoming = [...annotations];
244+
const controller = this.chart.getController('annotation');
245+
const current = controller.getComponents().map((co) => co.extra);
246+
247+
controller.clear(true);
248+
for (let i = 0; i < current.length; i++) {
249+
let annotation = current[i];
250+
251+
const findIndex = incoming.findIndex((item) => item.id === annotation.id);
252+
if (findIndex !== -1) {
253+
annotation = deepAssign({}, annotation, incoming[findIndex]);
254+
incoming.splice(findIndex, 1);
255+
}
256+
controller.annotation(annotation);
257+
}
258+
259+
incoming.forEach((annotation) => controller.annotation(annotation));
260+
261+
this.chart.render(true);
262+
}
263+
239264
/**
240265
* 销毁
241266
*/

src/types/annotation.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Types } from '@antv/g2';
22

3-
export type Annotation =
3+
type AnnotationOption =
44
| Types.ArcOption
55
| Types.ImageOption
66
| Types.LineOption
@@ -11,3 +11,5 @@ export type Annotation =
1111
| Types.DataRegionOption
1212
| Types.ShapeAnnotationOption
1313
| Types.HtmlAnnotationOption;
14+
15+
export type Annotation = { id?: string } & AnnotationOption;

src/types/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export * from './statistic';
66
export * from './meta';
77
export * from './axis';
88
export * from './interaction';
9+
export * from './annotation';
910
export * from './locale';
1011
export * from './button';
1112

0 commit comments

Comments
 (0)