ECharts一个基于 JavaScript开源可视化库,用于创建各种交互式图表地图。ECharts 支持多种交互方式,包括点击事件高亮事件

 点击事件可以用户单击图表中的元素触发例如单击个数据点或者某个 Legend 图例项时。下面是一个使用 ECharts 点击事件示例代码

myChart.on('click', function (params) {
    console.log(params);
});

上面的代码中,myChart 是 ECharts 实例名称on 方法是 ECharts 提供的绑定事件方法。当用户单击图表中的元素时,该方法会自动触发回调函数,并传递一个参数 params,该参数包含用户点击的元素的相关信息例如数据值、坐标等。

 高亮事件可以在用户将鼠标悬停在图表中的元素上时触发,例如鼠标悬停在某个数据点或者某个 Legend 图例项上时。下面是一个使用 ECharts 高亮事件示例代码

myChart.on('mouseover', 'series', function (params) {
    myChart.dispatchAction({
        type: 'highlight',
        seriesIndex: params.seriesIndex,
        dataIndex: params.dataIndex
    });
});

上面的代码中,使用 dispatchAction 方法来触发高亮操作,使得用户悬停的元素被高亮显示。 

需求点击饼图,点击部分高亮(最多只会有一块区域是处于高亮状态)。并将数据传递下拉框点击选中下拉框某个值,并将数据传递给饼图。实现数据的上钻和下钻。

代码实现部分代码):

1.点击饼图

  this.myChart.off("click"); //点击前先取消之前的点击事件
      //   echartsthis指向问题
      // echarts点击事件里的this指向的是echarts,但我们需要的是vue实例里的数据,因此不改变this指向是取不到值的
      let that = this;
      that.myChart.on("click", function (params) {
        console.log(params.dataIndex, "pppppppp");
        // 数据下钻
        that.value1 = params.dataIndex;
        // 需求:饼图只能有一块是高亮的,点击前找到一个高亮的就把他还原
        let _index = that.list.findIndex((item) => {
          return item.hightLight === true;
        });
        if (_index > -1) {
          that.myChart.dispatchAction({
            type: "downplay",
            seriesIndex: 0,
            dataIndex: _index,
          });
        }
        that.list.forEach((item, index) => {
          if (item.id === params.dataIndex) {
            if (item.hightLight === true) {
              (item.hightLight = false),
                that.myChart.dispatchAction({
                  type: "downplay",
                  seriesIndex: 0,
                  dataIndex: params.dataIndex,
                });
            } else {
              (item.hightLight = true),
                that.myChart.dispatchAction({
                  type: "highlight",
                  seriesIndex: 0,
                  dataIndex: params.dataIndex,
                });
            }
          } else {
            item.hightLight = false;
          }
        });
      });

2.点击选择器

this.list.forEach((item, index) => {
        let _index = this.list.findIndex((item) => {
          return item.hightLight === true;
        });
        if (_index > -1) {
          this.list[_index].hightLight = false;
          this.myChart.dispatchAction({
            type: "downplay",
            seriesIndex: 0,
            dataIndex: _index,
          });
        }
        if (item.hightLight === false) {
          this.list[this.value1].hightLight = true;
          this.myChart.dispatchAction({
            type: "highlight",
            seriesIndex: 0,
            dataIndex: this.value1,
          });
        }
      });

需要注意的点: echarts中this指向的问题

在 ECharts 中,this 的指向会根据不同上下文发生变化。在事件处理函数中,this 会指向当前被触发事件的组件实例。而在一般的 JavaScript 代码中,this 则指向调用当前函数对象

解决的方法:

方法一:如果需要在事件处理函数使用外部变量或方法,可以使用闭包或者使用 ES6 的箭头函数解决上下文问题箭头函数没有自己this,所以在箭头函数使用 this 时,其指向与外层作用域相同

方法二:如果使用普通函数,则需要在事件处理函数内部保存外部this。如上图

最终的效果

饼图点击联动

原文地址:https://blog.csdn.net/m0_66697014/article/details/129108144

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_41902.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注