echarts-for-react vs react-chartjs-2 vs recharts
React 数据可视化库
echarts-for-reactreact-chartjs-2recharts类似的npm包:

React 数据可视化库

在现代Web开发中,数据可视化库帮助开发者将复杂的数据以图形化的方式展示,从而使用户能够更直观地理解数据。ECharts、React Chart.js 2和Recharts都是流行的React数据可视化库,各自具有独特的特性和优势,适用于不同的使用场景和需求。选择合适的库可以显著提高开发效率和用户体验。

npm下载趋势

3 年

GitHub Stars 排名

统计详情

npm包名称
下载量
Stars
大小
Issues
发布时间
License
echarts-for-react04,961530 kB502 个月前MIT
react-chartjs-206,92655.1 kB1065 个月前MIT
recharts026,9406.76 MB4379 天前MIT

功能对比: echarts-for-react vs react-chartjs-2 vs recharts

图表类型支持

  • echarts-for-react:

    ECharts for React支持多种图表类型,包括折线图、柱状图、饼图、散点图、雷达图等,且可以通过丰富的配置选项进行高度定制,适合复杂的数据可视化需求。

  • react-chartjs-2:

    React Chart.js 2支持常见的图表类型,如折线图、柱状图、饼图等,适合快速实现标准图表,且易于使用和集成。

  • recharts:

    Recharts提供了一系列基础图表类型,如折线图、柱状图和面积图,且通过组合不同的组件可以轻松创建复杂的图表,强调组件化设计。

交互性与动画

  • echarts-for-react:

    ECharts for React提供丰富的交互性和动画效果,支持数据动态更新和实时交互,适合需要用户与图表进行深度交互的应用场景。

  • react-chartjs-2:

    React Chart.js 2支持基本的动画效果,但交互性相对较少,适合展示静态数据或简单的交互需求。

  • recharts:

    Recharts提供基本的交互功能,如悬停提示和点击事件,适合需要一定交互性的图表,但不如ECharts丰富。

学习曲线

  • echarts-for-react:

    ECharts for React的学习曲线相对较陡,特别是在需要深入定制和复杂配置时,开发者需要花费时间熟悉其API和配置选项。

  • react-chartjs-2:

    React Chart.js 2的学习曲线较平缓,文档清晰,适合快速上手,尤其是对于初学者和需要快速实现图表的项目。

  • recharts:

    Recharts的学习曲线也相对平缓,强调组件化设计,开发者可以快速理解和使用,适合React开发者。

性能

  • echarts-for-react:

    ECharts for React在处理大量数据时表现良好,能够高效渲染复杂的图表,但在极端情况下可能需要优化以提升性能。

  • react-chartjs-2:

    React Chart.js 2在处理较小数据集时性能优越,但在数据量较大时可能会出现性能瓶颈,适合展示简单数据。

  • recharts:

    Recharts在处理中等规模的数据时表现良好,但在数据量过大时可能会出现性能问题,开发者需要注意优化。

社区与支持

  • echarts-for-react:

    ECharts拥有强大的社区支持和丰富的文档资源,开发者可以轻松找到解决方案和示例,适合需要长期维护的项目。

  • react-chartjs-2:

    React Chart.js 2的社区相对较小,但文档清晰,适合快速开发和实现基本功能,支持较好。

  • recharts:

    Recharts在React社区中有一定的用户基础,文档和示例较为丰富,适合希望在React生态中保持一致性的开发者。

如何选择: echarts-for-react vs react-chartjs-2 vs recharts

  • echarts-for-react:

    选择ECharts for React如果你需要一个功能强大且高度可定制的图表库,适合处理复杂的数据可视化需求,特别是在需要交互性和动态更新的场景中。它适合需要丰富图表类型和复杂配置的项目。

  • react-chartjs-2:

    选择React Chart.js 2如果你需要一个简单易用且具有良好文档支持的图表库,适合快速开发和实现常见图表类型。它适合那些希望快速上手并实现基本数据可视化的项目。

  • recharts:

    选择Recharts如果你需要一个基于React的图表库,强调组件化和可重用性,适合构建响应式和灵活的图表。它适合那些希望在React生态系统中保持一致性和简洁性的项目。

echarts-for-react的README

echarts-for-react

The simplest, and the best React wrapper for Apache ECharts. Using visualization with MCP Server mcp-server-chart.

npm build Coverage NPM downloads License ECharts Ver React Ver

Install

$ npm install --save echarts-for-react

# `echarts` is the peerDependence of `echarts-for-react`, you can install echarts with your own version.
$ npm install --save echarts

Then use it.

import ReactECharts from 'echarts-for-react';

// render echarts option.
<ReactECharts option={this.getOption()} />

You can run website.

$ git clone git@github.com:hustcc/echarts-for-react.git

$ npm install

$ npm start

Then open http://127.0.0.1:8081/ in your browser. or see https://git.hust.cc/echarts-for-react/ which is deploy on gh-pages.

