Skip to content

Commit e779cfd

Browse files
authored
feat(gauge): add statistic, demo, docs (#1653)
1 parent 7933709 commit e779cfd

File tree

13 files changed

+248
-1
lines changed

13 files changed

+248
-1
lines changed

__tests__/unit/plots/gauge/index-spec.ts

+11
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ describe('gauge', () => {
3434
count: 3,
3535
},
3636
},
37+
statistic: {
38+
content: {
39+
formatter: ({ percent }) => `分数:${percent * 100}`,
40+
},
41+
},
3742
});
3843

3944
gauge.render();
@@ -79,6 +84,12 @@ describe('gauge', () => {
7984
expect(v2.getCoordinate().radius).toEqual(gauge.options.radius);
8085
expect(v2.getCoordinate().innerRadius).toEqual(gauge.options.innerRadius);
8186

87+
// statistic
88+
const annotations = gauge.chart.getComponents();
89+
expect(annotations.length).toBe(1);
90+
expect(annotations[0].extra.position).toEqual(['50%', '100%']);
91+
expect(annotations[0].extra.content).toBe('分数:75');
92+
8293
// @ts-ignore
8394
expect(v2.options.axes).toEqual(undefined);
8495

examples/gauge/basic/API.en.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
## 配置属性

examples/gauge/basic/API.zh.md

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
## 配置属性
2+
3+
### 图表容器
4+
5+
`markdown:docs/common/chart-options.zh.md`
6+
7+
### 数据映射
8+
9+
#### percent 📌
10+
11+
**必选**, _number_
12+
13+
功能描述: 指标比例
14+
15+
默认配置: 无
16+
17+
#### radius
18+
19+
**可选**, _number_
20+
21+
功能描述: 圆盘的外半径, 0 ~ 1 。
22+
23+
默认配置: `0.95`
24+
25+
#### innerRadius
26+
27+
**可选**, _number_
28+
29+
功能描述: 圆盘的内半径, 0 ~ 1 。
30+
31+
默认配置: `0.9`
32+
33+
#### startAngle
34+
35+
**可选**, _number_
36+
37+
功能描述: 圆盘的起始角度。
38+
39+
默认配置: `(-7 / 6) * Math.PI`
40+
41+
#### endAngle
42+
43+
**可选**, _number_
44+
45+
功能描述: 圆盘的终止角度。
46+
47+
默认配置: `(1 / 6) * Math.PI`
48+
49+
### 图形样式
50+
51+
#### range
52+
53+
**可选**, _object_
54+
55+
功能描述: 仪表盘辅助圆弧的样式。
56+
57+
| 配置项 | 类型 | 描述 |
58+
| ------- | ---------------------- | -------- |
59+
| ticks | number[] | 辅助圆弧显示数字数组 |
60+
| color | string[] | 辅助圆弧的颜色色板,按照色板顺序取值 |
61+
62+
#### indicator
63+
64+
**可选**, _object_
65+
66+
功能描述: 仪表盘指示器样式配置。按照组件分成为:
67+
68+
- `pointer`:指示器中的指针样式配置
69+
- `pin`:指示器中的圆盘样式配置
70+
71+
| 配置项 | 类型 | 描述 |
72+
| --------- | -------- | -------------------- |
73+
| style | object | 组件样式配置 |
74+
75+
#### statistic
76+
77+
**可选**, _object_
78+
79+
功能描述: 指标文本组件 。
80+
81+
默认配置: 无
82+
83+
`markdown:docs/common/statistic.zh.md`
84+
85+
#### axis
86+
87+
**可选**, _object_
88+
89+
功能描述: 指标辅助轴样式 。
90+
91+
默认配置: 无
92+
93+
`markdown:docs/common/axis.zh.md`
94+

examples/gauge/basic/demo/basic.ts

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { Gauge } from '@antv/g2plot';
2+
3+
const gauge = new Gauge('container', {
4+
percent: 0.65,
5+
range: {
6+
color: ['l(0) 0:#5d7cef 1:#e35767'],
7+
},
8+
});
9+
10+
gauge.render();

examples/gauge/basic/demo/complex.ts

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { Gauge } from '@antv/g2plot';
2+
3+
const gauge = new Gauge('container', {
4+
percent: 0.75,
5+
range: {
6+
ticks: [0, 0.2, 0.4, 0.75, 1],
7+
color: ['red', 'yellow', 'green'],
8+
},
9+
indicator: {
10+
pointer: {
11+
style: {
12+
stroke: 'pink',
13+
},
14+
},
15+
pin: {
16+
style: {
17+
stroke: 'blue',
18+
},
19+
},
20+
},
21+
axis: {
22+
label: {
23+
formatter(v: string) {
24+
return Number(v) * 100;
25+
},
26+
},
27+
subTickLine: {
28+
count: 3,
29+
},
30+
},
31+
statistic: {
32+
content: {
33+
formatter: ({ percent }) => `分数:${percent * 100}`,
34+
},
35+
},
36+
});
37+
38+
gauge.render();

examples/gauge/basic/demo/meta.json

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"title": {
3+
"zh": "中文分类",
4+
"en": "Category"
5+
},
6+
"demos": [
7+
{
8+
"filename": "basic.ts",
9+
"title": {
10+
"zh": "仪表盘",
11+
"en": "Gauge chart"
12+
},
13+
"screenshot": "https://gw.alicdn.com/tfs/TB1peOutYr1gK0jSZFDXXb9yVXa-484-468.png"
14+
},
15+
{
16+
"filename": "complex.ts",
17+
"title": {
18+
"zh": "自定义配置的仪表盘",
19+
"en": "Gauge chart - custom style"
20+
},
21+
"screenshot": "https://gw.alicdn.com/tfs/TB1IICyt4D1gK0jSZFyXXciOVXa-517-474.png"
22+
}
23+
]
24+
}

examples/gauge/basic/design.en.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
title: 设计规范
3+
---
4+
5+
设计规范

examples/gauge/basic/design.zh.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
title: 设计规范
3+
---
4+
5+
设计规范

examples/gauge/basic/index.en.md

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
title: 仪表盘
3+
order: 1
4+
---

examples/gauge/basic/index.zh.md

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
title: 仪表盘
3+
order: 1
4+
---

gatsby-config.js

+8
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,14 @@ module.exports = {
112112
en: 'Scatter Charts',
113113
},
114114
},
115+
{
116+
slug: 'gauge',
117+
icon: 'gauge',
118+
title: {
119+
zh: '仪表盘',
120+
en: 'Gauge Charts',
121+
},
122+
},
115123
{
116124
slug: 'histogram',
117125
icon: 'histogram',

src/plots/gauge/adaptor.ts

+31-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { isString, isArray } from '@antv/util';
1+
import { isString, isArray, isFunction } from '@antv/util';
22
import { interaction, animation, theme, scale } from '../../adaptor/common';
33
import { Params } from '../../core/adaptor';
44
import { Data } from '../../types';
@@ -74,6 +74,35 @@ function meta(params: Params<GaugeOptions>): Params<GaugeOptions> {
7474
)(params);
7575
}
7676

