Skip to content

Commit 29b7502

Browse files
committed
docs(gauge): 丰富自定义仪表盘指示器 demo
1 parent 8a21cf9 commit 29b7502

File tree

2 files changed

+103
-0
lines changed

2 files changed

+103
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import { Gauge, G2 } from '@antv/g2plot';
2+
3+
const { registerShape, Util } = G2;
4+
5+
// 自定义 Shape 部分
6+
registerShape('point', 'custom-gauge-indicator2', {
7+
draw(cfg, container) {
8+
// 使用 customInfo 传递参数
9+
const { indicator, defaultColor } = cfg.customInfo;
10+
const { pointer, pin } = indicator;
11+
12+
const group = container.addGroup();
13+
// 获取极坐标系下画布中心点
14+
const center = this.parsePoint({ x: 0, y: 0 });
15+
// 绘制指针
16+
if (pointer) {
17+
const { startAngle, endAngle } = Util.getAngle(cfg, this.coordinate);
18+
const radius = this.coordinate.getRadius();
19+
const midAngle = (startAngle + endAngle) / 2;
20+
const { x: x1, y: y1 } = Util.polarToCartesian(center.x, center.y, radius / 15, midAngle + 1 / Math.PI);
21+
const { x: x2, y: y2 } = Util.polarToCartesian(center.x, center.y, radius / 15, midAngle - 1 / Math.PI);
22+
const { x, y } = Util.polarToCartesian(center.x, center.y, radius * 0.65, midAngle);
23+
const { x: x0, y: y0 } = Util.polarToCartesian(center.x, center.y, radius * 0.1, midAngle + Math.PI);
24+
25+
const path = [['M', x0, y0], ['L', x1, y1], ['L', x, y], ['L', x2, y2], ['Z']];
26+
// pointer
27+
group.addShape('path', {
28+
name: 'pointer',
29+
attrs: {
30+
path,
31+
fill: defaultColor,
32+
...pointer.style,
33+
},
34+
});
35+
}
36+
37+
if (pin) {
38+
const pinStyle = pin.style || {};
39+
const { lineWidth = 2, fill = defaultColor, stroke = defaultColor } = pinStyle;
40+
const r = 6;
41+
group.addShape('circle', {
42+
name: 'pin-outer',
43+
attrs: {
44+
x: center.x,
45+
y: center.y,
46+
...pin.style,
47+
fill: 'transparent',
48+
r: r * 1.5,
49+
lineWidth,
50+
stroke: stroke,
51+
},
52+
});
53+
54+
group.addShape('circle', {
55+
name: 'pin-inner',
56+
attrs: {
57+
x: center.x,
58+
y: center.y,
59+
r,
60+
stroke: 'transparent',
61+
fill,
62+
},
63+
});
64+
}
65+
66+
return group;
67+
},
68+
});
69+
70+
const gauge = new Gauge('container', {
71+
percent: 0.78,
72+
range: {
73+
color: '#30BF78',
74+
},
75+
indicator: {
76+
shape: 'custom-gauge-indicator2',
77+
pointer: {
78+
style: {
79+
stroke: '#D0D0D0',
80+
lineWidth: 1,
81+
fill: '#D0D0D0',
82+
},
83+
},
84+
pin: {
85+
style: {
86+
lineWidth: 2,
87+
stroke: '#D0D0D0',
88+
fill: '#D0D0D0',
89+
},
90+
},
91+
},
92+
});
93+
94+
gauge.render();

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

+9
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,15 @@
6868
},
6969
"new": true,
7070
"screenshot": "https://gw.alipayobjects.com/zos/antfincdn/DUTCv9MXHO/71a294f0-99ed-4c41-9592-83add57871bc.png"
71+
},
72+
{
73+
"filename": "custom-indicator2.ts",
74+
"title": {
75+
"zh": "自定义仪表盘指示器",
76+
"en": "Custom gauge indicator"
77+
},
78+
"new": true,
79+
"screenshot": "https://gw.alipayobjects.com/zos/antfincdn/%26tVjT99wV1/6dae1f99-b160-4c6d-9769-b0972d02fd19.png"
7180
}
7281
]
7382
}

0 commit comments

Comments
 (0)