2014-12-05 66 views
0

我已经使用这个帮助突出neighour节点:如何在D3强制布局中突出显示/选择邻居的邻居?

http://jsfiddle.net/simonraper/jz2AU/light/

var toggle = 0;//Toggle stores whether the highlighting is on 
var linkedByIndex = {};//Create an array logging what is connected to what 

for (i = 0; i < network.network.data.nodes.length; i++) //-populate the array 
{ 
    linkedByIndex[i + "," + i] = 1; 
}; 
network.network.data.edges.forEach(function (d) //-checks what nodes are related in array 
{ 
    linkedByIndex[d.source.index + "," + d.target.index] = 1; 
}); 
//-------------------------check if nodes are linked 
function neighboring(a, b) //This function looks up whether a pair are neighbours 
{ 
    return linkedByIndex[a.index + "," + b.index]; 
} 
//-------------------------finds out connected nodes, keeps their styles but changes the opacity of every other 
function connectedNodes() { 
//Reduce the opacity of all but the neighbouring nodes 
d = d3.select(this).node().__data__; 
    if (toggle == 0) { 

     // nodes.classed("highlighted", function (o) { 
      // return neighboring(d, o) | neighboring(o, d) ? true : false; 
     // });  

     nodes.style("opacity", function (o) { 
      return neighboring(d, o) | neighboring(o, d) ? 1 : 0.1; 
     }); 
     links.style("opacity", function (o) { 
      return d.index==o.source.index | d.index==o.target.index ? 1 : 0.1; 
     }); 
     //Reduce the op 
     toggle = 1; 
    } else { 
     //Put them back to opacity=1 
     nodes.style("opacity", 1); 
     links.style("opacity", 1); 
     nodes.classed("highlighted", false); 
     toggle = 0; 
    } 
} 

这样做是什么,当一个节点上双击,即选择的节点和它的邻国保持其不透明度,而所有其他人低他们的不透明度。

现在,当我双击第一个选定节点的一个子节点时,整个选择消失(所有节点的不透明度为1)。

我想要的是,当我双击其中一个子节点时,选择不会消失,该子节点的相关节点现在变为“高亮显示”。

这样做会有助于指导我轻松使用力指向图,尤其是对于大量数据。

回答

0

出现了这一个小时,它的一个简单改变到代码

更改都切换到试图对 - 肘节= 0;

这样当选择一个节点时,它不会清除选择,它只会突出显示邻居