Building Interactive Charts with ECharts for Websites
Apache ECharts is a powerful library for complex visualizations: heatmaps, relationship graphs, treemaps, sunbursts, scatter plots, candlestick charts. Suitable for dashboards with large datasets.
Installation
npm install echarts echarts-for-react
Basic React Integration
import ReactECharts from 'echarts-for-react';
function RevenueChart({ data }) {
const option = {
tooltip: {
trigger: 'axis',
formatter: (params) => {
const p = params[0];
return `${p.axisValue}<br/>${p.marker}Revenue: <b>${formatCurrency(p.value)}</b>`;
}
},
legend: { data: ['Revenue', 'Forecast'] },
grid: { left: '3%', right: '4%', bottom: '3%', containLabel: true },
xAxis: {
type: 'category',
boundaryGap: false,
data: data.map(d => d.date)
},
yAxis: {
type: 'value',
axisLabel: { formatter: v => `${(v/1000).toFixed(0)}k` }
},
series: [
{
name: 'Revenue',
type: 'line',
smooth: true,
areaStyle: { opacity: 0.2 },
data: data.map(d => d.revenue),
lineStyle: { color: '#3b82f6' }
},
{
name: 'Forecast',
type: 'line',
smooth: true,
lineStyle: { type: 'dashed', color: '#f59e0b' },
data: data.map(d => d.forecast)
}
]
};
return (
<ReactECharts
option={option}
style={{ height: 350 }}
opts={{ renderer: 'svg' }}
/>
);
}
Heatmap (Activity Heatmap)
function ActivityHeatmap({ data }) {
const option = {
tooltip: { position: 'top' },
grid: { height: '50%', top: '10%' },
xAxis: {
type: 'category',
data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
splitArea: { show: true }
},
yAxis: {
type: 'category',
data: Array.from({ length: 24 }, (_, i) => `${i}:00`),
splitArea: { show: true }
},
visualMap: {
min: 0, max: 100,
calculable: true,
orient: 'horizontal',
left: 'center',
bottom: '15%',
inRange: { color: ['#f0f9ff', '#0369a1'] }
},
series: [{
name: 'Activity',
type: 'heatmap',
data: data,
label: { show: false },
emphasis: { itemStyle: { shadowBlur: 10 } }
}]
};
return <ReactECharts option={option} style={{ height: 400 }} />;
}
Candlestick (Stock Chart)
function CandlestickChart({ ohlcData }) {
const option = {
xAxis: {
type: 'category',
data: ohlcData.map(d => d.date)
},
yAxis: { scale: true },
series: [{
type: 'candlestick',
data: ohlcData.map(d => [d.open, d.close, d.low, d.high]),
itemStyle: {
color: '#22c55e',
color0: '#ef4444',
borderColor: '#22c55e',
borderColor0: '#ef4444'
}
}]
};
return <ReactECharts option={option} style={{ height: 400 }} />;
}
DataZoom — Zooming
const option = {
dataZoom: [
{
type: 'inside',
start: 0,
end: 100
},
{
type: 'slider',
start: 0,
end: 100
}
],
// ... rest of option
};
Dynamic Data Updates
const chartRef = useRef();
function updateChart(newData) {
const chart = chartRef.current?.getEchartsInstance();
if (!chart) return;
chart.setOption({
series: [{ data: newData }]
}, { notMerge: false });
}
Timeline
5–7 chart types with DataZoom and tooltip — 3–5 days.