Usage

Code of a simple demo code showed below. For more example can see: https://git.hust.cc/echarts-for-react/

import React from 'react';
import ReactECharts from 'echarts-for-react';  // or var ReactECharts = require('echarts-for-react');

<ReactECharts
  option={this.getOption()}
  notMerge={true}
  lazyUpdate={true}
  theme={"theme_name"}
  onChartReady={this.onChartReadyCallback}
  onEvents={EventsDict}
  opts={}
/>

Import ECharts.js modules manually to reduce bundle size

With Echarts.js v5 or v6:

import React from 'react';
// import the core library.
import ReactEChartsCore from 'echarts-for-react/lib/core';
// Import the echarts core module, which provides the necessary interfaces for using echarts.
import * as echarts from 'echarts/core';
// Import charts, all with Chart suffix
import {
  // LineChart,
  BarChart,
  // PieChart,
  // ScatterChart,
  // RadarChart,
  // MapChart,
  // TreeChart,
  // TreemapChart,
  // GraphChart,
  // GaugeChart,
  // FunnelChart,
  // ParallelChart,
  // SankeyChart,
  // BoxplotChart,
  // CandlestickChart,
  // EffectScatterChart,
  // LinesChart,
  // HeatmapChart,
  // PictorialBarChart,
  // ThemeRiverChart,
  // SunburstChart,
  // CustomChart,
} from 'echarts/charts';
// import components, all suffixed with Component
import {
  // GridSimpleComponent,
  GridComponent,
  // PolarComponent,
  // RadarComponent,
  // GeoComponent,
  // SingleAxisComponent,
  // ParallelComponent,
  // CalendarComponent,
  // GraphicComponent,
  // ToolboxComponent,
  TooltipComponent,
  // AxisPointerComponent,
  // BrushComponent,
  TitleComponent,
  // TimelineComponent,
  // MarkPointComponent,
  // MarkLineComponent,
  // MarkAreaComponent,
  // LegendComponent,
  // LegendScrollComponent,
  // LegendPlainComponent,
  // DataZoomComponent,
  // DataZoomInsideComponent,
  // DataZoomSliderComponent,
  // VisualMapComponent,
  // VisualMapContinuousComponent,
  // VisualMapPiecewiseComponent,
  // AriaComponent,
  // TransformComponent,
  DatasetComponent,
} from 'echarts/components';
// Import renderer, note that introducing the CanvasRenderer or SVGRenderer is a required step
import {
  CanvasRenderer,
  // SVGRenderer,
} from 'echarts/renderers';

// Register the required components
echarts.use(
  [TitleComponent, TooltipComponent, GridComponent, BarChart, CanvasRenderer]
);

// The usage of ReactEChartsCore are same with above.
<ReactEChartsCore
  echarts={echarts}
  option={this.getOption()}
  notMerge={true}
  lazyUpdate={true}
  theme={"theme_name"}
  onChartReady={this.onChartReadyCallback}
  onEvents={EventsDict}
  opts={}
/>

With Echarts.js v3 or v4:

import React from 'react';
// import the core library.
import ReactEChartsCore from 'echarts-for-react/lib/core';

// then import echarts modules those you have used manually.
import echarts from 'echarts/lib/echarts';
// import 'echarts/lib/chart/line';
import 'echarts/lib/chart/bar';
// import 'echarts/lib/chart/pie';
// import 'echarts/lib/chart/scatter';
// import 'echarts/lib/chart/radar';

// import 'echarts/lib/chart/map';
// import 'echarts/lib/chart/treemap';
// import 'echarts/lib/chart/graph';
// import 'echarts/lib/chart/gauge';
// import 'echarts/lib/chart/funnel';
// import 'echarts/lib/chart/parallel';
// import 'echarts/lib/chart/sankey';
// import 'echarts/lib/chart/boxplot';
// import 'echarts/lib/chart/candlestick';
// import 'echarts/lib/chart/effectScatter';
// import 'echarts/lib/chart/lines';
// import 'echarts/lib/chart/heatmap';

// import 'echarts/lib/component/graphic';
// import 'echarts/lib/component/grid';
// import 'echarts/lib/component/legend';
import 'echarts/lib/component/tooltip';
// import 'echarts/lib/component/polar';
// import 'echarts/lib/component/geo';
// import 'echarts/lib/component/parallel';
// import 'echarts/lib/component/singleAxis';
// import 'echarts/lib/component/brush';

import 'echarts/lib/component/title';

// import 'echarts/lib/component/dataZoom';
// import 'echarts/lib/component/visualMap';

// import 'echarts/lib/component/markPoint';
// import 'echarts/lib/component/markLine';
// import 'echarts/lib/component/markArea';

// import 'echarts/lib/component/timeline';
// import 'echarts/lib/component/toolbox';

// import 'zrender/lib/vml/vml';

