簡體   English   中英

獲取通過其鏈接連接的所有節點

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

是否有人已經知道如何獲取所有通過其鏈接連接的節點。

這是我目前的代碼,只能在一個級別上使用:

 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;
      }
  });

我的jsfiddle 節點突出顯示

我的功能通過鏈接進行迭代

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}));
  }
}
}

好的,我在這個jsfiddle中有一個效率不高的示例。 您應該可以將其應用於小提琴。

重要的代碼在這里:

.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);
          }
        })
      }
    })
  })

讓我們逐步進行一下。

  1. 我們將填充樣式應用於當前節點。
  2. 然后,我們找到從當前節點開始的鏈接。
  3. 然后,我們找到所有從這些鏈接結束的節點
  4. 我們為那些節點調用此函數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM