事件與行為
使用者在 ECharts 圖表中可以透過滑鼠等方式觸發相應的事件。開發者可以監聽這些事件,然後透過回撥函式做相應的處理,例如,跳轉到一個新的連結,或者彈出一個對話方塊,或者進行資料下鑽等等。
ECharts 中的事件名稱對應於 DOM 事件名稱,均為小寫字串。這是一個繫結 click
事件的例子。
myChart.on('click', function(params) {
// Print name in console
console.log(params.name);
});
在 ECharts 中,事件有兩種。一種是滑鼠事件,在使用者滑鼠點選或懸浮到圖形上時觸發。另一種是使用者在使用可以互動的元件後觸發的行為事件。例如,切換圖例(legend)時會觸發 'legendselectchanged' 事件(注意,`legendselected` 事件在 ECharts 3 中不再支援),縮放資料區域(dataZoom)時會觸發 'datazoom' 事件。
滑鼠事件的處理
ECharts 支援常規的滑鼠事件,包括 'click'
、'dblclick'
、'mousedown'
、'mousemove'
、'mouseup'
、'mouseover'
、'mouseout'
、'globalout'
、'contextmenu'
。下面是一個在柱狀圖上點選,然後開啟對應的百度搜索頁面的示例。
// Init the ECharts base on DOM
var myChart = echarts.init(document.getElementById('main'));
// Config
var option = {
xAxis: {
data: [
'Shirt',
'Wool sweater',
'Chiffon shirt',
'Pants',
'High-heeled shoes',
'socks'
]
},
yAxis: {},
series: [
{
name: 'Sales',
type: 'bar',
data: [5, 20, 36, 10, 10, 20]
}
]
};
// Use the option and data to display the chart
myChart.setOption(option);
// Click and jump to Baidu search website
myChart.on('click', function(params) {
window.open(
'https://www.google.com/search?q=' + encodeURIComponent(params.name)
);
});
所有的滑鼠事件都包含一個引數 params
,裡面包含了當前資料的各種資訊。
格式
type EventParams = {
// The component name clicked,
// component type, could be 'series'、'markLine'、'markPoint'、'timeLine', etc..
componentType: string,
// series type, could be 'line'、'bar'、'pie', etc.. Works when componentType is 'series'.
seriesType: string,
// the index in option.series. Works when componentType is 'series'.
seriesIndex: number,
// series name, works when componentType is 'series'.
seriesName: string,
// name of data (categories).
name: string,
// the index in 'data' array.
dataIndex: number,
// incoming raw data item
data: Object,
// charts like 'sankey' and 'graph' included nodeData and edgeData as the same time.
// dataType can be 'node' or 'edge', indicates whether the current click is on node or edge.
// most of charts have one kind of data, the dataType is meaningless
dataType: string,
// incoming data value
value: number | Array,
// color of the shape, works when componentType is 'series'.
color: string
};
判斷滑鼠點選到了哪裡。
myChart.on('click', function(params) {
if (params.componentType === 'markPoint') {
// Clicked on the markPoint
if (params.seriesIndex === 5) {
// clicked on the markPoint of the series with index = 5
}
} else if (params.componentType === 'series') {
if (params.seriesType === 'graph') {
if (params.dataType === 'edge') {
// clicked at the edge of graph.
} else {
// clicked at the node of graph.
}
}
}
});
使用 query
來觸發指定元件的回撥
chart.on(eventName, query, handler);
query
可以是 string
或者 Object
。
如果是 string
,格式可以是 mainType
或者 mainType.subType
,例如:
chart.on('click', 'series', function () {...});
chart.on('click', 'series.line', function () {...});
chart.on('click', 'dataZoom', function () {...});
chart.on('click', 'xAxis.category', function () {...});
如果是 Object
,query
可以包含一個或多個屬性
{
${mainType}Index: number // component index
${mainType}Name: string // component name
${mainType}Id: string // component id
dataIndex: number // data item index
name: string // data item name
dataType: string // date item type, such as 'node', 'edge'
element: string // name of element in custom series.
}
例如:
chart.setOption({
// ...
series: [
{
name: 'uuu'
// ...
}
]
});
chart.on('mouseover', { seriesName: 'uuu' }, function() {
// when elements in series named 'uuu' triggered 'mouseover'
});
例如:
chart.setOption({
// ...
series: [
{
// ...
},
{
// ...
data: [
{ name: 'xx', value: 121 },
{ name: 'yy', value: 33 }
]
}
]
});
chart.on('mouseover', { seriesIndex: 1, name: 'xx' }, function() {
// when data named 'xx' in series index 1 triggered 'mouseover'.
});
例如:
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() {
// call this method while the node of graph was clicked.
});
chart.on('click', { dataType: 'edge' }, function() {
// call this method while the edge of graph was clicked.
});
例如:
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() {
// when data named 'my_el' triggered 'mouseup'.
});
你可以在回撥函式中,根據資料名或系列名,透過資料庫查詢結果來顯示一個彈出視窗或更新圖表。下面是一個例子:
myChart.on('click', function(parmas) {
$.get('detail?q=' + params.name, function(detail) {
myChart.setOption({
series: [
{
name: 'pie',
// using pie chart to show the data distribution in one column.
data: [detail.data]
}
]
});
});
});
元件互動事件
ECharts 中所有的元件互動都會觸發相應的事件。常用的事件和引數在 events 文件中列出。
下面是一個監聽圖例事件的例子:
// Show/hide the legend only trigger legendselectchanged event
myChart.on('legendselectchanged', function(params) {
// State if legend is selected.
var isSelected = params.selected[params.name];
// print in the console.
console.log(
(isSelected ? 'Selected' : 'Not Selected') + 'legend' + params.name
);
// print for all legends.
console.log(params.selected);
});
透過程式碼手動觸發元件行為
像 'legendselectchanged'
這樣的事件不僅能由使用者觸發,也可以透過程式碼手動觸發。這可以用來顯示提示框(tooltip)或選擇圖例。
在 ECharts 中,使用 myChart.dispatchAction({ type: '' })
來觸發行為。這種方式可以管理所有的動作,並方便地記錄這些行為。
常用的行為和對應的引數在 action 中列出。
下面的例子展示瞭如何使用 dispatchAction
在餅圖中逐個高亮扇區。
option = { tooltip: { trigger: 'item', formatter: '{a} <br/>{b} : {c} ({d}%)' }, legend: { orient: 'vertical', left: 'left', data: [ 'Direct Access', 'Email Marketing', 'Affiliate Ads', 'Video Ads', 'Search Engines' ] }, series: [ { name: 'Access Source', type: 'pie', radius: '55%', center: ['50%', '60%'], data: [ { value: 335, name: 'Direct Access' }, { value: 310, name: 'Email Marketing' }, { value: 234, name: 'Affiliate Ads' }, { value: 135, name: 'Video Ads' }, { value: 1548, name: 'Search Engines' } ], emphasis: { itemStyle: { shadowBlur: 10, shadowOffsetX: 0, shadowColor: 'rgba(0, 0, 0, 0.5)' } } } ] }; let currentIndex = -1; setInterval(function() { var dataLen = option.series[0].data.length; myChart.dispatchAction({ type: 'downplay', seriesIndex: 0, dataIndex: currentIndex }); currentIndex = (currentIndex + 1) % dataLen; myChart.dispatchAction({ type: 'highlight', seriesIndex: 0, dataIndex: currentIndex }); myChart.dispatchAction({ type: 'showTip', seriesIndex: 0, dataIndex: currentIndex }); }, 1000);
監聽空白區域的事件
有時開發者需要監聽從畫布空白區域觸發的事件。例如,當用戶點選空白區域時需要重置圖表。
在討論這個功能之前,我們需要澄清兩種事件:zrender 事件和 echarts 事件。
myChart.getZr().on('click', function(event) {
// This listener is listening to a `zrender event`.
});
myChart.on('click', function(event) {
// This listener is listening to a `echarts event`.
});
zrender 事件不同於 echarts 事件。前者在滑鼠/指標位於任何位置時都會觸發,而後者只有在滑鼠/指標位於圖形元素上時才能觸發。實際上,echarts 事件是基於 zrender 事件實現的,也就是說,當一個 zrender 事件在圖形元素上觸發時,echarts 就會觸發一個 echarts 事件。
有了 zrender 事件,我們可以像下面這樣實現在空白區域監聽事件:
myChart.getZr().on('click', function(event) {
// No "target" means that mouse/pointer is not on
// any of the graphic elements, which is "blank".
if (!event.target) {
// Click on blank. Do something.
}
});