Vue在echarts?tooltip中添加點(diǎn)擊事件案例詳解
需求
需要在echarts tooltip點(diǎn)擊學(xué)校的名稱,跳轉(zhuǎn)到詳情頁面;項(xiàng)目是從上海市---> 某個(gè)區(qū)----> 具體的學(xué)校(在最后一級(jí)的tooltip中綁定一個(gè)點(diǎn)擊事件)




?項(xiàng)目是用vue和echarts實(shí)現(xiàn)的,echarts是新版本(^5.0.2),并不能把點(diǎn)擊事件綁定在window上
解決方法
1、設(shè)置tooltip
enterable: true, //允許鼠標(biāo)進(jìn)入提示懸浮層中,triggeron:'click',//提示框觸發(fā)的條件? mousemove鼠標(biāo)移動(dòng)時(shí)觸發(fā) click鼠標(biāo)點(diǎn)擊時(shí)觸發(fā)? 'mousemove|click'同時(shí)鼠標(biāo)移動(dòng)和點(diǎn)擊時(shí)觸發(fā)
tooltip: {
// 提示框組件
show: true, // 顯示提示框組件
trigger: "item", // 觸發(fā)類型
triggerOn: "mousemove", // 出發(fā)條件
// formatter: "名稱:<br/>坐標(biāo):{c}",
enterable: true, //允許鼠標(biāo)進(jìn)入提示懸浮層中
showContent: true,
triggerOn: "click", //提示框觸發(fā)的條件 mousemove鼠標(biāo)移動(dòng)時(shí)觸發(fā) click鼠標(biāo)點(diǎn)擊時(shí)觸發(fā) 'mousemove|click'同時(shí)鼠標(biāo)移動(dòng)和點(diǎn)擊時(shí)觸發(fā)
// confine: true, //把toolTip限制在圖表的區(qū)域內(nèi)
className: "areaTool",
// hideDelay: 100000, //延時(shí)消失時(shí)間
formatter: (item) => {
this.hookToolTip = item;
// 經(jīng)緯度太長需要對(duì)位數(shù)進(jìn)行截取顯示,保留七位小數(shù)
// 需要綁定點(diǎn)擊事件
var tipHtml = "";
tipHtml =
'<div style="width:2.5rem;height:80%;background:rgba(22,80,158,0.8);border:0.0125rem solid rgba(7,166,255,0.7)">' +
'<div style="width:100%;height:0.375rem;line-height:0.375rem;border-bottom:0.025rem solid rgba(7,166,255,0.7);">' +
'<i style="display:inline-block;width:0.125rem;height:0.125rem;background:#16d6ff;border-radius:0.5rem;">' +
"</i>" +
'<span id="btn-tooltip" style="margin-left:0.125rem;color:#fff;font-size:0.2rem;cursor:pointer">' +
item.name +
"</span>" +
"</div>" +
'<div style="padding:0.1875rem;text-align: left;">' +
'<p style="color:#fff;font-size:0.15rem;">' +
'<i style="display:inline-block;width:0.1rem;height:0.1rem;background:#16d6ff;border-radius:0.5rem;margin:0 0.1rem">' +
"</i>" +
"經(jīng)度" +
'<span style="color:#11ee7d;margin:0 0.075rem;">' +
item.value[0].substr(0, 11) +
"</span>" +
"</p>" +
'<p style="color:#fff;font-size:0.15rem;">' +
'<i style="display:inline-block;width:0.1rem;height:0.1rem;background:#16d6ff;border-radius:0.5rem;margin:0 0.1rem">' +
"</i>" +
"緯度" +
'<span style="color:#11ee7d;margin:0 0.075rem;">' +
item.value[1].substr(0, 11) +
"</span>" +
"</p>" +
'<p style="color:#fff;font-size:0.15rem;">' +
'<i style="display:inline-block;width:0.1rem;height:0.1rem;background:#16d6ff;border-radius:0.5rem;margin:0 0.1rem">' +
"</i>" +
"考場數(shù)" +
'<span style="color:#11ee7d;margin:0 0.075rem;">' +
item.componentIndex +
"</span>" +
"個(gè)" +
"</p>" +
'<p style="color:#fff;font-size:0.15rem;">' +
'<i style="display:inline-block;width:0.1rem;height:0.1rem;background:#16d6ff;border-radius:0.5rem;margin:0 0.1rem">' +
"</i>" +
"監(jiān)考教師" +
'<span style="color:#11ee7d;margin:0 0.075rem;">' +
item.componentIndex +
"</span>" +
"個(gè)" +
"</p>";
return tipHtml;
},
},
2、定義hookToolTip變量
在formatter中給hookToolTip賦值,添加一個(gè)id,然后通過watch去檢測dom元素,可以通過onclick去綁定事件,也可以通過addEventListerner去注冊事件



watch: {
hookToolTip: {
handler(newVal, oldVal) {
console.log(newVal, oldVal, "---------watch");
let tooltipButton = document.querySelectorAll("#btn-tooltip");
//通過onclick注冊事件 querySelectorAll獲取的元素是一個(gè)數(shù)組
if (tooltipButton.length > 0) {
tooltipButton[0].onclick = this.pointNameClick;
}
// 通過addEventListener注冊事件
for (let i = 0; i < tooltipButton.length; i++) {
tooltipButton[i].addEventListener("click", this.chartClick);
}
},
// immediate: true,
// deep: true,
},
},
3、在methods中添加方法

4、完整代碼
data(){
hookToolTip: {},
},
watch: {
hookToolTip: {
handler(newVal, oldVal) {
console.log(newVal, oldVal, "---------watch");
let tooltipButton = document.querySelectorAll("#btn-tooltip");
//通過onclick注冊事件 querySelectorAll獲取的元素是一個(gè)數(shù)組
if (tooltipButton.length > 0) {
tooltipButton[0].onclick = this.pointNameClick;
}
// 通過addEventListener注冊事件
for (let i = 0; i < tooltipButton.length; i++) {
tooltipButton[i].addEventListener("click", this.chartClick);
}
},
//并不需要進(jìn)入頁面就檢查
// immediate: true,
// deep: true,
},
},
methods: {
chartClick() {
console.log(
this.hookToolTip,
"-------addEventList",
this.hookToolTip.name
);
},
},
//echarts
tooltip: {
// 提示框組件
show: true, // 顯示提示框組件
trigger: "item", // 觸發(fā)類型
triggerOn: "mousemove", // 出發(fā)條件
// formatter: "名稱:<br/>坐標(biāo):{c}",
enterable: true, //允許鼠標(biāo)進(jìn)入提示懸浮層中
showContent: true,
triggerOn: "click", //提示框觸發(fā)的條件 mousemove鼠標(biāo)移動(dòng)時(shí)觸發(fā) click鼠標(biāo)點(diǎn)擊時(shí)觸發(fā) 'mousemove|click'同時(shí)鼠標(biāo)移動(dòng)和點(diǎn)擊時(shí)觸發(fā)
// confine: true, //把toolTip限制在圖表的區(qū)域內(nèi)
className: "areaTool",
// hideDelay: 100000, //延時(shí)消失時(shí)間
formatter: (item) => {
this.hookToolTip = item;
console.log(item, "-----", this.hookToolTip);
// 經(jīng)緯度太長需要對(duì)位數(shù)進(jìn)行截取顯示,保留七位小數(shù)
// 需要綁定點(diǎn)擊事件
var tipHtml = "";
tipHtml =
'<div style="width:2.5rem;height:80%;background:rgba(22,80,158,0.8);border:0.0125rem solid rgba(7,166,255,0.7)">' +
'<div style="width:100%;height:0.375rem;line-height:0.375rem;border-bottom:0.025rem solid rgba(7,166,255,0.7);">' +
'<i style="display:inline-block;width:0.125rem;height:0.125rem;background:#16d6ff;border-radius:0.5rem;">' +
"</i>" +
'<span id="btn-tooltip" style="margin-left:0.125rem;color:#fff;font-size:0.2rem;cursor:pointer" onclick="chartClick">' +
item.name +
"</span>" +
"</div>" +
'<div style="padding:0.1875rem;text-align: left;">' +
'<p style="color:#fff;font-size:0.15rem;">' +
'<i style="display:inline-block;width:0.1rem;height:0.1rem;background:#16d6ff;border-radius:0.5rem;margin:0 0.1rem">' +
"</i>" +
"經(jīng)度" +
'<span style="color:#11ee7d;margin:0 0.075rem;">' +
item.value[0].substr(0, 11) +
"</span>" +
"</p>" +
'<p style="color:#fff;font-size:0.15rem;">' +
'<i style="display:inline-block;width:0.1rem;height:0.1rem;background:#16d6ff;border-radius:0.5rem;margin:0 0.1rem">' +
"</i>" +
"緯度" +
'<span style="color:#11ee7d;margin:0 0.075rem;">' +
item.value[1].substr(0, 11) +
"</span>" +
"</p>" +
'<p style="color:#fff;font-size:0.15rem;">' +
'<i style="display:inline-block;width:0.1rem;height:0.1rem;background:#16d6ff;border-radius:0.5rem;margin:0 0.1rem">' +
"</i>" +
"考場數(shù)" +
'<span style="color:#11ee7d;margin:0 0.075rem;">' +
item.componentIndex +
"</span>" +
"個(gè)" +
"</p>" +
'<p style="color:#fff;font-size:0.15rem;">' +
'<i style="display:inline-block;width:0.1rem;height:0.1rem;background:#16d6ff;border-radius:0.5rem;margin:0 0.1rem">' +
"</i>" +
"監(jiān)考教師" +
'<span style="color:#11ee7d;margin:0 0.075rem;">' +
item.componentIndex +
"</span>" +
"個(gè)" +
"</p>";
return tipHtml;
},
},
到此這篇關(guān)于Vue在echarts tooltip中添加點(diǎn)擊事件案例詳解的文章就介紹到這了,更多相關(guān)Vue的內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue中computed、methods與watch的區(qū)別總結(jié)
這篇文章主要給大家介紹了關(guān)于Vue中computed、methods與watch區(qū)別的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Vue具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04
Vue?vant-ui框架實(shí)現(xiàn)上拉加載下拉刷新功能
功能需求——獲取后端接口返回的數(shù)據(jù),實(shí)現(xiàn)列表數(shù)據(jù)上滑加載更多下一頁數(shù)據(jù),下拉數(shù)據(jù)刷新功能,結(jié)合vant-ui框架實(shí)現(xiàn)??芍苯訁⒖际褂?/div> 2022-09-09
Vue組件傳值異步問題子組件拿到數(shù)據(jù)較慢解決
這篇文章主要為大家介紹了Vue組件傳值異步中子組件拿到數(shù)據(jù)較慢的問題解決方法,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08
vue-cli開發(fā)環(huán)境實(shí)現(xiàn)跨域請(qǐng)求的方法
本篇文章主要介紹了vue-cli開發(fā)環(huán)境實(shí)現(xiàn)跨域請(qǐng)求的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-04-04最新評(píng)論

