如何将替代深度值只分配给d3js中的子节点?

现在所有的节点都有固定的深度。 我怎样才能分配一个替代值只有子节点

看图

我尝试改变值nodes.forEach(function(d) { dy = d.depth * 200; }); 但它会影响所有的节点。 我想要这样的输出:

这个

您可以使用d.depth来过滤值:

 nodes.forEach(function(d) { // value of y differ if d.depth === 1 dy = d.depth === 1 ? d.depth * maxLabel : d.depth * 200; }); 

http://jsfiddle.net/zmozdpum/

注意:如果你想为奇数节点设置不同的大小,请看这个小提琴

演示http://jsfiddle.net/b2rd7ps8/1/

 function normalizeNodesDepth(node){ /* get index of parent from it's parent's children array..*/ var parentIndex = (!!node.parent && node.parent != 'null' && !!node.parent.parent && node.parent.parent != 'null' ) ? node.parent.parent.children.map(e => e.id).indexOf(node.parent.id) : null; /* if node is the last child and it's parent index is even, mutiple by greater ...*/ node.y = node.depth * maxLabel * ((parentIndex != null && !(parentIndex % 2) && (!node.children || !!node.children.length)) ? 1.5 : 1); if(!node.children) { return; } /* recur for all its children ...*/ node.children.forEach((child, index) => { normalizeNodesDepth(child); }); } //nodes.forEach(function(d) { dy = d.depth * maxLabel; }); normalizeNodesDepth(root);