[英]“Leaf-justify” D3 tree graphs?
一種實現方法是發現樹的深度,並將沒有任何子節點的每個節點的depth
屬性設置為該值。
假設您的樹數據如下:
var data = [
{
"name": "Top Level",
"parent": "null",
"children": [
{
"name": "Level 2: A",
"parent": "Top Level",
"children": [ ...
您使用以下方法創建了一棵樹:
var tree = d3.layout.tree();
你可以得到depth
通過搜索的最大值每個節點樹的depth
:
var treeDepth = d3.max(tree(data[0]), function(d) { return d.depth; });
一旦有了它,就可以在每次重新計算樹布局時將其重置:
tree.nodes(data[0]).forEach(function(d) {
var depthSize = 50;
if (!d.children) { // this node has no children
d.depth = treeDepth; // set depth to depth of tree
}
d.y = d.depth * depthSize; // recalculate y
});
這適用於示例中從左到右排列的樹木。 對於自上而下的布局,您必須改為dx
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.