背景:最近做了很多用【echarts】组件写图表的需求,然后需要自动轮播toolTip,以及鼠标移入自动轮播停止,鼠标移出,自动轮播开始等需求。下面是方法的代码:
(function () { let myChart = 开发者根据自己上下文赋值; // 图表 let categoryLength = 开发者根据自己上下文赋值; // 类别数量,类似于图表X轴数据的个数长度 let timers = null; let pos = 0; const initTimer = () => { if (timers) { clearInterval(timers); } timers = setInterval(() => { let seriesIndex = 0; if (pos > 0) { myChart.dispatchAction({ type: 'downplay', seriesIndex: seriesIndex, dataIndex: pos - 1, }); } else { myChart.dispatchAction({ type: 'downplay', seriesIndex: seriesIndex, dataIndex: categoryLength - 1, }); } // 多个系列如果数据为0则循环不到,所以处理一下 let optionSeries = myChart.getOption().series; if (optionSeries.length > 1 && !optionSeries[seriesIndex].data[pos]) { let a = optionSeries.map(one => one.data); let b = []; a.forEach(list => { list.forEach((one, i) => { if (!b[i]) { b[i] = []; } b[i].push(one); }); }); seriesIndex = b[pos].findIndex(one => one) || 0; } myChart.dispatchAction({ type: 'highlight', seriesIndex: seriesIndex, dataIndex: pos, }); // tooltip 跟随显示 myChart.dispatchAction({ type: 'showTip', seriesIndex: seriesIndex, dataIndex: pos, }); pos++; if (pos > categoryLength - 1) { pos = 0; } }, 2000); }; const autoShowTip = () => { initTimer(); }; const stopShowTip = () => { clearInterval(timers); }; // 鼠标悬浮上去时自动轮播停止 let zRender = myChart.getZr(); myChart.on('mouseover', function () { stopShowTip(); }); myChart.on('mouseout', function () { autoShowTip(); }); zRender.on('mousemove', () => { stopShowTip(); }); zRender.on('globalout', () => { autoShowTip(); }); })();
标签:轮播,pos,toolTip,myChart,let,timers,seriesIndex,echarts From: https://www.cnblogs.com/grow-up-up/p/16792325.html