Skip to content

Commit 3c54049

Browse files
committed
chore: 增加Gauge文档
1 parent 40eda4d commit 3c54049

File tree

1 file changed

+216
-0
lines changed

1 file changed

+216
-0
lines changed

site/docs/api/chart/gauge.zh.md

Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
---
2+
title: Gauge 仪表盘
3+
type: 组件
4+
order: 5
5+
---
6+
7+
# Gauge 仪表盘
8+
9+
仪表盘组件用于显示进度或完成度的可视化组件,常用于展示关键绩效指标(KPI)。
10+
11+
## 何时使用
12+
13+
- 需要显示单个指标的完成进度
14+
- 展示目标值与实际值的对比
15+
- 显示百分比或比例数据
16+
- 仪表盘样式的数据展示
17+
18+
## 代码演示
19+
20+
### 基础仪表盘
21+
22+
<code src="../../../examples/other/area/demo/gauge.jsx"></code>
23+
24+
### 带颜色区间的仪表盘
25+
26+
<code src="../../../examples/other/area/demo/gauge-color.jsx"></code>
27+
28+
### 自定义文本的仪表盘
29+
30+
<code src="../../../examples/other/area/demo/gauge-text.jsx"></code>
31+
32+
## API
33+
34+
### Gauge
35+
36+
| 属性名 | 类型 | 默认值 | 描述 |
37+
| ---------- | ------------------------ | ------------------ | ---------------- |
38+
| percent | number | 0 | 进度值,范围 0-1 |
39+
| startAngle | number | Math.PI | 起始角度(弧度) |
40+
| endAngle | number | Math.PI \* 2 | 结束角度(弧度) |
41+
| center | { x: number, y: number } | { x: 150, y: 150 } | 仪表盘中心点坐标 |
42+
| r | number \| string | 100 | 仪表盘半径 |
43+
| r0 | number \| string | 0 | 内圆半径 |
44+
| tickCount | number | 5 | 刻度数量 |
45+
| tickOffset | number \| string | -20px | 刻度偏移量 |
46+
| tickLength | number \| string | 10px | 刻度长度 |
47+
48+
## 使用示例
49+
50+
### 基础使用
51+
52+
```jsx
53+
import { jsx, Canvas, Gauge } from '@antv/f2';
54+
55+
const context = document.getElementById('container').getContext('2d');
56+
57+
const { props } = (
58+
<Canvas context={context} pixelRatio={window.devicePixelRatio}>
59+
<Gauge
60+
center={{ x: 150, y: 150 }}
61+
startAngle={Math.PI}
62+
endAngle={Math.PI * 2}
63+
percent={0.75}
64+
r="100px"
65+
/>
66+
</Canvas>
67+
);
68+
69+
const chart = new Canvas(props);
70+
chart.render();
71+
```
72+
73+
### 带颜色区间的仪表盘
74+
75+
```jsx
76+
import { jsx, Canvas, Gauge } from '@antv/f2';
77+
78+
const context = document.getElementById('container').getContext('2d');
79+
80+
const actualValue = 85;
81+
const targetValue = 100;
82+
const percent = actualValue / targetValue;
83+
84+
// 根据值获取颜色
85+
const getColor = (value) => {
86+
if (value < 60) return '#ff4d4f'; // 红色
87+
if (value < 80) return '#faad14'; // 黄色
88+
return '#52c41a'; // 绿色
89+
};
90+
91+
const { props } = (
92+
<Canvas context={context} pixelRatio={window.devicePixelRatio}>
93+
<group>
94+
<Gauge
95+
center={{ x: 150, y: 150 }}
96+
startAngle={Math.PI}
97+
endAngle={Math.PI * 2}
98+
percent={percent}
99+
r="100px"
100+
tickCount={10}
101+
tickOffset="-20px"
102+
tickLength="10px"
103+
/>
104+
105+
{/* 添加文本显示 */}
106+
<text
107+
attrs={{
108+
x: 150,
109+
y: 130,
110+
text: `${actualValue}`,
111+
fontSize: 36,
112+
textAlign: 'center',
113+
textBaseline: 'middle',
114+
fill: getColor(actualValue),
115+
}}
116+
/>
117+
<text
118+
attrs={{
119+
x: 150,
120+
y: 160,
121+
text: '实际值',
122+
fontSize: 14,
123+
textAlign: 'center',
124+
textBaseline: 'middle',
125+
fill: '#666',
126+
}}
127+
/>
128+
<text
129+
attrs={{
130+
x: 150,
131+
y: 180,
132+
text: `目标值: ${targetValue}`,
133+
fontSize: 12,
134+
textAlign: 'center',
135+
textBaseline: 'middle',
136+
fill: '#999',
137+
}}
138+
/>
139+
</group>
140+
</Canvas>
141+
);
142+
143+
const chart = new Canvas(props);
144+
chart.render();
145+
```
146+
147+
### 自定义样式
148+
149+
```jsx
150+
import { jsx, Canvas, Gauge } from '@antv/f2';
151+
152+
const context = document.getElementById('container').getContext('2d');
153+
154+
const { props } = (
155+
<Canvas context={context} pixelRatio={window.devicePixelRatio}>
156+
<Gauge
157+
center={{ x: 200, y: 200 }}
158+
startAngle={Math.PI * 0.75}
159+
endAngle={Math.PI * 2.25}
160+
percent={0.85}
161+
r="150px"
162+
tickCount={8}
163+
tickOffset="-30px"
164+
tickLength="15px"
165+
/>
166+
</Canvas>
167+
);
168+
169+
const chart = new Canvas(props);
170+
chart.render();
171+
```
172+
173+
## 常见问题
174+
175+
### 如何设置颜色区间?
176+
177+
Gauge 组件本身不直接支持颜色区间设置,但你可以通过以下方式实现:
178+
179+
1. 使用自定义渲染覆盖默认颜色
180+
2. 在文本或标签中显示对应的颜色状态
181+
3. 使用多个 Gauge 组件叠加实现
182+
183+
### 如何显示百分比?
184+
185+
可以通过添加文本组件来显示百分比:
186+
187+
```jsx
188+
<text
189+
attrs={{
190+
x: 150,
191+
y: 150,
192+
text: `${Math.round(percent * 100)}%`,
193+
fontSize: 24,
194+
textAlign: 'center',
195+
textBaseline: 'middle',
196+
fill: '#333',
197+
}}
198+
/>
199+
```
200+
201+
### 如何调整仪表盘大小?
202+
203+
通过调整`r`属性来改变仪表盘半径,同时需要相应调整`center`坐标:
204+
205+
```jsx
206+
<Gauge
207+
center={{ x: 100, y: 100 }}
208+
r="80px"
209+
// 其他属性...
210+
/>
211+
```
212+
213+
## 相关组件
214+
215+
- [Progress](/api/chart/progress) - 进度条组件
216+
- [Ring](/api/chart/ring) - 环形图组件

0 commit comments

Comments
 (0)