| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- import React, { useMemo } from 'react';
- import { BaseChart, BaseChartProps } from './BaseChart';
- import type { TouchEvent, ChartsConfig, LegendConfig } from '../lib/charts/index';
- /**
- * 雷达图组件的 Props 接口
- */
- export interface RadarChartProps extends Omit<BaseChartProps, 'type'> {
- /** 是否显示图例 */
- legend?: boolean;
- /** 字体大小 */
- fontSize?: number;
- /** 背景颜色 */
- background?: string;
- /** 是否开启动画 */
- animation?: boolean;
- /** 是否显示数据标签 */
- dataLabel?: boolean;
- /** 雷达图轴线颜色 */
- axisColor?: string;
- /** 雷达图区域透明度 */
- areaOpacity?: number;
- /** 额外雷达图配置 */
- extra?: ChartsConfig['extra'];
- /** tooltip 格式化函数 */
- tooltipFormatter?: (item: any, category: string) => string;
- }
- /**
- * RadarChart 雷达图组件
- *
- * 用于显示多维数据对比的雷达图
- */
- export const RadarChart: React.FC<RadarChartProps> = (props) => {
- const {
- legend = true,
- fontSize = 11,
- background = '#FFFFFF',
- animation = true,
- dataLabel = false,
- axisColor = '#cccccc',
- areaOpacity = 0.5,
- extra = {},
- tooltipFormatter,
- categories = [],
- series = [],
- config = {},
- ...baseProps
- } = props;
- const chartRef = React.useRef<any>(null);
- /**
- * 默认配置
- */
- const defaultConfig = useMemo(() => {
- const legendConfig: LegendConfig = legend ? { show: true } : { show: false };
- return {
- legend: legendConfig,
- fontSize,
- background,
- animation,
- dataLabel,
- extra: {
- radar: {
- gridColor: axisColor,
- axisColor,
- opacity: areaOpacity,
- max: 100
- },
- ...extra
- }
- };
- }, [legend, fontSize, background, animation, dataLabel, axisColor, areaOpacity, extra]);
- /**
- * tooltip 事件处理
- */
- const handleTouchStart = (e: TouchEvent) => {
- if (chartRef.current) {
- if (tooltipFormatter) {
- chartRef.current.showToolTip(e, {
- formatter: tooltipFormatter
- });
- } else {
- chartRef.current.showToolTip(e, {
- formatter: (item: any, category: string) => {
- return category + ' ' + item.name + ':' + item.data;
- }
- });
- }
- }
- baseProps.onTouchStart?.(e);
- };
- return (
- <BaseChart
- {...baseProps}
- categories={categories}
- series={series}
- type="radar"
- config={{ ...defaultConfig, ...config }}
- onTouchStart={handleTouchStart}
- />
- );
- };
- export default RadarChart;
|