简体   繁体   English

获取通过其链接连接的所有节点

[英]get all nodes that is connected through their links

Does anybody already know how to get all the nodes that is connected through their links. 是否有人已经知道如何获取所有通过其链接连接的节点。

Here is my current code that works only in one level: 这是我目前的代码,只能在一个级别上使用:

 var connectedLinks = [];
.on('mouseover', function(d) {   
  link.style('stroke-width', function(l) {
        if (d === l.source || d === l.target) {
          connectedLinks.push({type: 'direct', s: l.source.index, t: l.target.index});
        return 5;
      } else {
         connectedLinks.push({type: 'indirect', s: l.source.index, t: l.target.index});
        return 4;
      }
  });

My jsfiddle Nodes Highlighting 我的jsfiddle 节点突出显示

My function to iterate through links 我的功能通过链接进行迭代

function checkIfLinkIsInPipeline(){
 for(var i in connectedLinks){
   if(connectedLinks[i]['type'] == "indirect") {
     console.log("a " + JSON.stringify(connectedLinks[i]));  
     digNeighbor(connectedLinks[i]['s'], connectedLinks[i]['t']);        
   }
 }
}

  function digNeighbor(s, t) {
    for(var j in connectedLinks) {
      if(connectedLinks[j]['type'] == "direct"){
        console.log("b " + JSON.stringify(connectedLinks[j]));  
      }

if(connectedLinks[j]['type'] == "direct" &&
   (connectedLinks[j]['s'] == s ||
    connectedLinks[j]['s'] == t ||
    connectedLinks[j]['t'] == s ||
    connectedLinks[j]['t'] == t)) {
    connectedLinks.push({type: "direct", s: s, t: t});
    console.log("changed to direct " + JSON.stringify({type: "direct", s: s, t: t}));
  }
}
}

Okay, i got an inefficient sample to work in this jsfiddle . 好的,我在这个jsfiddle中有一个效率不高的示例。 You should be able to apply it to your fiddle. 您应该可以将其应用于小提琴。

The important code is here: 重要的代码在这里:

.on('mouseover', function fillBlue(datum, index) {
    console.log('firing for ', this);
    d3.select(this).style("fill", "aliceblue");
    links.forEach(function(link) {
      if (link.source.index === index) {
        svg.selectAll('.node').each(function(d, i) {
          if (i === link.target.index) {
            fillBlue.call(this, d, i);
          }
        })
      }
    })
  })

Lets go through it step by step. 让我们逐步进行一下。

  1. We apply the fill style for our current node. 我们将填充样式应用于当前节点。
  2. We then find the links that start from our current node. 然后,我们找到从当前节点开始的链接。
  3. We then find all the nodes that end from those links 然后,我们找到所有从这些链接结束的节点
  4. We call this function for those nodes. 我们为那些节点调用此函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM