I have a force directed graph with the ability to double click on a node and only that node and its neighbors will be shown, the rest given a hidden class -> visiblity:hidden
But this only works for one node. I have the ability to select multiple nodes and give them a selectedNode class.
Now what I wish to happen is to use this neighboring algorithm on all nodes with the class selectedNode. This is so all selected nodes and the edges connecting them are still shown and the unselected nodes and edges will be hidden.
Here is how I am showing/hiding the edges. Again, this only works for one node d3.select(this).node().__data__;
. I have tried d = d3.selectAll("selectedNode").data();
but no luck :(
var linkedByIndex = {};//Create an array logging what is connected to what
for (i = 0; i < graph.data.nodes.length; i++) //-populate the array
{
linkedByIndex[i + "," + i] = 1;
};
graph.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];
}
d = d3.select(this).node().__data__;
links.classed("hidden", function (o) {
return d.index==o.source.index | d.index==o.target.index ? false : true;
});
Added code
var links = inner.selectAll(".link").append("g")
.data(force.links())
.enter().append("line")
.attr("id", "links")
.attr("class", "link")
.attr("x1", function(d) { return d.source.x; })
.attr("y1", function(d) { return d.source.y; })
.attr("x2", function(d) { return d.target.x; })
.attr("y2", function(d) { return d.target.y; })
.style("marker-end", "url(#arrow)") //adds arrows, main code on SVG append towards bottom
.style("stroke-width", lineWidth)
;