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)。
我想要的是,當我雙擊其中一個子節點時,選擇不會消失,該子節點的相關節點現在變爲「高亮顯示」。
這樣做會有助於指導我輕鬆使用力指向圖,尤其是對於大量數據。