一、背景
Echarts.js目前已經提供了connect功能,只要圖標的legend一樣,那么就能實現聯動。
下面兩個鏈接是介紹Echarts connect的用法的。
https://blog.csdn.net/u014452812/article/details/78202789
http://echarts.baidu.com/api.html#echarts.connect
真正的圖表聯動展示應該不止于此,D3.js理解思路上與Echarts.js有所不同,可能會更容易實現多圖聯動,這里先介紹Echarts如何讓圖標聯動起來。
二、原始例子
通過重新繪制
http://echarts.baidu.com/examples/editor.html?c=dataset-link
在這個樣例中,隨著鼠標在坐標軸上的移動,餅圖會不斷的變化,其關鍵在于這一段代碼。
myChart.on('updateAxisPointer', function (event) {
var xAxisInfo = event.axesInfo[0];
if (xAxisInfo) {
var dimension = xAxisInfo.value + 1;
myChart.setOption({
series: {
id: 'pie',
label: {
formatter: '{b}: {@[' + dimension + ']} (rwdmaqx%)'
},
encode: {
value: dimension,
tooltip: dimension
}
}
});
}
});
本質上是通過鼠標事件,不斷獲取xAxisInfo,然后根據獲取到的xAxisInfo.value(dimension)重新繪制餅圖。這個可視化過程中數據是以數據集形式呈現的,所以用的encode,具體介紹可以在http://www.echartsjs.com/tutorial.html#%E4%BD%BF%E7%94%A8%20dataset%20%E7%AE%A1%E7%90%86%E6%95%B0%E6%8D%AE看到。
如果要看看event都有些什么,通過console.log(event)
即可獲取,比如移到第一個橫軸坐標2012時,輸出的內容如下,其value為0。
通過事件
Echarts事件介紹:
http://www.echartsjs.com/tutorial.html#ECharts%20%E4%B8%AD%E7%9A%84%E4%BA%8B%E4%BB%B6%E5%92%8C%E8%A1%8C%E4%B8%BA
輪流高亮例子:
http://www.echartsjs.com/gallery/editor.html?c=doc-example/pie-highlight
其關鍵在于
myChart.dispatchAction({
type: 'highlight',
seriesIndex: 0,
dataIndex: app.currentIndex
});
Echarts的action還有很多,在echarts官網的API中可以輸入action進行檢索。
之所以可以使用這個實現多圖聯動,實質上是一個圖上發生鼠標移動事件后,調用另一個圖,使其產生動作。比如mychart1和mychart2,如果獲取到了mychart1的params信息,通過這些信息可以對應到mychart2的dataIndex、SeriesIndex,那么,就可以用這樣的形式實現聯動。
三、實現方法
記住這兩個東西就好。
myChart.setOption \\重新繪制
myChart.dispatchAction \\echarts action
四、具體例子
這是一個對http://echarts.baidu.com/examples/editor.html?c=dataset-link進行的改進。
效果示意:
全部源碼如下,可以先粘走看看效果。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>ECharts</title>
<!-- 引入 echarts.js -->
<script src="echarts.min.js"></script>
</head>
<body>
<!-- 為ECharts準備一個具備大小(寬高)的Dom -->
<div id="main" style="width: 900px;height:600px;"></div>
<script type="text/javascript">
// 基于準備好的dom,初始化echarts實例
var myChart = echarts.init(document.getElementById('main'));
// 指定圖表的配置項和數據
var option = {
legend: {},
tooltip: {
//trigger: 'axis',
showContent: false,
},
dataset: {
source: [
['product', '2012', '2013', '2014', '2015', '2016', '2017'],
['Matcha Latte', 41.1, 30.4, 65.1, 53.3, 83.8, 98.7],
['Milk Tea', 86.5, 92.1, 85.7, 83.1, 73.4, 55.1],
['Cheese Cocoa', 24.1, 67.2, 79.5, 86.4, 65.2, 82.5],
['Walnut Brownie', 55.2, 67.1, 69.2, 72.4, 53.9, 39.1]
]
},
xAxis: {type: 'category',
triggerEvent: true,
//axisPointer: {show:true}
},
yAxis: {gridIndex: 0},
grid: {top: '55%'},
series: [
{type: 'line', smooth: true, seriesLayoutBy: 'row', symbolSize: 10},
{type: 'line', smooth: true, seriesLayoutBy: 'row', symbolSize: 10},
{type: 'line', smooth: true, seriesLayoutBy: 'row', symbolSize: 10},
{type: 'line', smooth: true, seriesLayoutBy: 'row', symbolSize: 10},
{
type: 'pie',
id: 'pie',
radius: '30%',
center: ['50%', '25%'],
label: {
formatter: '{b}: {@[2012]} (cvipv1p%)'
},
encode: {
itemName: 'product',
value: '2012',
tooltip: '2012'
}
}
]
};
setTimeout(function () {
myChart.on('mouseover',function(params){
if(params.componentType == "xAxis"){
var xAxisInfo = params.value;
myChart.setOption({
series: {
id: 'pie',
label: {
formatter: '{b}: {@[' + xAxisInfo + ']} (4sn49nl%)'
},
encode: {
value: xAxisInfo,
tooltip: xAxisInfo
}
}
});
}
if(params.componentType == "series" && params.seriesType == 'line'){
var xAxisInfo = params.value[0];
myChart.setOption({
series: {
id: 'pie',
label: {
formatter: '{b}: {@[' + xAxisInfo + ']} (bplqb4b%)'
},
encode: {
value: xAxisInfo,
tooltip: xAxisInfo
}
}
});
}
setTimeout(function(){
myChart.dispatchAction({
type: 'highlight',
seriesIndex: 4,
dataIndex: params.seriesIndex
});
},300);
});
myChart.on('mouseout',function(params){
myChart.dispatchAction({
type: 'downplay',
seriesIndex: 4,
dataIndex: params.seriesIndex
});
});
},0);
// 使用剛指定的配置項和數據顯示圖表。
myChart.setOption(option);
</script>
</body>
</html>
那兩個if主要用于獲取信息,如果不是兩個圖畫在同一個mychart里其實就沒有那么麻煩,實際上可以忽略不看。在鼠標移動到某一個折線圖的點的時候,餅圖中對應的塊將會高亮。
實現的關鍵就在于通過鼠標事件,獲取到了params的相關信息,通過
myChart.dispatchAction({
type: 'highlight',
seriesIndex: 4,
dataIndex: params.seriesIndex
});
實現餅圖部分的高亮。這個例子中兩個圖是在同一個mychart中,如果我們畫在兩個不同的圖像中如何實現高亮呢。
簡化來寫就是這樣
myChart1.on('mouseover',function(params){
myChart2.dispatchAction({
type: 'highlight',
seriesIndex: 4,
dataIndex: params.seriesIndex
});
});
具體需要根據params的對應情況來實現高亮。
有空再做更詳細的聯動展示例子。