Skip to content

Commit 0125828

Browse files
authored
docs(gauge): 增加仪表盘自定义 demo (#3092)
1 parent 6663aed commit 0125828

File tree

2 files changed

+158
-0
lines changed

2 files changed

+158
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
import { Gauge, G2 } from '@antv/g2plot';
2+
3+
const { Util, registerShape } = G2;
4+
// 自定义 Shape 部分
5+
registerShape('point', 'custom-gauge-indicator', {
6+
draw(cfg, container) {
7+
// 使用 customInfo 传递参数
8+
const { indicator, defaultColor } = cfg.customInfo;
9+
const { pointer, pin } = indicator;
10+
11+
const group = container.addGroup();
12+
// 获取极坐标系下画布中心点
13+
const center = this.parsePoint({ x: 0, y: 0 });
14+
15+
if (pin) {
16+
const pinStyle = pin.style || {};
17+
const { lineWidth = 2, fill = defaultColor } = pinStyle;
18+
const r = 6;
19+
group.addShape('circle', {
20+
name: 'pin-outer',
21+
attrs: {
22+
x: center.x,
23+
y: center.y,
24+
...pin.style,
25+
fill,
26+
r: r * 1.5,
27+
lineWidth,
28+
},
29+
});
30+
}
31+
// 绘制指针
32+
if (pointer) {
33+
const { startAngle, endAngle } = Util.getAngle(cfg, this.coordinate);
34+
const radius = this.coordinate.getRadius();
35+
const midAngle = (startAngle + endAngle) / 2;
36+
const { x: x1, y: y1 } = Util.polarToCartesian(center.x, center.y, radius / 15, midAngle + 1 / Math.PI);
37+
const { x: x2, y: y2 } = Util.polarToCartesian(center.x, center.y, radius / 15, midAngle - 1 / Math.PI);
38+
const { x, y } = Util.polarToCartesian(center.x, center.y, radius * 0.65, midAngle);
39+
const { x: x0, y: y0 } = Util.polarToCartesian(center.x, center.y, radius * 0.1, midAngle + Math.PI);
40+
const sa = Math.PI / 2 + midAngle;
41+
const r1 = 5.5;
42+
const p1 = {
43+
x: center.x + Math.cos(sa) * r1,
44+
y: center.y + Math.sin(sa) * r1,
45+
};
46+
const p2 = {
47+
x: center.x - Math.cos(sa) * r1,
48+
y: center.y - Math.sin(sa) * r1,
49+
};
50+
const r2 = r1 / 4;
51+
const p11 = {
52+
x: x + Math.cos(sa) * r2,
53+
y: y + Math.sin(sa) * r2,
54+
};
55+
const p21 = {
56+
x: x - Math.cos(sa) * r2,
57+
y: y - Math.sin(sa) * r2,
58+
};
59+
60+
const path = [
61+
['M', p21.x, p21.y],
62+
// 参数信息: cx, cy, .., .., .., endPointX, endPointY
63+
['A', r2, r2, 0, 0, 1, p11.x, p11.y],
64+
['L', p1.x, p1.y],
65+
['A', r1, r1, 0, 0, 1, p2.x, p2.y],
66+
['Z'],
67+
];
68+
// pointer
69+
group.addShape('path', {
70+
name: 'pointer',
71+
attrs: {
72+
path,
73+
fill: defaultColor,
74+
lineCap: 'round',
75+
...pointer.style,
76+
},
77+
});
78+
// pointer
79+
group.addShape('circle', {
80+
name: 'pointer-center',
81+
attrs: {
82+
x: center.x,
83+
y: center.y,
84+
r: 2,
85+
fill: '#fff',
86+
},
87+
});
88+
}
89+
90+
return group;
91+
},
92+
});
93+
94+
const gauge = new Gauge('container', {
95+
percent: 0.75,
96+
range: {
97+
ticks: [0, 1 / 4, 1 / 2, 3 / 4, 1],
98+
color: ['#F4664A', '#FAAD14', '#30BF78', '#000'],
99+
},
100+
startAngle: -Math.PI,
101+
endAngle: 0,
102+
gaugeStyle: {
103+
// 因为圆角是借助重叠去实现的,所以建议填充色不要有透明度
104+
fillOpacity: 1,
105+
lineCap: 'round',
106+
stroke: '#fff',
107+
lineWidth: 4,
108+
},
109+
indicator: {
110+
shape: 'custom-gauge-indicator',
111+
pointer: {
112+
style: {
113+
stroke: '#30BF78',
114+
lineWidth: 1,
115+
fill: '#30BF78',
116+
},
117+
},
118+
pin: {
119+
style: {
120+
lineWidth: 2,
121+
stroke: '#D0D0D0',
122+
fill: '#D0D0D0',
123+
},
124+
},
125+
},
126+
// 为底部预留 annotation 的位置
127+
appendPadding: [0, 0, 80, 0],
128+
statistic: {
129+
content: {
130+
offsetY: 70,
131+
customHtml: () => {
132+
// 提示信息,可以考虑不在这里展示,用其它方案代替。
133+
// 否则,方案1: 考虑这里返回一个 div,然后用 createPortal 搞上去
134+
// 方案2: 监听事件,动态增加一个 tooltip 信息提示
135+
return `
136+
<div><span>安全</span>
137+
<span className="info-icon" data-info="安全指标" style="line-height:26px;"><img width="16" src="https://gw.alipayobjects.com/zos/antfincdn/wKEJBoEf4w/question-circle-o.png"></span>
138+
</div>
139+
`;
140+
},
141+
style: {
142+
fontSize: '24px',
143+
lineHeight: '26px',
144+
},
145+
},
146+
},
147+
});
148+
149+
gauge.render();

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

+9
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,15 @@
7676
},
7777
"screenshot": "https://gw.alipayobjects.com/zos/antfincdn/%26tVjT99wV1/6dae1f99-b160-4c6d-9769-b0972d02fd19.png"
7878
},
79+
{
80+
"filename": "custom-indicator3.ts",
81+
"title": {
82+
"zh": "自定义仪表盘指示器",
83+
"en": "Custom gauge indicator"
84+
},
85+
"new": true,
86+
"screenshot": "https://gw.alipayobjects.com/zos/antfincdn/mgM7BC01Fk/9505de5d-484a-4577-b433-e40cc1bd4b0d.png"
87+
},
7988
{
8089
"filename": "simple-indicator.ts",
8190
"title": {

0 commit comments

Comments
 (0)