ECharts Event Handling
In ECharts, we can trigger corresponding callback functions by listening to user interaction behaviors.
ECharts viaonUse a method to listen to user behaviors, for example, monitoring user click actions.
Events in ECharts are divided into two types:
User mouse operation clicks, such as'click'、'dblclick'、'mousedown'、'mousemove'、'mouseup'、'mouseover'、'mouseout'、'globalout'、'contextmenu'Event.
The other type is behavioral events triggered after users interact with interactive components, such as the 'legendselectchanged' event triggered when toggling the legend switch, the 'datazoom' event triggered when scaling the data area, and so on.
myChart.on('click', function (params) {
// 在用户点击后控制台打印数据的名称
console.log(params);
});
myChart.on('legendselectchanged', function (params) {
console.log(params);
});
chart.on('click', 'series.line', function (params) {
console.log(params);
});
chart.on('mouseover', {seriesIndex: 1, name: 'xx'}, function (params) {
console.log(params);
});
Mouse Event
ECharts SupportedofMouse EventType, including 'click'、'dblclick'、'mousedown'、'mousemove'、'mouseup'、'mouseover'、'mouseout'、'globalout'、'contextmenu' Event.
The following example pops up a dialog box when clicking on a bar chart:
Example
var myChart = echarts.init(document.getElementById('main'));
// Specify the chart's configuration options and data
var option = {
xAxis: {
data: ["Shirt",Wool sweater,Chiffon blouse,"Pants",High heels,"Socks"]
},
yAxis: {},
series: [{
name: 'Sales',
type: 'bar',
data: [5, 20, 36, 10, 10, 20]
}]
};
// Use the configuration items and data just specified to display the chart.
myChart.setOption(option);
// Handle click event and pop up data name
myChart.on('click', function (params) {
alert(params.name);
});
Try it Yourself »
All mouse events include the parameter params, which is an object containing data information about the clicked graphic, with the following format:
{
// 当前点击的图形元素所属的组件名称,
// 其值如 'series'、'markLine'、'markPoint'、'timeLine' 等。
componentType: string,
// 系列类型。值可能为:'line'、'bar'、'pie' 等。当 componentType 为 'series' 时有意义。
seriesType: string,
// 系列在传入的 option.series 中的 index。当 componentType 为 'series' 时有意义。
seriesIndex: number,
// 系列名称。当 componentType 为 'series' 时有意义。
seriesName: string,
// 数据名,类目名
name: string,
// 数据在传入的 data 数组中的 index
dataIndex: number,
// 传入的原始数据项
data: Object,
// sankey、graph 等图表同时含有 nodeData 和 edgeData 两种 data,
// dataType 的值会是 'node' 或者 'edge',表示当前点击在 node 还是 edge 上。
// 其他大部分图表中只有一种 data,dataType 无意义。
dataType: string,
// 传入的数据值
value: number|Array
// 数据图形的颜色。当 componentType 为 'series' 时有意义。
color: string
}
How to distinguish where the mouse clicked:
myChart.on('click', function (params) {
if (params.componentType === 'markPoint') {
// 点击到了 markPoint 上
if (params.seriesIndex === 5) {
// 点击到了 index 为 5 的 series 的 markPoint 上。
}
}
else if (params.componentType === 'series') {
if (params.seriesType === 'graph') {
if (params.dataType === 'edge') {
// 点击到了 graph 的 edge(边)上。
}
else {
// 点击到了 graph 的 node(节点)上。
}
}
}
});
Use query to trigger callbacks only for graphic elements of the specified component:
chart.on(eventName, query, handler);
query can be a string or an Object.
If it is a string, it indicates the component type. The format can be 'mainType' or 'mainType.subType'. For example:
chart.on('click', 'series', function () {...});
chart.on('click', 'series.line', function () {...});
chart.on('click', 'dataZoom', function () {...});
chart.on('click', 'xAxis.category', function () {...});
If it is an Object, it can contain one or more of the following attributes, each of which is optional:
{
<mainType>Index: number // 组件 index
<mainType>Name: string // 组件 name
<mainType>Id: string // 组件 id
dataIndex: number // 数据项 index
name: string // 数据项 name
dataType: string // 数据项 type,如关系图中的 'node', 'edge'
element: string // 自定义系列中的 el 的 name
}
For example:
chart.setOption({
// ...
series: [{
name: 'uuu'
// ...
}]
});
chart.on('mouseover', {seriesName: 'uuu'}, function () {
// series name 为 'uuu' 的系列中的图形元素被 'mouseover' 时,此方法被回调。
});
For example:
chart.setOption({
// ...
series: [{
// ...
}, {
// ...
data: [
{name: 'xx', value: 121},
{name: 'yy', value: 33}
]
}]
});
chart.on('mouseover', {seriesIndex: 1, name: 'xx'}, function () {
// series index 1 的系列中的 name 为 'xx' 的元素被 'mouseover' 时,此方法被回调。
});
For example:
chart.setOption({
// ...
series: [{
type: 'graph',
nodes: [{name: 'a', value: 10}, {name: 'b', value: 20}],
edges: [{source: 0, target: 1}]
}]
});
chart.on('click', {dataType: 'node'}, function () {
// 关系图的节点被点击时此方法被回调。
});
chart.on('click', {dataType: 'edge'}, function () {
// 关系图的边被点击时此方法被回调。
});
For example:
chart.setOption({
// ...
series: {
// ...
type: 'custom',
renderItem: function (params, api) {
return {
type: 'group',
children: [{
type: 'circle',
name: 'my_el',
// ...
}, {
// ...
}]
}
},
data: [[12, 33]]
}
})
chart.on('mouseup', {element: 'my_el'}, function () {
// name 为 'my_el' 的元素被 'mouseup' 时,此方法被回调。
});
You can obtain the data name and series name in this object from the callback function, then index other information in your own data repository to update the chart, display overlays, and so on, as shown in the following example code:
Example
$.get('detail?q=' + params.name, function (detail) {
myChart.setOption({
series: [{
name: 'pie',
// Display the data distribution in a single bar through a pie chart
data: [detail.data]
}]
});
});
});
Behavioral events of component interaction
In ECharts, basically all component interaction behaviors trigger corresponding events. Commonly used events and their corresponding parameters are listed in the events documentation.
The following is an example of listening to a legend switch:
Example
myChart.on('legendselectchanged', function (params) {
// Get the selected state of the clicked legend
var isSelected = params.selected[params.name];
// Print to the console
console.log((isSelected ? 'Selected' : 'Deselected') + 'Legend' + params.name);
// Print the state of all legends
console.log(params.selected);
});
Try it Yourself »
Trigger component behaviors in ECharts with code
Above we only described user interactions, but sometimes we also need to call methods in the program to trigger chart behaviors, such as showing tooltip.
ECharts triggers chart behaviors through dispatchAction({ type: '' }), centrally managing all actions, and can also record user behavior paths as needed.
The above example is used for the tooltip in the carousel pie chart:
Example
var dataLen = option.series[0].data.length;
// Unhighlight previously highlighted graphics
myChart.dispatchAction({
type: 'downplay',
seriesIndex: 0,
dataIndex: app.currentIndex
});
app.currentIndex = (app.currentIndex + 1) % dataLen;
// Highlight the current graphic
myChart.dispatchAction({
type: 'highlight',
seriesIndex: 0,
dataIndex: app.currentIndex
});
// Show tooltip
myChart.dispatchAction({
type: 'showTip',
seriesIndex: 0,
dataIndex: app.currentIndex
});
}, 1000);
Try it Yourself »