在做這個項目的時候遇到了echart數據實時更新與dataZoom一起使用,但是
及時更新在通常情況下是沒有問題,但是和dataZoom縮放組件一起使用的話。每次的setOption都會將以前的dataZoom進行重置。
比如,現在的dataZoom的初始位置為80-100,手動縮放到40-60,此時使用setOption的話,dataZoom會瞬間回到80-100的位置,即回到重新設置的位置。
這並不是我們想要的效果,經歷幾次修改,在此記錄一下
首先datazoom的設置:
dataZoom: [{
type: 'slider',
start: 0,
end: 50
}, {
type: 'inside',
start: 0,
end: 50
}],
然后全局綁定滾輪事件,獲得dataZoom最新的位置:
myChart.on('dataZoom',function(event){
if(event.batch){
start=event.batch[0].start;
end=event.batch[0].end;
}else{
start=event.start;
end=event.end;
};
});
最后把最新的start和end賦值給要更新的option:
window.setInterval(function () {
option.dataZoom[0].start=start;
option.dataZoom[0].end=end;
myChart.setOption(option);
},3000);