Skip to content

Commit 706942a

Browse files
authored
feat(conversion-tag): 转化分析 tag 添加 elements 信息,方便业务上使用 (#2441)
* feat(conversion-tag): 转化分析 tag 添加 elements 信息,方便业务上使用 * docs(conversion-tag): 转化率标签组件的事件交互 文档以及demo说明 * docs: 修改文案
1 parent 9836e08 commit 706942a

31 files changed

+295
-69
lines changed

__tests__/unit/adaptor/conversion-tag-spec.ts

+52-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { Column, ColumnOptions, Bar, BarOptions } from '../../../src';
44
import { createDiv } from '../../utils/dom';
55
import { delay } from '../../utils/delay';
66
import { near } from '../../utils/number';
7+
import { dispatchEvent } from '../../utils/event';
78
import { subSalesByArea } from '../../data/sales';
89

910
const DATA = [
@@ -66,19 +67,27 @@ describe('column conversion tag', () => {
6667
expect(texts[idx - 1].attr('text')).toBe(((DATA[idx].y / DATA[idx - 1].y) * 100).toFixed(2) + '%');
6768
}
6869
});
70+
texts.forEach((text, idx) => {
71+
// origin info
72+
expect(text.get('origin').element).toBe(plot.chart.geometries[0].elements[idx]);
73+
expect(text.get('origin').nextElement).toBe(plot.chart.geometries[0].elements[idx + 1]);
74+
});
6975

7076
// 箭头
7177
const arrows = group.findAllByName('conversion-tag-arrow');
7278
const arrowIds = arrows.map((arrow) => arrow.get('id')) as string[];
7379
// 每一个都有唯一的 ID
7480
expect(uniq(arrowIds)).toHaveLength(DATA.length - 1);
7581
expect(arrows).toHaveLength(DATA.length - 1);
76-
arrows.forEach((arrow) => {
82+
arrows.forEach((arrow, idx) => {
7783
const bbox = arrow.getBBox();
7884
// spacing: 8
7985
expect(near(bbox.width, totalWidth - 8 * 2)).toBeTruthy();
8086
// size: 32
8187
expect(near(bbox.height, 32)).toBeTruthy();
88+
// origin info
89+
expect(arrow.get('origin').element).toBe(plot.chart.geometries[0].elements[idx]);
90+
expect(arrow.get('origin').nextElement).toBe(plot.chart.geometries[0].elements[idx + 1]);
8291
});
8392
});
8493

@@ -604,3 +613,45 @@ describe('conversion tag disabled with seriesField', () => {
604613
expect(group).toBeUndefined();
605614
});
606615
});
616+
617+
describe('conversion tag listent to events', () => {
618+
const container = createDiv();
619+
620+
const options: ColumnOptions = {
621+
data: DATA,
622+
autoFit: false,
623+
width: 600,
624+
height: 400,
625+
xField: 'x',
626+
yField: 'y',
627+
conversionTag: {},
628+
animation: false,
629+
};
630+
const plot = new Column(container, options);
631+
632+
it('events', async () => {
633+
plot.render();
634+
let c;
635+
plot.on('conversion-tag-group:mouseenter', (evt) => {
636+
c = evt?.target;
637+
});
638+
639+
await delay(100);
640+
641+
const foreground = plot.chart.foregroundGroup;
642+
const group: IGroup = foreground.findAllByName('conversion-tag-group')[0] as IGroup;
643+
// 箭头
644+
const arrows = group.findAllByName('conversion-tag-arrow');
645+
const texts = group.findAllByName('conversion-tag-text');
646+
647+
const canvas = plot.chart.getCanvas();
648+
const bbox = arrows[0].getCanvasBBox();
649+
dispatchEvent(bbox, canvas, 'mouseenter');
650+
651+
await delay(100);
652+
653+
expect(c?.get('id')).toBe(texts[0].get('id'));
654+
expect(c?.get('origin').element).toBe(plot.chart.geometries[0].elements[0]);
655+
expect(c?.get('origin').nextElement).toBe(plot.chart.geometries[0].elements[1]);
656+
});
657+
});

__tests__/utils/event.ts

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { IElement } from '@antv/g2/lib/dependents';
1+
import { BBox, ICanvas, IElement } from '@antv/g2/lib/dependents';
22

