第一種方法
function autohover() {
var count = 0;
var timeTicket = null;
var dataLength = 34; //此處設置的是需要輪播的次數
timeTicket && clearInterval(timeTicket);
timeTicket = setInterval(function () {
myChart.dispatchAction({
type: 'downplay',
seriesIndex: 0, //serieIndex的索引值 可以觸發多個
});
myChart.dispatchAction({
type: 'highlight',
seriesIndex: 0,
dataIndex: (count) % dataLength
});
myChart.dispatchAction({
type: 'showTip',
seriesIndex: 0,
dataIndex: (count) % dataLength
});
count++;
}, time);
myChart.on('mouseover', function (params) {
clearInterval(timeTicket);
myChart.dispatchAction({
type: 'downplay',
seriesIndex: 0
});
myChart.dispatchAction({
type: 'highlight',
seriesIndex: 0,
dataIndex: params.dataIndex
});
myChart.dispatchAction({
type: 'showTip',
seriesIndex: 0,
dataIndex: params.dataIndex,
});
});
myChart.on('mouseout', function (params) {
timeTicket && clearInterval(timeTicket);
timeTicket = setInterval(function () {
myChart.dispatchAction({
type: 'downplay',
seriesIndex: 0,
});
myChart.dispatchAction({
type: 'highlight',
seriesIndex: 0,
dataIndex: (count) % dataLength
});
myChart.dispatchAction({
type: 'showTip',
seriesIndex: 0,
dataIndex: (count) % dataLength
});
count++;
}, 1000);
});
}
第二種方法 引入自動輪播插件echarts-tooltip-cyclic-display
github:https://github.com/chengwubin/echarts-tooltip-cyclic-display
使用方法與第三種基本一致
第三種方法 引入自動輪播插件echarts-auto-tooltip
github:https://github.com/liuyishiforever/echarts-auto-tooltip
## 使用方法
1. 引入ehcrts-auto-tooltips
```html
<script type="text/javascript" src="js/echarts-auto-tooltip.js"></script>
```
2. 在初始化 echarts 實例並通過 setOption 方法生成圖表的代碼下添加如下代碼
```js
// 使用指定的配置項和數據顯示圖表
myChart.setOption(option);
tools.loopShowTooltip(myChart, option, {loopSeries: true}); // 使用本插件
```
## 參數說明:
mychart: 初始化echarts的實例
option: 指定圖表的配置項和數據
loopOption: 本插件的配置
| 屬性 | 說明 | 默認值 |
| ----------- | ------------------------------------------------------------------------- | ------------------- |
| interval | 輪播時間間隔,單位毫秒 | 默認爲2000 |
| loopSeries | true表示循環所有series的tooltip,false則顯示指定seriesIndex的tooltip | boolean類型,默認爲false |
| seriesIndex | 指定某個系列(option中的series索引)循環顯示tooltip,當loopSeries爲true時,從seriesIndex系列開始執行. | 默認爲0|
實例代碼
```js
function drawSensitiveFile() {
let myChart = echarts.init(document.getElementById('sensitive-file'));
let option = {
title: {
text: '敏感文件分佈分析',
x: '40',
textStyle: {
color: '#fff'
}
},
tooltip: {
trigger: 'item',
formatter: "{a} <br/>{b} : {c} ({d}%)",
},
legend: {
type: 'scroll',
orient: 'vertical',
right: 10,
top: 20,
bottom: 20,
data: ['人事類', '研發類', '營銷類', '客戶信息類'],
textStyle: {
color: '#fff'
}
},
series: [
{
name: '敏感文件分佈數量',
type: 'pie',
radius: '55%',
center: ['50%', '60%'],
data: [
{value: 335, name: '人事類'},
{value: 310, name: '研發類'},
{value: 234, name: '營銷類'},
{value: 1548, name: '客戶信息類'}
],
itemStyle: {
emphasis: {
shadowBlur: 10,
shadowOffsetX: 0,
shadowColor: 'rgba(0, 0, 0, 0.5)'
}
},
labelLine: {
normal: {
//length:5, // 改變標示線的長度
lineStyle: {
color: "#fff" // 改變標示線的顏色
}
},
},
label: {
normal: {
textStyle: {
color: '#fff' // 改變標示文字的顏色
}
}
},
}
]
};
myChart.setOption(option);
tools.loopShowTooltip(myChart, option, {loopSeries: true});
}
```