简体   繁体   English

如何增加D3可折叠树中子节点之间的距离?

[英]How to increase distance between child nodes in a D3 Collapsible Tree?

I have tried the suggestion in this answer but it makes no difference whatsoever. 我在这个答案中尝试了这个建议,但它没有任何区别。

I'm using the standard collapsible tree example from the D3 website. 我正在使用D3网站上的标准可折叠树示例

Specifically, I've added the following, as the answer suggests: 具体来说,我添加了以下内容,正如答案所示:

var tree = d3.layout.tree()
    .separation(function(a, b) { return ((a.parent == root) && (b.parent == root)) ? 3 : 1; })
    .size([height, width - 160]);

But nothing. 但没什么。 The documentation also suggests adjusting the separation attribute but modifying the default one listed there with any random value seems to produce no effect on the layout: 文档还建议调整separation属性,但修改其中列出的默认值与任何随机值似乎不会对布局产生影响:

.separation(function(a, b) { return a.parent == b.parent ? 1 : 7; }) // no effect

Am I missing something obvious? 我错过了一些明显的东西吗 I've not done any JavaScript or jQuery and it's my first time using this library so I'm just trying to go along as I can. 我没有做任何JavaScript或jQuery,这是我第一次使用这个库,所以我只是想尽我所能。

The reason for trying to accomplish this is that the names of each node are quite long and when expanding a child node with lots of children, they overlap in an ugly way (I'm using different json data to the one in the example). 尝试实现此目的的原因是每个节点的名称都很长,并且在扩展具有大量子节点的子节点时,它们以丑陋的方式重叠(我使用不同的json数据到示例中的那个)。

A helpful and generous friend of mine, working in the web development industry, has been able to crack the problem for me and the solution is as follows. 我的一个有用和慷慨的朋友,在网络开发行业工作,已经能够解决我的问题,解决方案如下。

The line that takes care of the distance is this: 照顾距离的线是这样的:

nodes.forEach(function(d) { d.y = d.depth * 180; });

This specifies a separation in pixels (in this case 180) between a parent and a child. 这指定了父项和子项之间的像素分隔(在这种情况下为180)。

If the text also needs to be adjusted, the line in question is: 如果还需要调整文本,那么该行是:

.attr("x", function(d) { return d.children || d._children ? -10 : 10; })

This is spacing out the text only and it's either on the left or the right of the node depending on whether or not it's a child node. 这只是文本的间隔,它位于节点的左侧或右侧,具体取决于它是否是子节点。 Adjusting by -10 or 10 should produce visible results. 调整-10或10应产生可见的结果。

Hope it is of help to anyone who comes across this problem! 希望对遇到这个问题的人有所帮助!

var tree = d3.layout.tree().nodeSize([150, 40]); var tree = d3.layout.tree()。​​nodeSize([150,40]);

Here you change the width value, to change the distance between adjacent child nodes. 在此处更改宽度值,以更改相邻子节点之间的距离。

nodes.forEach(function(d) { d.y = d.depth * 180; });

It works for me. 这个对我有用。 Here 180 is the distance in pixel between two nodes. 这里180是两个节点之间的像素距离。 You can change the value on your demand. 您可以根据需要更改值。

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

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