当前位置:首页 > React

react如何引入echarts

2026-03-30 18:39:33React

安装依赖

在项目中安装 echartsecharts-for-react 库。echarts-for-react 是一个封装好的 React 组件,方便在 React 中使用 ECharts。

npm install echarts echarts-for-react

引入基本图表

创建一个 React 组件并引入 ECharts 图表。以下是一个简单的折线图示例:

react如何引入echarts

import React from 'react';
import ReactECharts from 'echarts-for-react';

const LineChart = () => {
  const option = {
    xAxis: {
      type: 'category',
      data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
    },
    yAxis: {
      type: 'value'
    },
    series: [
      {
        data: [820, 932, 901, 934, 1290, 1330, 1320],
        type: 'line'
      }
    ]
  };

  return <ReactECharts option={option} />;
};

export default LineChart;

按需引入模块

如果项目对体积敏感,可以按需引入 ECharts 模块以减少打包体积:

import React from 'react';
import ReactECharts from 'echarts-for-react';
import * as echarts from 'echarts/core';
import { LineChart } from 'echarts/charts';
import {
  TitleComponent,
  TooltipComponent,
  GridComponent
} from 'echarts/components';
import { CanvasRenderer } from 'echarts/renderers';

// 注册必要的组件
echarts.use([
  LineChart,
  TitleComponent,
  TooltipComponent,
  GridComponent,
  CanvasRenderer
]);

const CustomChart = () => {
  const option = {
    title: {
      text: '按需引入示例'
    },
    tooltip: {},
    xAxis: {
      data: ['A', 'B', 'C', 'D', 'E']
    },
    yAxis: {},
    series: [
      {
        name: '数据',
        type: 'line',
        data: [5, 20, 36, 10, 10]
      }
    ]
  };

  return <ReactECharts echarts={echarts} option={option} />;
};

export default CustomChart;

动态更新数据

通过 React 的状态管理动态更新图表数据:

react如何引入echarts

import React, { useState, useEffect } from 'react';
import ReactECharts from 'echarts-for-react';

const DynamicChart = () => {
  const [data, setData] = useState([10, 20, 30, 40, 50]);

  useEffect(() => {
    const interval = setInterval(() => {
      setData(prevData =>
        prevData.map(value => value + Math.floor(Math.random() * 10) - 5)
      );
    }, 1000);
    return () => clearInterval(interval);
  }, []);

  const option = {
    xAxis: {
      type: 'category',
      data: ['A', 'B', 'C', 'D', 'E']
    },
    yAxis: {
      type: 'value'
    },
    series: [
      {
        data,
        type: 'bar'
      }
    ]
  };

  return <ReactECharts option={option} />;
};

export default DynamicChart;

主题配置

可以使用 ECharts 的主题功能自定义图表样式:

import React from 'react';
import ReactECharts from 'echarts-for-react';
import { dark } from 'echarts/themes';

const ThemedChart = () => {
  const option = {
    title: {
      text: '深色主题示例'
    },
    xAxis: {
      type: 'category',
      data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
    },
    yAxis: {
      type: 'value'
    },
    series: [
      {
        data: [120, 200, 150, 80, 70, 110, 130],
        type: 'bar'
      }
    ]
  };

  return <ReactECharts option={option} theme={dark} />;
};

export default ThemedChart;

事件处理

为图表添加事件处理函数:

import React from 'react';
import ReactECharts from 'echarts-for-react';

const EventChart = () => {
  const onChartClick = params => {
    console.log('图表点击事件', params);
  };

  const onEvents = {
    click: onChartClick
  };

  const option = {
    xAxis: {
      type: 'category',
      data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
    },
    yAxis: {
      type: 'value'
    },
    series: [
      {
        data: [120, 200, 150, 80, 70, 110, 130],
        type: 'bar'
      }
    ]
  };

  return <ReactECharts option={option} onEvents={onEvents} />;
};

export default EventChart;

标签: reactecharts
分享给朋友:

相关文章

电脑如何安装react

电脑如何安装react

安装 Node.js 和 npm React 依赖于 Node.js 和 npm(Node Package Manager)。从 Node.js 官网下载并安装最新稳定版本,安装完成后会自动包含 np…

如何改造react

如何改造react

改造 React 项目的关键方法 分析当前项目结构 通过评估现有组件、状态管理和依赖项,明确需要改进的部分。使用工具如 webpack-bundle-analyzer 识别性能瓶颈。 升级 Reac…

如何构建react

如何构建react

构建 React 项目的步骤 安装 Node.js 和 npm 确保系统已安装 Node.js(包含 npm)。可通过命令行验证: node -v npm -v 使用 Create React Ap…

react如何发布

react如何发布

发布React应用的步骤 构建生产版本 使用以下命令生成优化后的生产版本代码,代码会被压缩并移除开发环境中的调试工具: npm run build 构建完成后会生成build文件夹,包含所有静态资源…

react 如何循环

react 如何循环

循环渲染列表 在React中,循环渲染列表通常使用map方法。map可以遍历数组并返回一个新的React元素数组。 const items = ['Apple', 'Banana', 'Orange…

react如何减少伤病

react如何减少伤病

减少React应用中的性能问题(避免"伤病") 优化React应用性能需要从组件渲染、状态管理和外部依赖等多方面入手。以下是关键优化策略: 使用React.memo进行组件记忆 对函数组件使用Rea…