vue中echarts點擊事件點擊一次多次觸發(fā)問題
前言
vue中使用echarts圖,并且使用他的點擊事件就會發(fā)現(xiàn)一個問題,第一次我echarts圖的點擊事件是生效的而且正常的,但是一旦重新渲染這個echarts圖以后,就會出現(xiàn)再重新渲染一次,(相當于2次渲染echarts圖),點擊事件會被調(diào)用2次,第二次重新渲染,點擊事件就會被調(diào)用3次,這個問題。
問題展示
(我這里是調(diào)用后臺,我的日歷刷新一次時間,就會重新渲染一次我的echarts圖)
正常點擊事件
(前,點擊一次調(diào)用一次后臺)

異常
(當我選了日歷以后,重新渲染echarts圖,再點擊的時候,重新渲染幾次,點擊多幾次)

解決辦法
再渲染echarts圖前加
this.myChart.off('click') // 這里很重要!!解決重復點擊
this.myChart = echarts.init(this.$refs.chart);
this.myChart.off('click') // 這里很重要??!解決重復點擊
this.myChart.setOption({封裝組件源碼
<template>
<div class="echarts" ref="chart"></div>
</template>
<script>
const echarts = require('echarts');
export default {
props:{
data:{//echarts數(shù)據(jù)
type:Array,
default:()=>[]
},
Params:Object,
},
data () {
return {
name:'柱圖',
myChart:null,
};
},
components: {},
mounted() {
this.initCharts(this.data);
},
watch:{
data(val){
this.initCharts(val);
}
},
methods: {
initCharts(data){
if(data.length==0){
return;
}
let unit = this.Params.unit;//單位
/**
* 處理數(shù)據(jù)
*/
// let dataAxis = ['10.24', '10.25', '10.26', '10.27', '10.28', '10.29', '今日'];
// let Ydata = [220, 182, 191, 234, 290, 330, 310];
let dataAxis = [];
let Ydata = [];
data.forEach(item => {
dataAxis.push(item.date);//日期
Ydata.push(item.value);//積分
});
let maxLengthVal = Ydata.length-1;
/**
* 獲取數(shù)據(jù)內(nèi)部最大值,+100來設置背景圖高度
*/
var max = Ydata.reduce( (a,b)=> b > a ? b : a );//獲取最大值
var dataShadow = [];
var yMax;
if(max<100){
yMax = max+30;
}else if(max>100 && max<500){
yMax = max+100;
}else{
yMax = max+200;
}
for (var i = 0; i < Ydata.length; i++) {
dataShadow.push(yMax);
}
this.myChart = echarts.init(this.$refs.chart);
this.myChart.off('click') // 這里很重要??!解決重復點擊
this.myChart.setOption({
xAxis: {
data: dataAxis,
axisLabel: {
inside: true,
textStyle: {
color: '#000',
fontSize:'100%'
}
},
axisTick: {
show: false
},
axisLine: {
show: false
},
position: "top",
z:10
},
yAxis: {
axisLine: {
show: false
},
axisTick: {
show: false
},
axisLabel: {
show: false
},
splitLine:{//網(wǎng)格線
show:false
}
},
dataZoom: [
{
type: 'inside'
}
],
series: [
{ // For shadow
type: 'bar',
itemStyle: {
normal: {color: '#F6FBFE'},
emphasis: {color: 'rgba(255,188,117,.3)'}
},
barGap:'-100%',
barCategoryGap:'40%',
data: dataShadow,
animation: false
},
{
type: 'bar',
label: {
normal: {
show: true,
position: 'top',
color:'#000',
fontSize:'100%',
formatter: function (params) {
return params.value+unit;
},
},
},
itemStyle: {
normal: {
// color: new echarts.graphic.LinearGradient(
// 0, 0, 0, 1,
// [
// {offset: 0, color: '#FF3405'},
// {offset: 0.5, color: '#FF6A47'},
// {offset: 1, color: '#FF9076'}
// ]
// )
color: function(params) {
if(params.dataIndex == maxLengthVal){
return new echarts.graphic.LinearGradient(
0, 0, 0, 1,
[
{offset: 0, color: '#FF3405'},
{offset: 0.5, color: '#FF6A47'},
{offset: 1, color: '#FF9076'}
]
);
};
return new echarts.graphic.LinearGradient(
0, 0, 0, 1,
[
{offset: 0, color: '#FFBD77'},
{offset: 0.5, color: '#FF9F38'},
{offset: 1, color: '#FF8505'}
]
);
}
},
emphasis: {
color: new echarts.graphic.LinearGradient(
0, 0, 0, 1,
[
{offset: 0, color: '#FF3405'},
{offset: 0.5, color: '#FF6A47'},
{offset: 1, color: '#FF9076'}
]
)
}
},
data: Ydata
}
]
})
this.myChart.on('click',(params)=>{
let name = '';
data.forEach(item=>{
if(item.date == params.name){
name = item.dateYear
}
})
this.$emit('echartsClick',name);
});
},
},
}
</script>
<style lang='less' scoped>
.echarts{
width: 100%;
height:100%;
}
</style>總結
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
vue實現(xiàn)兄弟組件之間跳轉(zhuǎn)指定tab標簽頁
這篇文章主要介紹了vue實現(xiàn)兄弟組件之間跳轉(zhuǎn)指定tab標簽頁,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-05-05
vue組件中使用props傳遞數(shù)據(jù)的實例詳解
這篇文章主要介紹了vue組件中使用props傳遞數(shù)據(jù)的實例詳解,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2018-04-04
vite創(chuàng)建一個標準vue3+ts+pinia項目
本文主要介紹了vite創(chuàng)建一個標準vue3+ts+pinia項目,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-05-05
Vue3使用exceljs將excel文件轉(zhuǎn)化為html預覽最佳方案
在企業(yè)應用中,我們時常會遇到需要上傳并展示 Excel 文件的需求,以實現(xiàn)文件內(nèi)容的在線預覽,經(jīng)過一番探索與嘗試,筆者最終借助 exceljs 這一庫成功實現(xiàn)了該功能,本文將以 Vue 3 為例,演示如何實現(xiàn)該功能,需要的朋友可以參考下2025-05-05
vue實現(xiàn)導出Word文件(數(shù)據(jù)流方式)
這篇文章主要介紹了vue實現(xiàn)導出Word文件(數(shù)據(jù)流方式),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-06-06
Vue.js如何利用v-for循環(huán)生成動態(tài)標簽
這篇文章主要給大家介紹了關于Vue.js如何利用v-for循環(huán)生成動態(tài)標簽的相關資料,文中通過實例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2022-01-01

