javascript - 在 Force Directed Graph d3 中引入 Arrow(directed)

标签 javascript d3.js force-layout

我在这里的示例中使用力导向图 - http://bl.ocks.org/mbostock/4062045

但是由于我的数据是定向的,所以我需要图表中的链接表示为箭头连接。也许喜欢,http://bl.ocks.org/d3noob/5141278 .

有人可以建议创建有向图的更改或添加,如 http://bl.ocks.org/mbostock/4062045

我是 D3 的新手,我找不到解决方案,也许是微不足道的,但我很感激能提供一点帮助。

最佳答案

合并这两个示例很简单,我创建了一个 JSFiddle to demo .首先,将箭头样式的定义添加到 SVG 中:

// build the arrow.
svg.append("svg:defs").selectAll("marker")
    .data(["end"])      // Different link/path types can be defined here
  .enter().append("svg:marker")    // This section adds in the arrows
    .attr("id", String)
    .attr("viewBox", "0 -5 10 10")
    .attr("refX", 15)
    .attr("refY", -1.5)
    .attr("markerWidth", 6)
    .attr("markerHeight", 6)
    .attr("orient", "auto")
  .append("svg:path")
    .attr("d", "M0,-5L10,0L0,5");

然后只需将标记添加到您的链接

.attr("marker-end", "url(#end)");

你最终得到这样的结果:

enter image description here

您会看到有些箭头比其他箭头大,因为并非所有链接都具有相同的 stroke-width。如果想让所有的箭头都一样大,修改一下就可以了

.style("stroke-width", function(d) { return Math.sqrt(d.value); })

添加链接时。

关于javascript - 在 Force Directed Graph d3 中引入 Arrow(directed),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28050434/

相关文章:

arrays - d3 数组中缺少第一个值

d3.js - 如何在 D3 强制布局中捕获击键事件?

d3.js - 如何在强制有向图中创建多个属性的缓慢同时转换?

javascript - 使用 d3 和 topojson 绘制 map

javascript - D3 强制布局 : Fit everything on a page?

javascript - 无需点击即可启动 Angular 函数?

javascript - react : How to use setState inside functional component?

javascript - 在 angular.js 中查询并返回 JSON 文件中的数据

javascript - 关于node.js/javascript在文件之间共享变量

javascript - D3 cartogram.js 未反射(reflect)连续的制图值