Skip to content

Commit b997fff

Browse files
wangyuhuwangxingkang
authored andcommitted
feat: 仪表盘
1 parent b419d8c commit b997fff

File tree

5 files changed

+119
-0
lines changed

5 files changed

+119
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { colorBlue2, colorGrey04, colorGrey10 } from '../../config';
2+
import type { AxisConfig, ScaleConfig, StyleConfig } from './types';
3+
4+
export const DEFAULT_STYLE_CONFIG: StyleConfig = {
5+
textContent: (target: number, total: number) => {
6+
return `${Math.round((target / total) * 100)}%`;
7+
},
8+
textFill: colorGrey10,
9+
textFontSize: '24px',
10+
textLineHeight: '24px',
11+
textFontFamily: 'DIN Alternate',
12+
textFontWeight: 'bold',
13+
textY: '70%',
14+
};
15+
16+
export const DEFAULT_AXIS_CONFIG: AxisConfig = {
17+
x: {
18+
title: false,
19+
},
20+
y: {
21+
tick: false,
22+
label: false,
23+
},
24+
};
25+
26+
export const DEFAULT_SCALE_CONFIG: ScaleConfig = {
27+
color: {
28+
range: [colorBlue2, colorGrey04],
29+
},
30+
};
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import React, { forwardRef } from 'react';
2+
import { Gauge as AntGauge } from '@ant-design/plots';
3+
import type { Chart } from '@ant-design/plots/es/interface';
4+
import { Renderer as SVGRenderer } from '@antv/g-svg';
5+
import type { GaugeConfig as AntGaugeConfig } from '@ant-design/plots';
6+
import { getItemConfig } from '../../helpers/utils';
7+
import { DEFAULT_INSET_LEFT, DEFAULT_INSET_RIGHT, colorBlue2, colorGrey04 } from '../../config';
8+
import { DEFAULT_AXIS_CONFIG, DEFAULT_SCALE_CONFIG, DEFAULT_STYLE_CONFIG } from './config';
9+
import type { AxisConfig, ScaleConfig, StyleConfig } from './types';
10+
11+
export interface GaugeConfig extends Omit<AntGaugeConfig, 'axis' | 'scale'> {
12+
axis?: AntGaugeConfig['axis'] | boolean;
13+
scale?: AntGaugeConfig['scale'] | boolean;
14+
startAngle?: number;
15+
endAngle?: number;
16+
}
17+
18+
export const Gauge = forwardRef<Chart, GaugeConfig>(
19+
(props, ref) => {
20+
const {
21+
data,
22+
insetLeft = DEFAULT_INSET_LEFT,
23+
insetRight = DEFAULT_INSET_RIGHT,
24+
startAngle = -1.25 * Math.PI,
25+
endAngle = 0.25 * Math.PI,
26+
axis = true,
27+
scale = true,
28+
...rest
29+
} = props;
30+
31+
// 需要给data的name赋值为空字符串,否则会显示默认值 scale
32+
const dataConfig = Object.assign({}, data, { name: '' });
33+
const axisConfig = getItemConfig<AxisConfig>(axis, DEFAULT_AXIS_CONFIG);
34+
const scaleConfig = getItemConfig<ScaleConfig>(scale, DEFAULT_SCALE_CONFIG);
35+
const styleConfig = getItemConfig<StyleConfig>(scale, DEFAULT_STYLE_CONFIG);
36+
37+
return (
38+
<AntGauge
39+
insetLeft={insetLeft}
40+
insetRight={insetRight}
41+
startAngle={startAngle}
42+
endAngle={endAngle}
43+
data={dataConfig}
44+
axis={axisConfig}
45+
scale={scaleConfig}
46+
style={styleConfig}
47+
{...rest}
48+
ref={ref}
49+
renderer={new SVGRenderer()}
50+
/>
51+
);
52+
},
53+
);
54+
55+
export default Gauge;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import type { GaugeConfig } from '@ant-design/plots';
2+
3+
export type StyleConfig = NonNullable<GaugeConfig['style']>;
4+
export type AxisConfig = NonNullable<GaugeConfig['axis']>;
5+
export type ScaleConfig = NonNullable<GaugeConfig['scale']>;

@sensoro-design/plots/src/components/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,6 @@ export type { RoseConfig } from './Rose';
2727

2828
export { Funnel } from './Funnel';
2929
export type { FunnelConfig } from './Funnel';
30+
31+
export { Gauge } from './Gauge';
32+
export type { GaugeConfig } from './Gauge';
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import React from 'react';
2+
import { Gauge, type GaugeConfig } from '@sensoro-design/plots';
3+
4+
const meta = {
5+
title: 'Charts/Gauge',
6+
};
7+
8+
export default meta;
9+
10+
export function Basic() {
11+
const config: GaugeConfig = {
12+
title: '仪表盘',
13+
// data 格式为 { target: number, total: number } | { percent: number }
14+
// percent 优先级更高,会覆盖 target 和 total
15+
data: {
16+
target: 120,
17+
total: 400,
18+
percent: 0.6,
19+
},
20+
width: 720,
21+
};
22+
23+
return (
24+
<Gauge {...config} />
25+
);
26+
}

0 commit comments

Comments
 (0)