33
// 触发 Canvas 上元素的鼠标事件
44
export const simulateMouseEvent = (element: IElement, event: string, cfg = {}) => {
@@ -15,3 +15,17 @@ export const simulateMouseEvent = (element: IElement, event: string, cfg = {}) =
1515
});
1616
canvas.get('el').dispatchEvent(mouseEvent);
1717
};
18+
19+
// 触发鼠标事件
20+
export const dispatchEvent = (bbox: BBox, canvas: ICanvas, event: string, cfg = {}) => {
21+
// 设置位置信息为 element 中央
22+
const x = bbox.x + bbox.width / 2;
23+
const y = bbox.y + bbox.height / 2;
24+
const client = canvas.getClientByPoint(x, y);
25+
const mouseEvent = new MouseEvent(event, {
26+
clientX: client.x,
27+
clientY: client.y,
28+
...cfg,
29+
});
30+
canvas.get('el').dispatchEvent(mouseEvent);
31+
};

docs/api/components/annotations.en.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
2-
title: Annotation
3-
order: 4
2+
title: Annotations
3+
order: 6
44
---
55

66
`markdown:docs/styles/component.md`
@@ -45,12 +45,12 @@ annotations: [
4545

4646
#### Configurations(_AnnotationCfg_
4747

48-
> means: _required_
48+
> ❗️ means: _required_
4949
5050
| Properties | Type | Description | Apply to |
5151
| --- | --- | --- | --- |
52-
| type | _string_ | 图形标注类型. |
53-
| position | _object_ | 标注位置. |
52+
| type ❗️| _string_ | 图形标注类型. |
53+
| position ❗️ | _object_ | 标注位置. |
5454
| animate | _boolean_ | 是否进行动画. |
5555
| autoAdjust | _boolean_ | 文本超出绘制区域时,是否自动调节文本方向. |
5656
| start | _array_ | 起始位置. | <tag color="green" text="line"> Line </tag> <tag color="green" text="region"> Region </tag>

docs/api/components/annotations.zh.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
2-
title: 图表标注
3-
order: 4
2+
title: 图表标注 - Annotations
3+
order: 6
44
---
55

66
`markdown:docs/styles/component.md`
@@ -46,12 +46,12 @@ annotations: [
4646

4747
#### 配置项(_AnnotationCfg_
4848

49-
> 代表 _必选_
49+
> ❗️ 代表 _必选_
5050
5151
| **属性** | **类型** | **描述** | **用于** |
5252
| --- | --- | --- | --- |
53-
| type | _string_ | 图形标注类型. |
54-
| position | _object_ | 标注位置. |
53+
| type ❗️ | _string_ | 图形标注类型. |
54+
| position ❗️ | _object_ | 标注位置. |
5555
| animate | _boolean_ | 是否进行动画. |
5656
| autoAdjust | _boolean_ | 文本超出绘制区域时,是否自动调节文本方向. |
5757
| start | _array_ | 起始位置. | <tag color="green" text="line"> Line </tag> <tag color="green" text="region"> Region </tag>

docs/api/components/axis.en.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: Axis
3-
order: 0
3+
order: 1
44
---
55

66
`markdown:docs/styles/component.md`

docs/api/components/axis.zh.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
2-
title: 坐标轴
3-
order: 0
2+
title: 坐标轴 - Axis
3+
order: 1
44
---
55

66
`markdown:docs/styles/component.md`
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
---
2+
title: ConversionTag
3+
order: 8
4+
---
5+
6+
Applicable to base bar charts and base bar charts, the Conversion Rate component allows the user to focus on the rate of change in the data.
7+
8+
#### _ConversionTagCfg_
9+
10+
`markdown:docs/common/conversion-tag.en.md`
11+
12+
#### Events and interactions
13+
14+
<playground path='general/events/demo/conversion-tag-with-link.ts' rid='conversion-tag-events'></playground>
15+
16+
转化标签的图形组成包含 `conversion-tag-arrow``conversion-tag-text`。同一个图表的转化标签组件都放置于一个 group 下,也就是 `conversion-tag-group`,我们可以通过监听 `conversion-tag-group` 事件来进行一些定制交互。
17+
18+
转化标签携带的原始数据信息包含:上一个图形元素(element)和下一个图形元素(nextElement),可通过下列方式获取:
19+
20+
```ts
21+
plot.on('conversion-tag-group:mouseenter', (evt) => {
22+
const { element, nextElement } = evt.target?.get('origin');
23+
// custom by yourself. details of element API, see: https://g2.antv.vision/en/docs/api/general/element
24+
});
25+
```
26+
27+
Example:
28+
29+
<playground path='general/events/demo/conversion-tag-with-link.ts' rid='conversion-tag-events'></playground>
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
---
2+
title: 转化标签 - ConversionTag
3+
order: 8
4+
---
5+
6+
适用于基础柱形图和基础条形图,转化率组件可以让用户关注到数据的变化比例。
7+
8+
#### 配置项
9+
10+
`markdown:docs/common/conversion-tag.zh.md`
11+
12+
#### 事件交互
13+
14+
转化标签的图形组成包含 `conversion-tag-arrow``conversion-tag-text`。同一个图表的转化标签组件都放置于一个 group 下,也就是 `conversion-tag-group`,我们可以通过监听 `conversion-tag-group` 事件来进行一些定制交互。
15+
16+
转化标签携带的原始数据信息包含:上一个图形元素(element)和下一个图形元素(nextElement),可通过下列方式获取:
17+
18+
```ts
19+
plot.on('conversion-tag-group:mouseenter', (evt) => {
20+
const { element, nextElement } = evt.target?.get('origin');
21+
// 自定义业务逻辑,element 的详细 API,见:https://g2.antv.vision/zh/docs/api/general/element
22+
});
23+
```
24+
25+
示例:
26+
27+
<playground path='general/events/demo/conversion-tag-with-link.ts' rid='conversion-tag-events'></playground>

docs/api/components/label.en.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: Label
3-
order: 1
3+
order: 4
44
---
55

66
`markdown:docs/styles/component.md`

docs/api/components/label.zh.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
2-
title: 数据标签
3-
order: 1
2+
title: 数据标签 - Label
3+
order: 4
44
---
55

66
`markdown:docs/styles/component.md`

docs/api/components/legend.en.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: Legend
3-
order: 2
3+
order: 0
44
---
55

66
`markdown:docs/styles/component.md`

docs/api/components/legend.zh.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
2-
title: 图例
3-
order: 2
2+
title: 图例 - Legend
3+
order: 0
44
---
55

66
`markdown:docs/styles/component.md`

docs/api/components/scrollbar.en.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: Scrollbar
3-
order: 7
3+
order: 3
44
---
55

66
`markdown:docs/styles/component.md`

docs/api/components/scrollbar.zh.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
2-
title: 滚动条
3-
order: 7
2+
title: 滚动条 - Scrollbar
3+
order: 3
44
---
55

66
`markdown:docs/styles/component.md`

docs/api/components/slider.en.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: Slider
3-
order: 6
3+
order: 2
44
---
55

66
`markdown:docs/styles/component.md`

docs/api/components/slider.zh.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
2-
title: 缩略轴
3-
order: 6
2+
title: 缩略轴 - Slider
3+
order: 2
44
---
55

66
`markdown:docs/styles/component.md`

docs/api/components/tooltip.en.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: Tooltip
3-
order: 3
3+
order: 5
44
---
55

66
`markdown:docs/styles/component.md`

docs/api/components/tooltip.zh.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
2-
title: Tooltip
3-
order: 3
2+
title: 悬浮提示 - Tooltip
3+
order: 5
44
---
55

66
`markdown:docs/styles/component.md`

docs/api/plots/bar.en.md

+2
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ The ratio of spacing between columns in groups( Range:[0-1] ), only for Grouped
8787

8888
#### Conversion Tag
8989

90+
Applicable to base bar charts and base bar charts, the Conversion Rate component allows the user to focus on the rate of change in the data.
91+
9092
`markdown:docs/common/conversion-tag.en.md`
9193

9294
#### Connected Area

docs/api/plots/bar.zh.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,9 @@ order: 3
8989

9090
`markdown:docs/common/slider.zh.md`
9191

92-
#### 转化率
92+
#### 转化标签
93+
94+
适用于基础柱形图和基础条形图,转化率组件可以让用户关注到数据的变化比例。
9395

9496
`markdown:docs/common/conversion-tag.zh.md`
9597

docs/api/plots/column.en.md

+2
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ The spacing between columns in a grouping [0-1] applies only to grouping columns
9191

9292
#### Conversion Tag
9393

94+
Applicable to base bar charts and base bar charts, the Conversion Rate component allows the user to focus on the rate of change in the data.
95+
9496
`markdown:docs/common/conversion-tag.en.md`
9597

9698
#### Connected Area

docs/api/plots/column.zh.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,9 @@ order: 2
9191

9292
`markdown:docs/common/slider.zh.md`
9393

94-
#### 转化率
94+
#### 转化标签
95+
96+
适用于基础柱形图和基础条形图,转化率组件可以让用户关注到数据的变化比例。
9597

9698
`markdown:docs/common/conversion-tag.zh.md`
9799

0 commit comments

Comments
 (0)