React Wrapper for Apache ECharts
echarts-for-react is a robust and widely-used React component that seamlessly integrates Apache ECharts, a powerful charting library, into React applications. It provides a declarative way to render various types of charts and visualizations, abstracting away direct ECharts instance management. The current stable version is 3.0.6. The project maintains an active development pace with frequent minor updates and bug fixes, building upon the significant v3.0.0 release that introduced ECharts v5 support and a full TypeScript rewrite. Key differentiators include its simplicity, direct exposure of ECharts options as React props, comprehensive TypeScript support, and the ability to manually import ECharts modules for bundle size optimization, making it a preferred choice for React developers needing advanced data visualization.
Common errors
-
Error: ECharts is not initialized
cause The `echarts` peer dependency is missing or not accessible at runtime.fixInstall `echarts` in your project: `npm install echarts` or `yarn add echarts`. Verify that it's correctly listed in your `package.json`. -
TypeScript error: Property 'option' does not exist on type 'IntrinsicAttributes & ReactEChartsProps'.
cause This typically indicates an outdated `echarts-for-react` version being used with newer ECharts options, or a conflict in TypeScript type definitions. It might also occur if a type from ECharts is not correctly imported.fixEnsure `echarts-for-react` is `v3.0.0` or higher, and `echarts` is also up-to-date (e.g., `^5.0.0` or `^6.0.0`). Check your `tsconfig.json` for correct module resolution and ensure `EChartsOption` type is imported if explicitly used. -
Error: Component 'grid' not found.
cause When using `ReactEChartsCore` for bundle size optimization, not all necessary ECharts components (like `GridComponent`, `TooltipComponent`, or specific chart types) have been explicitly imported and registered with `echarts.use()`.fixManually import all required ECharts components, charts, and renderers (e.g., `GridComponent`, `BarChart`, `CanvasRenderer`) from their respective `echarts/components`, `echarts/charts`, and `echarts/renderers` paths, then register them using `echarts.use([Component1, ChartType1, ...])`.
Warnings
- breaking The v3.0.0 release introduced a complete rewrite in TypeScript and explicit support for ECharts v5. Projects not using TypeScript may need refactoring due to changes in prop type validation and internal component structures. While generally backward compatible with ECharts v3 and v4, using older versions might not leverage all new features or bug fixes.
- gotcha Large bundle sizes can occur if the entire ECharts library is imported instead of only the necessary modules. This is a common issue when using the default `echarts-for-react` import without specific optimizations.
- gotcha `echarts-for-react` lists `echarts` as a peer dependency. This means `echarts` itself must be explicitly installed in your project, otherwise, the component will fail at runtime.
- gotcha ECharts instances do not always automatically resize when their containing HTML element changes dimensions. While `echarts-for-react` includes some resize detection, complex or dynamic layout changes may require manual intervention.
Install
-
npm install echarts-for-react -
yarn add echarts-for-react -
pnpm add echarts-for-react
Imports
- ReactECharts
const ReactECharts = require('echarts-for-react');import ReactECharts from 'echarts-for-react';
- ReactEChartsCore
import { ReactEChartsCore } from 'echarts-for-react/lib/core';import ReactEChartsCore from 'echarts-for-react/lib/core';
- EChartsOption
import type { EChartsOption } from 'echarts-for-react';
Quickstart
import React, { useState, useEffect, useRef } from 'react';
import ReactECharts from 'echarts-for-react'; // Or use 'echarts-for-react/lib/core' with manual imports
import * as echarts from 'echarts/core';
import { BarChart } from 'echarts/charts';
import { GridComponent, TooltipComponent, TitleComponent, DatasetComponent } from 'echarts/components';
import { CanvasRenderer } from 'echarts/renderers';
// Register necessary ECharts components if using ReactEChartsCore
// For ReactECharts (default import), these are often included, but explicit registration is good practice
echarts.use([
BarChart,
GridComponent,
TooltipComponent,
TitleComponent,
DatasetComponent,
CanvasRenderer
]);
const EChartsDemo: React.FC = () => {
const chartRef = useRef<echarts.ECharts | null>(null);
const getOption = () => {
return {
title: {
text: 'ECharts Sample Bar Chart'
},
tooltip: {},
xAxis: {
type: 'category',
data: ['Category A', 'Category B', 'Category C', 'Category D', 'Category E']
},
yAxis: {
type: 'value'
},
series: [{
name: 'Sales',
data: [120, 200, 150, 80, 70],
type: 'bar'
}]
};
};
const onChartReady = (instance: echarts.ECharts) => {
console.log('ECharts instance is ready:', instance);
chartRef.current = instance;
};
const onEvents = {
'click': (params: any) => {
console.log('Chart clicked:', params.name);
},
'mouseover': (params: any) => {
// console.log('Mouse over:', params.seriesName);
}
};
useEffect(() => {
// Example: Manual resize after a delay (e.g., if container size changes dynamically)
const timer = setTimeout(() => {
chartRef.current?.resize();
console.log('ECharts instance manually resized.');
}, 2000);
return () => clearTimeout(timer);
}, []);
return (
<div style={{ width: '100%', height: '400px', border: '1px solid #ccc', padding: '10px' }}>
<h3>Your ECharts Visualization</h3>
<ReactECharts
option={getOption()}
notMerge={true}
lazyUpdate={true}
theme="light"
onChartReady={onChartReady}
onEvents={onEvents}
opts={{ renderer: 'canvas' }} // Recommend specifying renderer explicitly
style={{ height: '100%' }}
/>
</div>
);
};
export default EChartsDemo;