( function ( global ) { global. tools = global. tools || { } ; tools. loopShowTooltip = function ( chart, chartOption, options ) { var defaultOptions = { interval : 2000 , loopSeries : false , seriesIndex : 0 , updateData : null } ; if ( ! chart || ! chartOption) { return { } ; } var dataIndex = 0 ; var seriesIndex = 0 ; var timeTicket = 0 ; var seriesLen = chartOption. series. length; var dataLen = 0 ; var chartType; var first = true ; if ( options) { options. interval = options. interval || defaultOptions. interval; options. loopSeries = options. loopSeries || defaultOptions. loopSeries; options. seriesIndex = options. seriesIndex || defaultOptions. seriesIndex; options. updateData = options. updateData || defaultOptions. updateData; } else { options = defaultOptions; } if ( options. seriesIndex < 0 || options. seriesIndex >= seriesLen) { seriesIndex = 0 ; } else { seriesIndex = options. seriesIndex; } function autoShowTip ( ) { function showTip ( ) { if ( dataIndex === 0 && ! first && typeof options. updateData === "function" ) { options. updateData ( ) ; chart. setOption ( chartOption) ; } var series = chartOption. series; chartType = series[ seriesIndex] . type; dataLen = series[ seriesIndex] . data. length; var tipParams = { seriesIndex : seriesIndex} ; switch ( chartType) { case 'map' : case 'pie' : case 'chord' : tipParams. name = series[ seriesIndex] . data[ dataIndex] . name; break ; case 'radar' : tipParams. seriesIndex = seriesIndex; tipParams. dataIndex = dataIndex; break ; default : tipParams. dataIndex = dataIndex; break ; } if ( chartType === 'pie' || chartType === 'radar' ) { chart. dispatchAction ( { type : 'downplay' , seriesIndex : options. loopSeries ? ( seriesIndex === 0 ? seriesLen - 1 : seriesIndex - 1 ) : seriesIndex, dataIndex : dataIndex === 0 ? dataLen - 1 : dataIndex - 1 } ) ; chart. dispatchAction ( { type : 'highlight' , seriesIndex : seriesIndex, dataIndex : dataIndex} ) ; } tipParams. type = 'showTip' ; chart. dispatchAction ( tipParams) ; dataIndex = ( dataIndex + 1 ) % dataLen; if ( options. loopSeries && dataIndex === 0 && ! first) { seriesIndex = ( seriesIndex + 1 ) % seriesLen; } first = false ; } showTip ( ) ; timeTicket = setInterval ( showTip, options. interval) ; } function stopAutoShow ( ) { if ( timeTicket) { clearInterval ( timeTicket) ; timeTicket = 0 ; if ( chartType === 'pie' || chartType === 'radar' ) { chart. dispatchAction ( { type : 'downplay' , seriesIndex : options. loopSeries ? ( seriesIndex === 0 ? seriesLen - 1 : seriesIndex - 1 ) : seriesIndex, dataIndex : dataIndex === 0 ? dataLen - 1 : dataIndex - 1 } ) ; } } } var zRender = chart. _zr; function zRenderMouseMove ( param ) { if ( param. event) { param. event. cancelBubble = true ; } stopAutoShow ( ) ; } function zRenderGlobalOut ( ) { if ( ! timeTicket) { autoShowTip ( ) ; } } chart. on ( 'mousemove' , stopAutoShow) ; zRender. on ( 'mousemove' , zRenderMouseMove) ; zRender. on ( 'globalout' , zRenderGlobalOut) ; autoShowTip ( ) ; return { clearLoop : function ( ) { if ( timeTicket) { clearInterval ( timeTicket) ; timeTicket = 0 ; } chart. off ( 'mousemove' , stopAutoShow) ; zRender. off ( 'mousemove' , zRenderMouseMove) ; zRender. off ( 'globalout' , zRenderGlobalOut) ; } } ; } ;
} ) ( window) ;
main.js 引入 echarts-auto.js
import './assets/js/echarts-auto.js'
< v- echarts ref= "chart" > < / v- echarts> this . $refs. chart. setOption ( options) ;
tools. loopShowTooltip ( this . $refs. chart. myChart, options, { loopSeries : true } ) ;