Skip to content

Commit 61f7ee4

Browse files
visikyai-qing-hai
andauthored
feat(gauge): 仪表盘增加 tooltip 配置 (#3149)
* feat(gauge): 仪表盘增加 tooltip 配置 * feat(gauge): 仪表盘添加tooltip 文档 单测 和demo Co-authored-by: ai-qing-hai <[email protected]>
1 parent e3fbced commit 61f7ee4

File tree

7 files changed

+182
-3
lines changed

7 files changed

+182
-3
lines changed
+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import { Gauge } from '../../../../src';
2+
import { createDiv } from '../../../utils/dom';
3+
4+
describe('gauge', () => {
5+
it('default tooltip', () => {
6+
const gauge = new Gauge(createDiv(), {
7+
width: 400,
8+
height: 300,
9+
percent: 0.75,
10+
});
11+
12+
gauge.render();
13+
// @ts-ignore
14+
expect(gauge.chart.options.tooltip).toBe(false);
15+
expect(gauge.chart.getComponents().find((co) => co.type === 'tooltip')).toBe(undefined);
16+
17+
gauge.update({
18+
tooltip: {},
19+
});
20+
// @ts-ignore
21+
expect(gauge.chart.options.tooltip).toMatchObject({
22+
showMarkers: false,
23+
showTitle: false,
24+
containerTpl: '<div class="g2-tooltip"><div class="g2-tooltip-list"></div></div>',
25+
domStyles: {
26+
'g2-tooltip': {
27+
padding: '4px 8px',
28+
fontSize: '10px',
29+
},
30+
},
31+
});
32+
33+
gauge.destroy();
34+
});
35+
36+
it('custom tooltip', () => {
37+
const customContent = (x, data) => {
38+
return `${(Number(data?.[0]?.value || 0) * 100).toFixed(2)}%`;
39+
};
40+
41+
const gauge = new Gauge(createDiv(), {
42+
width: 400,
43+
height: 300,
44+
percent: 0.75,
45+
tooltip: {
46+
containerTpl: '<div class="g2-tooltip"><div class="g2-tooltip-list">2131231</div></div>',
47+
domStyles: {
48+
'g2-tooltip': {
49+
padding: '40px',
50+
fontSize: '40px',
51+
},
52+
},
53+
customContent,
54+
},
55+
});
56+
57+
gauge.render();
58+
59+
// @ts-ignore
60+
expect(gauge.chart.options.tooltip).toMatchObject({
61+
showMarkers: false,
62+
showTitle: false,
63+
containerTpl: '<div class="g2-tooltip"><div class="g2-tooltip-list">2131231</div></div>',
64+
domStyles: {
65+
'g2-tooltip': {
66+
padding: '40px',
67+
fontSize: '40px',
68+
},
69+
},
70+
customContent,
71+
});
72+
73+
gauge.destroy();
74+
});
75+
});

docs/api/plots/gauge.en.md

+4
Original file line numberDiff line numberDiff line change
@@ -117,3 +117,7 @@ They all have the following configuration items:
117117
Metric central text component.
118118

119119
`markdown:docs/common/statistic.en.md`
120+
121+
#### tooltip
122+
123+
`markdown:docs/common/tooltip.eh.md`

docs/api/plots/gauge.zh.md

+3
Original file line numberDiff line numberDiff line change
@@ -119,3 +119,6 @@ order: 5
119119

120120
`markdown:docs/common/statistic.zh.md`
121121

122+
#### tooltip
123+
124+
`markdown:docs/common/tooltip.zh.md`

examples/progress-plots/gauge/demo/meta.json

+8
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,14 @@
9292
"en": "Custom simple gauge indicator"
9393
},
9494
"screenshot": "https://gw.alipayobjects.com/zos/antfincdn/2RRg9bSnn2/380b6b20-aed3-44e2-9afe-5532b1f9daea.png"
95+
},
96+
{
97+
"filename": "tooltip-gauge.ts",
98+
"title": {
99+
"zh": "仪表盘 自定义 tooltip",
100+
"en": "Gauge Customize React Tooltip"
101+
},
102+
"screenshot": "https://gw.alipayobjects.com/zos/antfincdn/xDdGhZK84X/9746c5d8-4341-4c04-9528-87bcab55eedb.png"
95103
}
96104
]
97105
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { Gauge } from '@antv/g2plot';
2+
3+
const gauge = new Gauge('container', {
4+
tooltip: {
5+
containerTpl: '<div class="g2-tooltip"><div class="g2-tooltip-list"></div></div>',
6+
domStyles: {
7+
'g2-tooltip': {
8+
padding: '4px 8px',
9+
fontSize: '10px',
10+
},
11+
},
12+
customContent: (x, data) => {
13+
return `${(Number(data?.[0]?.value || 0) * 100).toFixed(2)}%`;
14+
},
15+
},
16+
percent: 0.75,
17+
range: {
18+
color: '#30BF78',
19+
},
20+
indicator: {
21+
pointer: {
22+
style: {
23+
stroke: '#D0D0D0',
24+
},
25+
},
26+
pin: {
27+
style: {
28+
stroke: '#D0D0D0',
29+
},
30+
},
31+
},
32+
axis: {
33+
label: {
34+
formatter(v) {
35+
return Number(v) * 100;
36+
},
37+
},
38+
subTickLine: {
39+
count: 3,
40+
},
41+
},
42+
statistic: {
43+
content: {
44+
formatter: ({ percent }) => `Rate: ${(percent * 100).toFixed(0)}%`,
45+
style: {
46+
color: 'rgba(0,0,0,0.65)',
47+
fontSize: 48,
48+
},
49+
},
50+
},
51+
});
52+
53+
gauge.render();

