Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(annotations): 修复addAnnotations在双轴图等具有分view图表失效的问题 #3119

Merged
merged 2 commits into from
Feb 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions __tests__/bugs/issue-3084-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { DualAxes } from '../../src';
import { createDiv } from '../utils/dom';

const data = [
{ year: '1991', value: 3, count: 10 },
{ year: '1992', value: 4, count: 4 },
{ year: '1993', value: 3.5, count: 5 },
{ year: '1994', value: 5, count: 5 },
{ year: '1995', value: 4.9, count: 4.9 },
{ year: '1996', value: 6, count: 35 },
{ year: '1997', value: 7, count: 7 },
{ year: '1998', value: 9, count: 1 },
{ year: '1999', value: 13, count: 20 },
];

describe('#3084', () => {
it('addAnnotations on view', () => {
const dualAxes = new DualAxes(createDiv(), {
data: [data, data],
xField: 'year',
yField: ['value', 'count'],
geometryOptions: [
{
geometry: 'line',
color: '#5B8FF9',
},
{
geometry: 'line',
color: '#5AD8A6',
},
],
});

dualAxes.render();
dualAxes.addAnnotations([
{
type: 'text',
position: ['median', 'median'],
content: '辅助文本',
style: {
fill: 'red',
},
},
]);
expect(dualAxes.chart.getController('annotation').getComponents().length).toBe(1);
expect(dualAxes.chart.views[0].getController('annotation').getComponents().length).toBe(0);
expect(dualAxes.chart.views[1].getController('annotation').getComponents().length).toBe(0);
dualAxes.addAnnotations(
[
{
type: 'text',
position: ['median', 'median'],
content: '辅助文本',
style: {
fill: 'red',
},
},
],
dualAxes.chart.views[0]
);
expect(dualAxes.chart.getController('annotation').getComponents().length).toBe(1);
expect(dualAxes.chart.views[0].getController('annotation').getComponents().length).toBe(1);
expect(dualAxes.chart.views[1].getController('annotation').getComponents().length).toBe(0);
dualAxes.addAnnotations(
[
{
type: 'text',
position: ['median', 'median'],
content: '辅助文本',
style: {
fill: 'red',
},
},
],
dualAxes.chart.views[1]
);
expect(dualAxes.chart.getController('annotation').getComponents().length).toBe(1);
expect(dualAxes.chart.views[0].getController('annotation').getComponents().length).toBe(1);
expect(dualAxes.chart.views[1].getController('annotation').getComponents().length).toBe(1);
dualAxes.destroy();
});
});
21 changes: 19 additions & 2 deletions docs/api/plot-api.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,27 @@ Get all status information of the current plot.
### 12. addAnnotations

```sign
plot.addAnnotations(annotations: Annotation[]) => void;
plot.addAnnotations(annotations: Annotation[], view?: View) => void;
```

批量为当前图表增加图表标注。通过 id 匹配,如果匹配成功,则更新,匹配不成功则增加。
example:
```ts
dualAxes.addAnnotations(
[
{
type: 'text',
position: ['median', 'median'],
content: '辅助文本',
style: {
fill: 'red',
},
},
],
dualAxes.chart.views[0]
);
```

Add labels to the specified views in batches (default is the current chart). Matching by id, if the match is successful, it will be updated, and if the match is unsuccessful, it will be incremented.

<!-- <playground path="dynamic-plots/basic/demo/dynamic-spline.ts" rid="addAnnotations"></playground> -->

Expand Down
21 changes: 19 additions & 2 deletions docs/api/plot-api.zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,27 @@ plot.getStates();
### 12. addAnnotations

```sign
plot.addAnnotations(annotations: Annotation[]) => void;
plot.addAnnotations(annotations: Annotation[], view?: View) => void;
```

批量为当前图表增加图表标注。通过 id 匹配,如果匹配成功,则更新,匹配不成功则增加。
示例代码:
```ts
dualAxes.addAnnotations(
[
{
type: 'text',
position: ['median', 'median'],
content: '辅助文本',
style: {
fill: 'red',
},
},
],
dualAxes.chart.views[0]
);
```

批量为指定的视图增加标注(默认为当前图表)。通过 id 匹配,如果匹配成功,则更新,匹配不成功则增加。

<!-- <playground path="dynamic-plots/basic/demo/dynamic-spline.ts" rid="addAnnotations"></playground> -->

Expand Down
9 changes: 5 additions & 4 deletions src/core/plot.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Chart, Event, Element } from '@antv/g2';
import { Chart, Event, Element, View } from '@antv/g2';
import { each } from '@antv/util';
import EE from '@antv/event-emitter';
import { bind } from 'size-sensor';
Expand Down Expand Up @@ -241,9 +241,10 @@ export abstract class Plot<O extends PickOptions> extends EE {
/**
* 增加图表标注。通过 id 标识,如果匹配到,就做更新
*/
public addAnnotations(annotations: Annotation[]): void {
public addAnnotations(annotations: Annotation[], view?: View): void {
view = view ? view : this.chart;
const incoming = [...annotations];
const controller = this.chart.getController('annotation');
const controller = view.getController('annotation');
const current = controller.getComponents().map((co) => co.extra);

controller.clear(true);
Expand All @@ -259,7 +260,7 @@ export abstract class Plot<O extends PickOptions> extends EE {
}

incoming.forEach((annotation) => controller.annotation(annotation));
this.chart.render(true);
view.render(true);
}

/**
Expand Down