D3.js 在节点选择上突出显示链接

Lor*_*a L 3 highlight hyperlink d3.js

所以首先,我的数据格式与下面的 Miserables.json 相同\n https://bl.ocks.org/mbostock/4062045

\n\n

我希望能够在单击节点时将连接到节点的链接设为红色。\n因此 psuedo\xe2\x81\xbbcode 如下

\n\n
selectAll(.links)\nif links.source=nodeID or links.target=nodeID\nthen links.color=red\n
Run Code Online (Sandbox Code Playgroud)\n\n

但我还做不到。我的最终目标是将其与下面的弧图集成\n http://bl.ocks.org/sjengle/5431779

\n

tar*_*len 5

你的伪代码是一个好的开始。您可以使用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")
Run Code Online (Sandbox Code Playgroud)