echarts-for-react: Setup, Examples & Interactive React Charts
What echarts-for-react is (and when to pick it)
In one sentence: echarts-for-react is a lightweight React wrapper around Apache ECharts that makes it straightforward to render high-performance, interactive charts inside React components. If you need full-featured visualization — multi-axis plots, geographic maps, complex tooltips, or highly interactive dashboards — Apache ECharts is a powerful engine; the wrapper adapts that engine to React idioms.
Compared to typical React chart libraries, echarts-for-react exposes the ECharts instance and event model directly, so you get programmatic control (zooming, highlighting, dynamic updates) without fighting a restrictive API. That directness is ideal for data-heavy dashboards and apps where custom interactivity or performance tuning matters.
Quick pointer: for a practical walkthrough, see this echarts-for-react tutorial and the official docs at Apache ECharts.
Getting started: installation & setup (minimal example)
First, install both ECharts and the React wrapper. ECharts is the rendering engine; the wrapper simply mounts it in React. Use npm or yarn depending on your project convention.
- npm: npm i echarts echarts-for-react
- yarn: yarn add echarts echarts-for-react
- TypeScript tip: install @types/echarts (if needed) or rely on the package’s types.
Here’s a minimal functional example demonstrating the wrapper, an options object, and basic event wiring. It targets a concise featured-snippet style: “How do I render a simple chart?”
// MyChart.jsx (React 17+ / 18+)
import React, { useRef, useMemo } from 'react';
import ReactECharts from 'echarts-for-react';
const MyChart = ({ data }) => {
// Memoize options to avoid unnecessary re-renders
const option = useMemo(() => ({
title: { text: 'Sales' },
tooltip: {},
xAxis: { type: 'category', data: data.map(d => d.label) },
yAxis: { type: 'value' },
series: [{ type: 'bar', data: data.map(d => d.value) }]
}), [data]);
// Event handlers mapping
const onEvents = {
click: params => console.log('clicked', params)
};
return ;
};
export default MyChart;
Key setup notes: memoize option objects (useMemo) to prevent unnecessary diffing, pass height/width via style or container, and provide an onEvents object for event handling. For direct API access (export image, resize, dispatch action), use a ref and getEchartsInstance().
Example of accessing the instance:
const ref = useRef(null);
// later:
const instance = ref.current.getEchartsInstance();
instance.dispatchAction({ type: 'highlight', seriesIndex: 0, dataIndex: 2 });
Core concepts, examples, and customization
Options are the heart of ECharts: they declare axes, series, datasets, tooltips, and more. Use dataset to decouple data from series and let ECharts handle transforms and performance optimizations. For complex UIs, split your option into a stable base and a small dynamic overlay (e.g., only update series.data).
Customization covers themes, visual maps, and custom renderers. Register a theme at app startup:
import * as echarts from 'echarts/core';
import { registerTheme } from 'echarts';
registerTheme('my-theme', {
color: ['#2f8cff', '#34c38f', '#f1c40f'],
// other theme fields
});
Then pass theme=”my-theme” to the wrapper:
For advanced visuals (sunburst, map, graph), prefer lazy-loading modules and tree-shakable builds to keep bundle size manageable. The ECharts ecosystem has modular imports; use the core + required chart types in modern builds.
Events, interactivity, and dashboard patterns
Interactions are straightforward: an onEvents object maps event names (like ‘click’, ‘mouseover’, ‘legendselectchanged’) to handlers. Use these handlers to update React state, sync filters across charts, or push analytics events. For example, clicking a bar can set a React state that triggers other charts to filter their datasets.
For dashboards, follow these patterns: (1) centralize data transforms outside chart components so multiple charts share a common dataset; (2) use refs and getEchartsInstance() for imperative actions (zooming, dispatchAction); (3) debounce updates and batch state changes to prevent thrashing.
Performance tips: prefer dataset + encode over constructing heavy series objects, memoize props, and avoid re-creating functions inline (move handlers outside renders or use useCallback). For very large datasets, use progressive rendering or server-side aggregation and downsampling.
- Sync interactions with dispatchAction (highlight/select).
- Use events for drill-downs or cross-filtering.
- Memoize and debouce updates; use dataset for efficient rendering.
Integrations and advanced techniques
echarts-for-react plays nicely with routing, state managers (Redux, Zustand), and server-driven UIs. Keep chart components pure: let them accept props and avoid side effects except through exposed refs. When embedding inside complex layouts, ensure container size changes trigger chart.resize(); the wrapper often handles resize, but you can call instance.resize() on layout changes.
For TypeScript users, create minimal types around options or import ECharts types from the echarts package. If you need tree-shaking, prefer modular imports from the ECharts core and register only used components.
Common advanced use cases: exporting PNG/SVG, programmatic animations with dispatchAction, and combining multiple chart types in a single canvas via multiple series. When you need low-level control, use getEchartsInstance() — it gives the full ECharts API in your React component.
Example resources: read a practical walkthrough on a community tutorial: echarts-for-react tutorial and check the official docs at Apache ECharts.
When to choose echarts-for-react (short checklist)
Use echarts-for-react when you need rich interactions, custom tooltips, maps, or a lot of chart types that are production-tested. If you prefer a minimalist chart API or need tight React-style immutability, evaluate other libraries — but ECharts’ flexibility is hard to beat for dashboards.
- Large dashboards requiring cross-filtering and programmatic control.
- Rich visualizations (maps, heatmaps, graph layouts, sunburst).
- When you need event-heavy interactivity and performance tuning.
If you need a plug-and-play chart with minimal customization, consider lighter alternatives. But for complex data visualization requirements, echarts-for-react + Apache ECharts is a pragmatic, flexible choice.
FAQ
Q: How do I install and get started with echarts-for-react?
A: Install both echarts and the wrapper (npm i echarts echarts-for-react), import ReactECharts, pass an option object and render. Memoize options and use refs for advanced APIs.
Q: How do I handle events and interactivity in echarts-for-react?
A: Provide an onEvents object mapping event names (like ‘click’) to handlers, and/or use getEchartsInstance() from a ref to call instance APIs like dispatchAction and resize.
Q: How do I customize charts and themes in echarts-for-react?
A: Register themes with ECharts, pass theme prop to the wrapper, use tooltip.formatter and visualMap for conditional styling, and prefer dataset/encode for clean, performant option structures.
Semantic core (keyword clusters)
Primary cluster: echarts-for-react, React ECharts, React chart library, React data visualization, React chart component.
Secondary cluster (intent-based & medium-frequency): echarts-for-react tutorial, echarts-for-react installation, echarts-for-react setup, echarts-for-react example, echarts-for-react getting started, React ECharts dashboard, React interactive charts.
Clarifying & LSI phrases (synonyms, related): Apache ECharts for React, React chart wrapper, echarts react wrapper, echarts customization, echarts events, echarts-for-react customization, react chart events, interactive charts in React, echarts performance tips.
Use these phrases naturally across headings, code captions, and alt text to improve topical relevance and voice-search matching (questions such as “how to install echarts-for-react” or “how to handle click events in echarts react”).
