d3.js - 力有向图中可能有双向箭头吗?

标签 d3.js directed-graph force-layout

我创建了一个类似于此力导向图的图:http://bl.ocks.org/d3noob/5141278 :

enter image description here

以链接为例:Sarah 链接到 James,James 链接到 Sarah。有没有一种方法可以让它只有一个两端都有三角形的箭头,而不是用两个箭头(每个方向一个)使页面变得困惑?

最佳答案

要实现此目的,您所要做的就是向链接添加另一个标记:

var path = svg.append("svg:g").selectAll("path")
  .data(force.links())
  .enter().append("svg:path")
  .attr("class", "link")
  .attr("marker-end", "url(#end)")
  .attr("marker-start", "url(#end");

示例 here 。但这并不完美,因为 SVG spec 的方式指定如何计算箭头指向的方向,特别是:

If there is only a path segment going out of the vertex (e.g., the first vertex on an open path), the marker's positive x-axis should point in the same direction as the tangent vector for the path segment going out of the vertex. (Refer to ‘path’ element implementation notes for a more thorough discussion of the directionality of path segments.)

在这些情况下,您可以自己计算并指定箭头的角度来固定方向。

关于d3.js - 力有向图中可能有双向箭头吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20431163/

相关文章:

algorithm - 给定 "roots"从无向无环图创建有向无环图

javascript - D3.js 在圆环图中的圆弧上放置文本

javascript - D3 折线图上的标签不起作用

python - networkx:在边缘绘制文本

breadth-first-search - 广度优先遍历有向图与无向图

javascript - 向 D3 力定向图中的链接添加标签

javascript - 为什么我的方法无法识别我的对象成员?

javascript - D3.js - 将力定向图节点从圆形更改为 svg :rect

javascript - d3.js 强制折叠标签

javascript - 交互式 d3 邮政编码分区统计表 - WA 州