RadarChart.tsx 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import React, { useMemo } from 'react';
  2. import { BaseChart, BaseChartProps } from './BaseChart';
  3. import type { TouchEvent, ChartsConfig, LegendConfig } from '../lib/charts/index';
  4. /**
  5. * 雷达图组件的 Props 接口
  6. */
  7. export interface RadarChartProps extends Omit<BaseChartProps, 'type'> {
  8. /** 是否显示图例 */
  9. legend?: boolean;
  10. /** 字体大小 */
  11. fontSize?: number;
  12. /** 背景颜色 */
  13. background?: string;
  14. /** 是否开启动画 */
  15. animation?: boolean;
  16. /** 是否显示数据标签 */
  17. dataLabel?: boolean;
  18. /** 雷达图轴线颜色 */
  19. axisColor?: string;
  20. /** 雷达图区域透明度 */
  21. areaOpacity?: number;
  22. /** 额外雷达图配置 */
  23. extra?: ChartsConfig['extra'];
  24. /** tooltip 格式化函数 */
  25. tooltipFormatter?: (item: any, category: string) => string;
  26. }
  27. /**
  28. * RadarChart 雷达图组件
  29. *
  30. * 用于显示多维数据对比的雷达图
  31. */
  32. export const RadarChart: React.FC<RadarChartProps> = (props) => {
  33. const {
  34. legend = true,
  35. fontSize = 11,
  36. background = '#FFFFFF',
  37. animation = true,
  38. dataLabel = false,
  39. axisColor = '#cccccc',
  40. areaOpacity = 0.5,
  41. extra = {},
  42. tooltipFormatter,
  43. categories = [],
  44. series = [],
  45. config = {},
  46. ...baseProps
  47. } = props;
  48. const chartRef = React.useRef<any>(null);
  49. /**
  50. * 默认配置
  51. */
  52. const defaultConfig = useMemo(() => {
  53. const legendConfig: LegendConfig = legend ? { show: true } : { show: false };
  54. return {
  55. legend: legendConfig,
  56. fontSize,
  57. background,
  58. animation,
  59. dataLabel,
  60. extra: {
  61. radar: {
  62. gridColor: axisColor,
  63. axisColor,
  64. opacity: areaOpacity,
  65. max: 100
  66. },
  67. ...extra
  68. }
  69. };
  70. }, [legend, fontSize, background, animation, dataLabel, axisColor, areaOpacity, extra]);
  71. /**
  72. * tooltip 事件处理
  73. */
  74. const handleTouchStart = (e: TouchEvent) => {
  75. if (chartRef.current) {
  76. if (tooltipFormatter) {
  77. chartRef.current.showToolTip(e, {
  78. formatter: tooltipFormatter
  79. });
  80. } else {
  81. chartRef.current.showToolTip(e, {
  82. formatter: (item: any, category: string) => {
  83. return category + ' ' + item.name + ':' + item.data;
  84. }
  85. });
  86. }
  87. }
  88. baseProps.onTouchStart?.(e);
  89. };
  90. return (
  91. <BaseChart
  92. {...baseProps}
  93. categories={categories}
  94. series={series}
  95. type="radar"
  96. config={{ ...defaultConfig, ...config }}
  97. onTouchStart={handleTouchStart}
  98. />
  99. );
  100. };
  101. export default RadarChart;