简体   繁体   English

如何在d3图中向边添加标签

[英]How to add label to edges in d3 graph

Please see http://bl.ocks.org/rkirsling/5001347 请参阅http://bl.ocks.org/rkirsling/5001347

It shows some nodes and the edges between them. 它显示了一些节点和它们之间的边缘。 Can you tell what code to add in that and where so that the edges have labels. 你能告诉你要添加哪些代码以及边缘有标签的地方。 You can assume any suitable location for the labels and you can also assume any label text. 您可以为标签假设任何合适的位置,您也可以假设任何标签文本。 Thank you. 谢谢。

You can add labels just as you add the paths for the links themselves. 您可以像添加链接本身的路径一样添加标签。 All you need to do is calculate the position according to the positions of the two nodes the link connects. 您需要做的就是根据链路连接的两个节点的位置计算位置。 The code would look something like this. 代码看起来像这样。

svg.selectAll("text").data(links).enter()
   .append("text")
   .attr("x", function(d) { return d.source.x + (d.target.x - d.source.x)/2; })
   .attr("y", function(d) { return d.source.y + (d.target.y - d.source.y)/2; })
   .text(function(d) { return d.something; });

Note that in your tick function, you would also need to update the position of the labels. 请注意,在tick函数中,您还需要更新标签的位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM