echarts点击click多次触发,绑定事件多次触发

解决办法:在每次绑定事件前加上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')
});
  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值