echart中tooltip自動(dòng)展示代碼示例
引入js文件
import {autoHover} from ‘./tooltip’
/**
* echarts tooltip輪播
* @param chart ECharts實(shí)例
* @param chartOption echarts的配置信息
* @param options object 選項(xiàng)
* {
* interval 輪播時(shí)間間隔,單位毫秒,默認(rèn)為3000
* true表示循環(huán)所有series的tooltip,false則顯示指定seriesIndex的tooltip
* seriesIndex 默認(rèn)為0,指定某個(gè)系列(option中的series索引)循環(huán)顯示tooltip,
* 當(dāng)loopSeries為true時(shí),從seriesIndex系列開(kāi)始執(zhí)行。
* }
* @returns {{clearLoop: clearLoop}|undefined}
*/
export function autoHover(myChart, option, num, time) {
var defaultData = { // 設(shè)置默認(rèn)值
time: 3000,
num: 14
};
if (!time) {
time = defaultData.time;
}
if (!num) {
num = defaultData.num;
}
var count = 0;
var timeTicket = 0;
// 清除定時(shí)器
function clearLoop() {
if (timeTicket) {
clearInterval(timeTicket);
timeTicket = 0;
}
myChart.off('mousemove', stopAutoShow);
myChart.getZr().off('mousemove', zRenderMouseMove);
// myChart.getZr().off('globalout', zRenderGlobalOut);
}
// 關(guān)閉輪播
function stopAutoShow() {
if (timeTicket) {
clearInterval(timeTicket);
timeTicket = 0;
}
}
function zRenderMouseMove(param) {
if (param.event) {
// 阻止canvas上的鼠標(biāo)移動(dòng)事件冒泡
// param.event.cancelBubble = true;
}
stopAutoShow();
}
timeTicket && clearInterval(timeTicket);
timeTicket = setInterval(function() {
myChart.dispatchAction({
type: 'downplay',
seriesIndex: 0 // serieIndex的索引值 可以觸發(fā)多個(gè)
});
myChart.dispatchAction({
type: 'highlight',
seriesIndex: 0,
dataIndex: count
});
myChart.dispatchAction({
type: 'showTip',
seriesIndex: 0,
dataIndex: count
});
count++;
if (count >= num) {
count = 0;
}
}, 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() {
timeTicket && clearInterval(timeTicket);
timeTicket = setInterval(function() {
myChart.dispatchAction({
type: 'downplay',
seriesIndex: 0 // serieIndex的索引值 可以觸發(fā)多個(gè)
});
myChart.dispatchAction({
type: 'highlight',
seriesIndex: 0,
dataIndex: count
});
myChart.dispatchAction({
type: 'showTip',
seriesIndex: 0,
dataIndex: count
});
count++;
if (count >= 14) {
count = 0;
}
}, 3000);
});
return {
clearLoop: clearLoop
};
}在echart里注冊(cè)使用
myChart.setOption(option, true);
this.tootipTimer && this.tootipTimer.clearLoop(); // this.tootipTimer 在data里定義
this.tootipTimer = 0;
this.tootipTimer = autoHover(myChart, option, 8, 3000);附:echart tooltip自定義
ECharts 提供了 tooltip 的自定義功能,可以通過(guò) formatter 函數(shù)來(lái)自定義 tooltip 的內(nèi)容和樣式。
下面是一個(gè)簡(jiǎn)單的示例:
option = {
// ... 其他配置項(xiàng)
tooltip: {
trigger: 'axis',
axisPointer: {
type: 'cross',
crossStyle: {
color: '#999'
}
},
formatter: function (params) {
// 自定義 tooltip 的內(nèi)容和樣式
return params[0].name + '<br/>' +
params[0].seriesName + ' : ' + params[0].value + '<br/>' +
params[1].seriesName + ' : ' + params[1].value + '<br/>' +
params[2].seriesName + ' : ' + params[2].value + '<br/>' +
params[3].seriesName + ' : ' + params[3].value + '<br/>'
}
},
// ... 其他配置項(xiàng)
series: [
{
name:'郵件營(yíng)銷',
type:'bar',
data:[120, 132, 101, 134, 90, 230, 210]
},
{
name:'聯(lián)盟廣告',
type:'bar',
data:[220, 182, 191, 234, 290, 330, 310]
},
{
name:'視頻廣告',
type:'bar',
data:[150, 232, 201, 154, 190, 330, 410]
},
{
name:'直接訪問(wèn)',
type:'bar',
data:[320, 332, 301, 334, 390, 330, 320]
}
]
};在上面的示例中,通過(guò) formatter 函數(shù)來(lái)自定義 tooltip 的內(nèi)容和樣式。在這個(gè)函數(shù)中,我們可以通過(guò) params 參數(shù)獲取到當(dāng)前 tooltip 中所展示的數(shù)據(jù),從而可以自定義 tooltip 的樣式和展示內(nèi)容。
需要注意的是,formatter 函數(shù)的返回值必須是一個(gè)字符串。在字符串中,可以使用 HTML 標(biāo)簽來(lái)控制 tooltip 的樣式,比如使用 <br/> 來(lái)?yè)Q行。
總結(jié)
到此這篇關(guān)于echart中tooltip自動(dòng)展示的文章就介紹到這了,更多相關(guān)echart tooltip自動(dòng)展示內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
JavaScript解構(gòu)賦值的5個(gè)常見(jiàn)場(chǎng)景與實(shí)例教程
解構(gòu)賦值是一種特殊的語(yǔ)法,它使我們可以將數(shù)組或?qū)ο蟆安鸢睘榈揭幌盗凶兞恐?因?yàn)橛袝r(shí)候使用變量更加方便,下面這篇文章主要給大家介紹了關(guān)于JavaScript解構(gòu)賦值的5個(gè)常見(jiàn)場(chǎng)景與實(shí)例的相關(guān)資料,需要的朋友可以參考下2021-11-11
JavaScript幾種數(shù)組去掉重復(fù)值的方法推薦
下面小編就為大家?guī)?lái)一篇JavaScript幾種數(shù)組去掉重復(fù)值的方法推薦。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看2016-04-04
HTML使用js給input標(biāo)簽增加disabled屬性的方法
最近項(xiàng)目上提出一個(gè)經(jīng)常遇到的需求,點(diǎn)擊新增時(shí)input可輸入,點(diǎn)擊編輯時(shí)input置灰,下面這篇文章主要給大家介紹了關(guān)于HTML使用js給input標(biāo)簽增加disabled屬性的相關(guān)資料,需要的朋友可以參考下2024-06-06
JS創(chuàng)建對(duì)象的十種方式總結(jié)
面向?qū)ο笫且环N重要的編程范式,如何靈活的創(chuàng)建對(duì)象,是對(duì)編程基本功的考驗(yàn),本來(lái)我們來(lái)探討一下JavaScript中創(chuàng)建對(duì)象的十種方式,感興趣的小伙伴可以了解下2023-10-10

