且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-08-30 16:18:58

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

Merging these two examples is straightforward, and I created a JSFiddle to demo. First, add the definition of the arrow style to the 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");

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

Then just add the marker to your links

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

你最终会得到这样的结果:

You end up with something like this:

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

You'll see that some arrows are bigger than others, because not all links have the same stroke-width. If you want to make all the arrows the same size, just modify

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

添加链接时.