|
| 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; |
0 commit comments