记 echarts 关系图 整条关系高亮效果

需要实现的效果

项目需求是 鼠标移入节点,该节点所在的整条直系关系高亮
focusNodeAdjacency: true的效果是实现相两个节点高亮不满足项目需求的;
翻遍全网看到有个朋友说用使用dispatchAction手动控制高亮

       myChart.on('click', function (params) {
  // 先取消已高亮的节点连线
  myChart.dispatchAction({
    type: 'downplay',
    dataIndex: currentDataIndexs
  })
 
  // 针对非最尾节点点击收缩展开后已高亮线路失效
  if (params.data.children) {
    myChart.dispatchAction({
      type: 'highlight',
      dataIndex: currentDataIndexs
    })
    return
  }
 
  const treeAncestors = params.treeAncestors //但是我,params没有treeAncestors,只能拿到一个dataIndex,解决办法是 myChart.getModel().getSeriesByIndex(0).getData().graph.edges所有的链接线,然后通过里边的id过滤出需要高亮的线,取出dataIndex
  const dataIndexs = treeAncestors.map((item) => item.dataIndex)
  // 重新保存当前高亮的节点
  currentDataIndexs = dataIndexs
 
  // 高亮相关节点连线
  myChart.dispatchAction({
    type: 'highlight',
    dataIndex: dataIndexs
  })
})

这方法我后面仅仅实现了所以的点高亮,线没有高亮

有了上面的思路,后面退而求次,选择采用双击,重新绘制整条关系图,再次双即绘制完整关系图

   //  originNodes  全量节点
   // originEdges  全量线
myChart.on("dblclick", (params) => {
            console.log("移入id----", params);

            if (isClick) { // true表示已经点击,再次点击就绘全量的关系图
                option.series[0].data = originNodes 
                option.series[0].links = originEdges

                myChart.setOption(option)

                isClick = false

                return;

            }

            isClick = true

            const paramsId = params.data.name;

            if (!paramsId) return;

            const findEdges = []

            // 寻找上级
            const superIds = [paramsId];
            const superObjs = [false];

            function findSuperIds(nodesIds) {
                for (let i = 0; i < nodesIds.length; i++) {
                    if (!superObjs[i]) {
                        const id = nodesIds[i];
                        for (let j = 0; j < originEdges.length; j++) {
                            const edges = originEdges[j];
                            if (edges.target === id && nodesIds.indexOf(edges.source) === -1) {
                                nodesIds.push(edges.source);
                                superObjs.push(false);
                                findEdges.push(edges)
                            }
                        }
                        superObjs[i] = true;
                    }
                }
            }

            findSuperIds(superIds);

            // 寻找子级
            const subIds = [paramsId];
            const subObjs = [false];

            function findSubIds(nodesIds) {
                for (let i = 0; i < nodesIds.length; i++) {
                    if (!subObjs[i]) {
                        const id = nodesIds[i];
                        for (let j = 0; j < originEdges.length; j++) {
                            const edges = originEdges[j];
                            if (edges.source === id && nodesIds.indexOf(edges.target) === -1) {
                                nodesIds.push(edges.target);
                                subObjs.push(false);
                                findEdges.push(edges)
                            }
                        }
                        subObjs[i] = true;
                    }
                }
            }

            findSubIds(subIds);

            const allIds = new Set([...superIds, ...subIds]);
            const ids = Array.from(allIds);

            const nodeIndexList = [];
            for (let i = 0, len = originNodes.length; i < len; i++) {
                const node = originNodes[i];
                if (ids.includes(node.name)) {
                    nodeIndexList.push(node);
                }
            }
            console.log("所有的node---", nodeIndexList, findEdges);
            option.series[0].data = nodeIndexList
            option.series[0].links = findEdges
            myChart.setOption(option)
        });

在这里插入图片描述
最终实现效果如上图

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值