【发布时间】:2014-12-18 12:39:25
【问题描述】:
我有一个力有向图,可以双击一个节点,只显示该节点及其邻居,其余的给定一个隐藏类 -> 可见性:隐藏
但这仅适用于一个节点。我有能力选择多个节点并给他们一个 selectedNode 类。
现在我希望在所有具有 selectedNode 类的节点上使用这种相邻算法。这样所有选定的节点和连接它们的边仍然会显示出来,而未选定的节点和边将被隐藏。
这是我显示/隐藏边缘的方式。同样,这只适用于一个节点d3.select(this).node().__data__;。我试过d = d3.selectAll("selectedNode").data(); 但没有运气:(
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;
});
添加代码
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)
;
【问题讨论】:
-
你能提供一个带有 baerbone 实现的 jsfiddle 吗?目前尚不清楚
this在该范围内指的是什么。 -
这里是基础知识,因为我的代码将近 1000 行长。 jsfiddle.net/reko91/skhhq62n/1。和 Lars 我已经看过了,但这仅适用于一个节点/线。如果可能,我想对多个节点执行此操作?
-
@LarsKotthoff 肯定有一种简单的应用方法:links.classed("hidden", function (o) { return d.index==o.source.index | d.index==o .target.index ? false : true; });到所有选定的节点而不是一个节点?
标签: javascript algorithm d3.js nearest-neighbor