Skip to content Skip to sidebar Skip to footer

Introducing Arrow(directed), In Force Directed Graph D3

I am using the force-directed graph in the sample here - http://bl.ocks.org/mbostock/4062045 But since my data is directed, I need the links in the graph to be represented as arrow

Solution 1:

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:

enter image description here

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) { returnMath.sqrt(d.value); })

when adding the links.

Post a Comment for "Introducing Arrow(directed), In Force Directed Graph D3"