解决办法:在每次绑定事件前加上this.echart.off('click')
就像这样
this.echart.setOption(option);
this.echart.off('click')// 清除事件
this.echart.on('click',(param) => {});
起因
我每次执行一遍echarts的重绘函数后再点击echart就会多执行一次点击事件,比如
initEchart(type){
if(type == 'resize' && this.echart){
this.echart.clear()
this.echart.resize()
} else {
this.echart= echarts.init(this.$refs.echart);
}
let option;
option = {
graphic: {
type: 'text',
left: 'center',
top: 'center',
style: {
text:`${(num * 100).toFixed(1)}%`,
textAlign: 'center',
fill: '#333',
width: 30,
height: 30,
fontWeight: 'bold',
fontSize: 40
}
},
tooltip: {
trigger: 'item'
},
title: {
text: `ecahrt11`,
bottom:'bottom',
left:'center',
},
series: [
{
name: 'ecahrt11',
type: 'pie',
radius: ['40%', '70%'],
avoidLabelOverlap: false,
label: {
show: false,
},
labelLine: {
show: false
},
itemStyle: {
borderRadius: '2%',
borderColor: '#fff',
borderWidth: 2
},
data: [
{ value: 52, name: '数量1', itemStyle: {color: '#70AD47'}},
{ value: 86, name: '数量2', itemStyle: {color: '#FFC000'}},
]
}
]
};
this.echart.setOption(option);
this.echart.on('click',(param) => {
this.EChartClick(param,'echart')
});
console.log('echart')
},
不管是重绘还是初始化 在上面的函数都有进行click 的绑定 ,但是echart的setOption应该是只会清除数据options的数据并不会清除给echart绑定的事件,导致每次重绘后就多绑定一个点击事件
因此要在绑定点击事件前加上this.echart.setOption(option);
,删除当前绑定的点击事件,重新添加
this.echart.on('click',(param) => {
this.EChartClick(param,'echart')
});