77+
/**
78+
* 统计指标文档
79+
* @param params
80+
*/
81+
function statistic(params: Params<GaugeOptions>): Params<GaugeOptions> {
82+
const { chart, options } = params;
83+
const { statistic, percent } = options;
84+
85+
const { title, content } = statistic;
86+
87+
// annotation title 和 content 分别使用一个 text
88+
[title, content].forEach((annotation) => {
89+
if (annotation) {
90+
const { formatter, style, offsetX, offsetY, rotate } = annotation;
91+
chart.annotation().text({
92+
top: true,
93+
position: ['50%', '100%'],
94+
content: isFunction(formatter) ? formatter({ percent }) : `${percent}`,
95+
style: isFunction(style) ? style({ percent }) : style,
96+
offsetX,
97+
offsetY,
98+
rotate,
99+
});
100+
}
101+
});
102+
103+
return params;
104+
}
105+
77106
/**
78107
* other 配置
79108
* @param params
@@ -97,6 +126,7 @@ export function adaptor(params: Params<GaugeOptions>) {
97126
return flow(
98127
geometry,
99128
meta,
129+
statistic,
100130
interaction,
101131
animation,
102132
theme,

src/plots/gauge/index.ts

+13
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,19 @@ export class Gauge extends Plot<GaugeOptions> {
6161
},
6262
},
6363
},
64+
statistic: {
65+
title: false,
66+
content: {
67+
formatter: ({ percent }) => `${(percent * 100).toFixed(2)}%`,
68+
style: {
69+
opacity: 0.75,
70+
fontSize: 30,
71+
textAlign: 'center',
72+
textBaseline: 'bottom',
73+
},
74+
offsetY: -16,
75+
},
76+
},
6477
meta: {
6578
// 两个 view 的 scale 同步到 v 上
6679
[RANGE_VALUE]: {

0 commit comments

Comments
 (0)