React 19 通用 ECharts 组件

好,我给你做一个 React 19 通用 ECharts 组件

支持饼图 / 折线图 / 柱状图,colors 可选,不传也能显示,防止重复渲染。


1. src/services/echarts.ts

注册所有需要的图表类型和组件(不用再分开写折线/饼图/柱状图)

复制代码
// src/services/echarts.ts
import * as echarts from 'echarts/core';
import {
  TitleComponent,
  TooltipComponent,
  GridComponent,
  LegendComponent,
  ToolboxComponent
} from 'echarts/components';
import { LineChart, PieChart, BarChart } from 'echarts/charts';
import { CanvasRenderer } from 'echarts/renderers';

// 按需注册组件
echarts.use([
  TitleComponent,
  TooltipComponent,
  GridComponent,
  LegendComponent,
  ToolboxComponent,
  LineChart,
  PieChart,
  BarChart,
  CanvasRenderer
]);

export default echarts;

2. src/components/EChartsBase.tsx

一个通用的 React 19 ECharts 组件

复制代码
// src/components/EChartsBase.tsx
import React, { useRef, useEffect } from 'react';
import echarts from '@/services/echarts';

interface EChartsBaseProps {
  option: echarts.EChartsOption;
  width?: string;
  height?: string;
}

const EChartsBase: React.FC<EChartsBaseProps> = ({
  option,
  width = '100%',
  height = '400px'
}) => {
  const chartRef = useRef<HTMLDivElement | null>(null);
  const chartInstance = useRef<echarts.EChartsType | null>(null);

  useEffect(() => {
    if (chartRef.current) {
      // 如果已有实例,先销毁,防止 React 19 重复渲染
      if (chartInstance.current) {
        chartInstance.current.dispose();
      }
      chartInstance.current = echarts.init(chartRef.current);
      chartInstance.current.setOption(option);
    }

    // 监听窗口大小变化
    const handleResize = () => {
      chartInstance.current?.resize();
    };
    window.addEventListener('resize', handleResize);

    return () => {
      chartInstance.current?.dispose();
      window.removeEventListener('resize', handleResize);
    };
  }, [option]);

  return <div ref={chartRef} style={{ width, height }} />;
};

export default EChartsBase;

3. 使用示例(不传 colors 也能显示)

复制代码
// src/pages/DemoCharts.tsx
import React from 'react';
import EChartsBase from '@/components/EChartsBase';

export default function DemoCharts() {
  const pieOption = {
    title: { text: '示例饼图', left: 'center' },
    tooltip: { trigger: 'item' },
    legend: { bottom: 0 },
    series: [
      {
        name: '访问来源',
        type: 'pie',
        radius: '50%',
        data: [
          { value: 1048, name: '搜索引擎' },
          { value: 735, name: '直接访问' },
          { value: 580, name: '邮件营销' }
        ]
      }
    ]
  };

  const lineOption = {
    title: { text: '示例折线图' },
    tooltip: { trigger: 'axis' },
    xAxis: { type: 'category', data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'] },
    yAxis: { type: 'value' },
    series: [
      { data: [150, 230, 224, 218, 135, 147, 260], type: 'line', smooth: true }
    ]
  };

  const barOption = {
    title: { text: '示例柱状图' },
    tooltip: {},
    xAxis: { type: 'category', data: ['苹果', '香蕉', '橘子', '梨子'] },
    yAxis: { type: 'value' },
    series: [
      { data: [5, 20, 36, 10], type: 'bar' }
    ]
  };

  return (
    <div style={{ padding: 20 }}>
      <EChartsBase option={pieOption} height="300px" />
      <EChartsBase option={lineOption} height="300px" />
      <EChartsBase option={barOption} height="300px" />
    </div>
  );
}

✅ 特点:

  • React 19 兼容

  • 饼图 / 折线图 / 柱状图通用

  • colors 可选,不传也正常显示

  • 自动销毁实例,避免 React 19 重复渲染导致的图表不显示

  • 自动响应窗口大小变化

相关推荐
快起来别睡了5 分钟前
前端存储新世界:IndexedDB详解
前端
阳火锅13 分钟前
# 🛠 被老板逼出来的“表格生成器”:一个前端的自救之路
前端·javascript·面试
Hilaku19 分钟前
我给团队做分享:不聊学什么,而是聊可以不学什么
前端·javascript·架构
Juchecar25 分钟前
TypeScript 中字符串与数值、日期时间的相互转换
javascript·python
土豆_potato28 分钟前
5分钟精通 useMemo
前端·javascript·面试
用户67570498850233 分钟前
一文吃透 Promise 与 async/await,异步编程也能如此简单!建议收藏!
前端·javascript·vue.js
花妖大人1 小时前
Python和Js对比
前端·后端
姑苏洛言1 小时前
使用 ECharts 实现菜品统计和销量统计
前端·javascript·后端
赵小川1 小时前
五年前端面试半年总结,终于知道会哭的孩子有奶吃
前端
小奋斗1 小时前
深入浅出:JavaScript中instanceof详解
javascript