// The usage of ReactEChartsCore are same with above.
<ReactEChartsCore
  echarts={echarts}
  option={this.getOption()}
  notMerge={true}
  lazyUpdate={true}
  theme={"theme_name"}
  onChartReady={this.onChartReadyCallback}
  onEvents={EventsDict}
  opts={}
/>

For Next.js user, code transpilation is needed. For Next.js 13.1 or higher, as all next-transpile-modules features are natively built-in and the package has been deprecated, so please add transpilePackages: ['echarts', 'zrender'] into nextConfig object:

// next.config.js
/** @type {import('next').NextConfig} */

const nextConfig = {
  // ...existing properties,
  transpilePackages: ['echarts', 'zrender'],
}

module.exports = nextConfig

For Next.js with version < 13.1:

// next.config.js
const withTM = require("next-transpile-modules")(["echarts", "zrender"]);

module.exports = withTM({})

Props of Component

  • option (required, object)

the echarts option config, can see https://echarts.apache.org/option.html#title.

  • notMerge (optional, object)

when setOption, not merge the data, default is false. See https://echarts.apache.org/api.html#echartsInstance.setOption.

  • replaceMerge (optional, string | string[])

when setOption, default is null. See https://echarts.apache.org/api.html#echartsInstance.setOption.

  • lazyUpdate (optional, object)

when setOption, lazy update the data, default is false. See https://echarts.apache.org/api.html#echartsInstance.setOption.

  • style (optional, object)

the style of echarts div. object, default is {height: '300px'}.

  • className (optional, string)

the class of echarts div. you can setting the css style of charts by class name.

  • theme (optional, string)

the theme of echarts. string, should registerTheme before use it (theme object format: https://github.com/ecomfe/echarts/blob/master/theme/dark.js). e.g.

// import echarts
import echarts from 'echarts';
...
// register theme object
echarts.registerTheme('my_theme', {
  backgroundColor: '#f4cccc'
});
...
// render the echarts use option `theme`
<ReactECharts
  option={this.getOption()}
  style={{height: '300px', width: '100%'}}
  className='echarts-for-echarts'
  theme='my_theme' />
  • onChartReady (optional, function)

when the chart is ready, will callback the function with the echarts object as it's paramter.

  • loadingOption (optional, object)

the echarts loading option config, can see https://echarts.apache.org/api.html#echartsInstance.showLoading.

  • showLoading (optional, bool, default: false)

bool, when the chart is rendering, show the loading mask.

  • onEvents (optional, array(string=>function) )

binding the echarts event, will callback with the echarts event object, and the echart object as it's paramters. e.g:

const onEvents = {
  'click': this.onChartClick,
  'legendselectchanged': this.onChartLegendselectchanged
}
...
<ReactECharts
  option={this.getOption()}
  style={{ height: '300px', width: '100%' }}
  onEvents={onEvents}
/>

for more event key name, see: https://echarts.apache.org/api.html#events

  • opts (optional, object)

the opts of echarts. object, will be used when initial echarts instance by echarts.init. Document here.

<ReactECharts
  option={this.getOption()}
  style={{ height: '300px' }}
  opts={{renderer: 'svg'}} // use svg to render the chart.
/>
  • autoResize (optional, boolean)

decide whether to trigger this.resize when window resize. default is true.

Component API & Echarts API

the Component only has one API named getEchartsInstance.

  • getEchartsInstance() : get the echarts instance object, then you can use any API of echarts.

for example:

// render the echarts component below with rel
<ReactECharts
  ref={(e) => { this.echartRef = e; }}
  option={this.getOption()}
/>

// then get the `ReactECharts` use this.echarts_react

const echartInstance = this.echartRef.getEchartsInstance();
// then you can use any API of echarts.
const base64 = echartInstance.getDataURL();

TypeScript and useRef() example:

const getOption = () => {/** */};

export default function App() {
	const echartsRef = useRef<InstanceType<typeof ReactEcharts>>(null);

	useEffect(() => {
		if (echartsRef.current) {
			const echartsInstance = echartsRef.current.getEchartsInstance();
			// do something
			echartsInstance.resize();
		}
	}, []);
	return <ReactEcharts ref={echartsRef} option={getOption()} />;
}

About API of echarts, can see https://echarts.apache.org/api.html#echartsInstance.

You can use the API to do:

  1. binding / unbinding event.
  2. dynamic charts with dynamic data.
  3. get the echarts dom / dataURL / base64, save the chart to png.
  4. release the charts.

FAQ

How to render the chart with svg when using echarts 4.x

Use the props opts of component with renderer = 'svg'. For example:

<ReactECharts
  option={this.getOption()}
  style={{height: '300px'}}
  opts={{renderer: 'svg'}} // use svg to render the chart.
/>

How to resolve Error Component series.scatter3D not exists. Load it first.

Install and import echarts-gl module when you want to create a GL instance

npm install --save echarts-gl
import 'echarts-gl'
import ReactECharts from "echarts-for-react";

<ReactECharts
  option={GL_OPTION}
/>

LICENSE

MIT@hustcc.