echarts map地图自动高亮 轮播高亮状态,鼠标移入取消轮播

该博客介绍了如何利用Echarts库在Vue项目中实现地图的自动轮播高亮效果,并处理鼠标移入和移出时的地图交互。通过dispatchAction方法控制地图的高亮状态,结合mouseover和mouseout事件,确保鼠标悬停时不会干扰自动轮播。在组件销毁前,正确清理定时器避免资源浪费。
摘要由CSDN通过智能技术生成

用dispatchAction修改地图当前状态,绘制地图后调用myChartChange

....
      this.myChart = echarts.init(document.getElementById("hzChart"));
      this.myChart.setOption(option);
      this.myChartChange();
},
myChartChange() {
      clearInterval(this.toolTipTimers);
      let index = this.index;
      this.toolTipTimers = setInterval(() => {
      // downplay取消上一个
        this.myChart.dispatchAction({
          type: "downplay",
          seriesIndex: 0,
          dataIndex: index == 0 ? this.mapData.length - 1 : index - 1, // 取消高亮
        });
        // highlight高亮下一个
        this.myChart.dispatchAction({
          type: "highlight",
          seriesIndex: 0,
          dataIndex: index, // 第几高亮
        });
        //showTip展示相应提示框
        this.myChart.dispatchAction({
          type: "showTip", // 提示框
          seriesIndex: 0,
          name: this.mapData[index]?.name, // 第几高亮
        });
        //如果是最后一个了就从头开始
        if (index == this.mapData.length - 1) index = 0;
        else index++;
        this.index = index;
      }, 3000);
    },

以上可以自动轮播地图高亮,但是鼠标移入后会出现两个高亮,所以要在鼠标移入后取消以上的操作
主要用mouseover和mouseout事件配合

 // 鼠标移入的时候 取消高亮 并且取消定时器
     this.myChart.on("mouseover", (params) => {
        this.myChart.dispatchAction({
          type: "downplay",
          seriesIndex: 0,
          dataIndex: this.index == 0 ? this.mapData.length - 1 : this.index - 1, // 取消高亮
        });
        clearInterval(this.toolTipTimers);
      });
      
      //鼠标移出继续轮播高亮
      this.myChart.on("mouseout", (params) => {
        this.myChartChange();
      });

提示:离开页面取消定时器

beforeDestroy() {
    if (this.toolTipTimers) {
      clearInterval(this.toolTipTimers); // 在Vue实例销毁前,清除我们的定时器
    }
  },
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值