Interaction events and data zoom
🎯 Introduction
Charts should be useful as well as attractive. In this article, you will add click handlers, custom tooltips, data zoom, and average-line annotations so users can interact with your charts.
🧱 Listening for events with on
Use the instance's on method to listen for interaction events. Here is the common click event:
chart.on('click', (params) => {
console.log(params.name); // Category name, such as 'Mon'
console.log(params.value); // Value, such as 120
console.log(params.seriesName); // Series name, such as 'Sales'
});
Common fields in params:
| Field | Meaning |
|---|---|
name | Category name at the clicked position |
value | Numeric value |
seriesName | Name of the series |
dataIndex | Index of the data item in the array |
For example, clicking a bar can navigate to the detail page for that day:
chart.on('click', (params) => {
router.push(`/detail?day=${params.name}`);
});
✨ Custom tooltips with formatter
By default, tooltip shows “series name: value”. Use formatter to customize the text:
tooltip: {
trigger: 'axis',
formatter: (params) => {
const item = params[0];
return `${item.name} Sales: ${item.value} units`;
},
},
trigger: 'axis', formatter receives an array containing all series at that axis position. With trigger: 'item', it receives a single object. Confusing these types is a common reason for broken formatters.🧱 Data zoom with dataZoom
When there are too many points to fit comfortably on screen, such as daily sales for a year, dataZoom lets users drag or scroll to inspect a smaller range:
dataZoom: [
{
type: 'slider', // Slider below the chart
},
{
type: 'inside', // Scroll and drag within the chart
},
],
'slider': displays a draggable slider below the chart.'inside': zooms directly within the chart using the mouse wheel or touch gestures.
Both types can be used together without conflict.
🧱 Annotations with markPoint and markLine
ECharts provides built-in annotations for highlighting maximum values or drawing an average line, without calculating them manually:
series: [
{
name: 'Sales',
type: 'line',
data: [820, 932, 901, 934, 1290, 1330, 1320],
markPoint: {
data: [
{ type: 'max', name: 'Maximum' },
{ type: 'min', name: 'Minimum' },
],
},
markLine: {
data: [{ type: 'average', name: 'Average' }],
},
},
],
markPoint: marks points, with built-in'max'and'min'types.markLine: draws lines, including the built-in'average'type for average lines.
Here is a live chart with automatic maximum and minimum markers and a calculated average line:
❓ Questions and answers
Q1: What does this refer to in an event callback?
Use arrow functions consistently to avoid confusion about this. You can access variables in the component scope normally inside an arrow function.
Q2: Can formatter return HTML?
Yes. The returned string supports simple HTML tags and inline styles, such as <b> and <br/>, for formatting.
Q3: Where does dataZoom belong in option?
It is a top-level array alongside series and xAxis. By default, it targets the first x-axis.
Q4: Can markLine only draw averages?
No. It also supports maximum and minimum lines, or fixed values such as { yAxis: 100 }. Average and maximum lines are common uses.
🧪 Practice
Add three interactions to the “Weekly sales” line chart:
- When a data point is clicked, use
alertto show “Sales on day: value”. - Add an average
markLine. - Add a
'slider'dataZoom.
const chart = echarts.init(chartRef.value);
chart.setOption({
// Write your configuration here
});
// Write your event handler here
🎉 Your charts now support interaction as well as visual presentation! Next, we will tackle a common real-world requirement: updating charts with changing API data.
