echarts使用tree悬浮高亮树枝

在这里插入图片描述

监听悬浮事件,echarts中dataIndex是可以作为标识的,如果使用id配合层级,则可能出现不同组件控制相同的页面,此时悬浮就会有问题。

/**
 * 处理悬浮高亮
 * @param {Array} chartData
 * @param {Array} prePath
 * @param {Boolean} isLast 当前的最后一个
 */
const handleLinks = (chartData, prePath, isLast = false) => {
  // eslint-disable-next-line consistent-return
  chartData.forEach(node => {
    if (isLast) {
      node.lineStyle.color = '#FFF';
    } else if (dataIndex === prePath[next]) {
      next++;
      node.lineStyle.color = '#FFF';
    } else {
      node.lineStyle.color = '#222';
    }
    dataIndex++;
    if (node.children && node.children.length) {
      // 确认为命中最后一个
      if (next === prePath.length && dataIndex - 1 === prePath[next - 1])
        handleLinks(node.children, prePath, true);
      else handleLinks(node.children, prePath, isLast);
    }
  });
};
// 鼠标悬浮
 myChart.on('mouseover', (params) => {
    const { treeAncestors } = params;
    const prePathIndexs = [];
    treeAncestors.forEach(item => {
      // 由于数据结构后续使用从0开始,但是treeAncestors的根节点会存在默认一个空节点。
      prePathIndexs.push(item.dataIndex - 1);
    })
    // echarts中dataIndex是可以作为标识的,如果使用id配合层级,则可能出现不同组件控制相同的页面,此时悬浮就会有问题。
    // 存在一个系列
    initDataIndex();
    const option = myChart.getOption();
    handleLinks(option.series[0].data, prePathIndexs);
    myChart.setOption(option);
})
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值