Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
389 views
in Technique[技术] by (71.8m points)

javascript - 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 connections. Maybe like in, http://bl.ocks.org/d3noob/5141278.

Can someone please suggest the alterations or additions that create a directed graph as in http://bl.ocks.org/mbostock/4062045

I am new to D3, and I couldn't find a solution, maybe its trivial, but a little help is appreciated.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

when adding the links.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...