在Force Directed Graph d3中引入Arrow(定向)

min*_*cha 9 javascript d3.js force-layout

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

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

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

我是D3的新手,我找不到解决方案,也许是微不足道的,但是有点帮助是值得赞赏的.

mdm*_*dml 15

合并这两个例子非常简单,我创建了一个JSFiddle来演示.首先,将箭头样式的定义添加到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");
Run Code Online (Sandbox Code Playgroud)

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

.attr("marker-end", "url(#end)");
Run Code Online (Sandbox Code Playgroud)

你最终得到这样的东西:

在此输入图像描述

你会看到一些箭头比其他箭头大,因为并非所有的链接都相同stroke-width.如果要使所有箭头的大小相同,只需修改即可

.style("stroke-width", function(d) { return Math.sqrt(d.value); })
Run Code Online (Sandbox Code Playgroud)

添加链接时.