src/plots/gauge/adaptor.ts

+38-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Geometry } from '@antv/g2';
2-
import { isString } from '@antv/util';
2+
import { get, isString } from '@antv/util';
33
import { interaction, animation, theme, scale, annotation } from '../../adaptor/common';
44
import { interval } from '../../adaptor/geometries';
55
import { AXIS_META_CONFIG_KEYS } from '../../constant';
@@ -149,6 +149,42 @@ function statistic(params: Params<GaugeOptions>, updated?: boolean): Params<Gaug
149149
return params;
150150
}
151151

152+
/**
153+
* tooltip 配置
154+
*/
155+
function tooltip(params: Params<GaugeOptions>): Params<GaugeOptions> {
156+
const { chart, options } = params;
157+
const { tooltip } = options;
158+
159+
if (tooltip) {
160+
chart.tooltip(
161+
deepAssign(
162+
{
163+
showTitle: false,
164+
showMarkers: false,
165+
containerTpl: '<div class="g2-tooltip"><div class="g2-tooltip-list"></div></div>',
166+
domStyles: {
167+
'g2-tooltip': {
168+
padding: '4px 8px',
169+
fontSize: '10px',
170+
},
171+
},
172+
customContent: (x: string, data: any[]) => {
173+
const percent = get(data, [0, 'data', PERCENT], 0);
174+
return `${(percent * 100).toFixed(2)}%`;
175+
},
176+
},
177+
tooltip
178+
)
179+
);
180+
} else {
181+
// 默认,不展示 tooltip
182+
chart.tooltip(false);
183+
}
184+
185+
return params;
186+
}
187+
152188
/**
153189
* other 配置
154190
* @param params
@@ -157,7 +193,6 @@ function other(params: Params<GaugeOptions>): Params<GaugeOptions> {
157193
const { chart } = params;
158194

159195
chart.legend(false);
160-
chart.tooltip(false);
161196

162197
return params;
163198
}
@@ -180,6 +215,7 @@ export function adaptor(params: Params<GaugeOptions>) {
180215
animation,
181216
geometry,
182217
meta,
218+
tooltip,
183219
statistic,
184220
interaction,
185221
annotation(),

src/plots/gauge/types.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export type GaugeRangeData = {
5454
* @title 仪表盘配置类型定义
5555
*/
5656
export interface GaugeOptions
57-
extends Omit<Options, 'data' | 'tooltip' | 'legend' | 'xAxis' | 'yAxis' | 'xField' | 'yField' | 'color'> {
57+
extends Omit<Options, 'data' | 'legend' | 'xAxis' | 'yAxis' | 'xField' | 'yField' | 'color'> {
5858
/**
5959
* @title 指标的比例
6060
* @description 范围0 ~ 1

0 commit comments

Comments
 (0)