简体   繁体   English

D3.js中的可折叠树中的链接

[英]LINKS IN Collapsible Tree in D3.js

I am new to D3 so I want to figure out some of the following things in this tree layout example( http://bl.ocks.org/mbostock/4339083 ) . 我是D3的新手,所以我想在这个树布局示例中找出以下一些内容( http://bl.ocks.org/mbostock/4339083 )。

  1. How can I add Names to Links(edges) that is if there is a link from A to B(B is child of A) then show the name of B on link. 如何将链接(边缘)添加到链接(边缘),如果存在从A到B的链接(B是A的子节点),则在链接上显示B的名称。
  2. How can I change the width of the link based on the size given in child, that is if there is a link from A to B and size of B is 50 then the link's width should be 50px. 如何根据子节点中给出的大小更改链接的宽度,即如果存在从A到B的链接,并且B的大小为50,则链接的宽度应为50px。

Please help me solve these problems as i'm not able to understand the way it has to be done. 请帮我解决这些问题,因为我无法理解必须要做的事情。

Thank you. 谢谢。

For labels, you need to append text elements to the link paths: 对于标签,您需要将text元素附加到链接路径:

link.enter().insert("text", "g")
  .attr("x", function(d) { return (d.source.x+d.target.x)/2; })
  .attr("y", function(d) { return (d.source.y+d.target.y)/2; })
  .text(function(d) { return d.target.name; });

You may want to adjust the label positions. 您可能需要调整标签位置。

To change the width of the link, you need to set the stroke-width attribute of the path: 要更改链接的宽度,需要设置路径的stroke-width属性:

link.enter().insert("path", "g")
  .attr("class", "link")
  .attr("stroke-width", function(d) { return d.target.size; })
  .attr("d", function(d) {
    var o = {x: source.x0, y: source.y0};
    return diagonal({source: o, target: o});
  });

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

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