所以首先,我的数据格式与下面的miserables.json相同 https://bl.ocks.org/mbostock/4062045
我希望能够在单击节点时将链接连接到我的节点。 所以伪代码就像
selectAll(.links)
if links.source=nodeID or links.target=nodeID
then links.color=red
但我无法做到。我的最终目标是将它与下面的Arc图集成 http://bl.ocks.org/sjengle/5431779
答案 0 :(得分:3)
在@ laurent的答案之上扩展,以“重置”在之前的交互过程中可能被涂成红色的链接的颜色:
thisNode = nodeObject; // where nodeObject is the javascript object for the node, it's probably called "d" in your function.
d3.selectAll(".link")
.style("stroke",function(d) {
return d.source === thisNode || d.target === thisNode ? "red" : "#888888";
})
答案 1 :(得分:1)
你的伪代码是一个好的开始。您可以使用filter
在选择中实现if条件。请注意,链接的.source
和.target
由d3编辑,它们不再是节点的ID,而是节点本身:
thisNode = nodeObject; // where nodeObject is the javascript object for the node, it's probably called "d" in your function.
d3.selectAll(".link")
.filter(function(d) {
return (d.source === thisNode) || (d.target === thisNode);
})
.style("stroke", "red")