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 重复渲染导致的图表不显示

  • 自动响应窗口大小变化

相关推荐
耀耀切克闹灬7 分钟前
word文档转html(mammoth )
前端
文心快码BaiduComate35 分钟前
双十一将至,用Rules玩转电商场景提效
前端·人工智能·后端
用户187294225083936 分钟前
告别函数的“两面派”人生:深度剖析箭头函数如何一劳永逸地解决 ‘this’ 的二义性
javascript
拉不动的猪38 分钟前
关于scoped样式隔离原理和失效情况&&常见样式隔离方案
前端·javascript·面试
摇滚侠38 分钟前
Vue 项目实战《尚医通》,医院详情菜单与子路由,笔记17
前端·vue.js·笔记
有来技术41 分钟前
vite-plugin-vue-mcp:在 Vue 3 + Vite 中启用 MCP,让 AI 理解并调试你的应用
前端·vue.js·人工智能
fruge1 小时前
前端本地存储进阶:IndexedDB 封装与离线应用开发
前端
忍者扔飞镖1 小时前
欧服加载太慢了,咋整
前端·性能优化
鹏北海1 小时前
Vue 3 超强二维码识别:多区域/多尺度扫描 + 高级图像处理
前端·javascript·vue.js
Jack莱杰1 小时前
Math.js封装工具库(解决前端因为浮点数导致计算错误)
javascript