最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Introducing Arrow(directed), in Force Directed Graph d3 - Stack Overflow

programmeradmin0浏览0评论

I am using the force-directed graph in the sample here -

But since my data is directed, I need the links in the graph to be represented as arrow connections. Maybe like in, .

Can someone please suggest the alterations or additions that create a directed graph as in

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

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.

Share Improve this question edited Jan 20, 2015 at 16:49 mdml 22.9k8 gold badges60 silver badges66 bronze badges asked Jan 20, 2015 at 16:29 minochaminocha 1,0441 gold badge12 silver badges26 bronze badges 2
  • 2 Maybe you find it easier to use a library I created: jsnetworkx.org/examples.html#example1 (it uses d3). – Felix Kling Commented Jan 20, 2015 at 16:32
  • @FelixKling This is wonderful ! I think I will try this out now. – minocha Commented Jan 20, 2015 at 16:44
Add a comment  | 

1 Answer 1

Reset to default 17

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:

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.

发布评论

评论列表(0)

  1. 暂无评论