【问题标题】:Introducing Arrow(directed), in Force Directed Graph d3在 Force Directed Graph d3 中引入 Arrow(directed)
【发布时间】:2015-03-18 23:33:41
【问题描述】:

我在此处的示例中使用力导向图 - http://bl.ocks.org/mbostock/4062045

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

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

我是 D3 的新手,我找不到解决方案,也许它微不足道,但我很感激你的帮助。

【问题讨论】:

标签: javascript d3.js force-layout


【解决方案1】:

合并这两个示例很简单,我创建了一个JSFiddle to demo。首先,将箭头样式的定义添加到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");

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

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

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

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

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

添加链接时。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-18
    • 2017-10-10
    • 2012-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-28
    • 2020-08-07
    相关资源
    最近更新 更多