现在所有节点都有固定的深度。如何仅为子节点
分配备用值
我尝试更改值nodes.forEach(function(d) { d.y = d.depth * 200; });
,但它会影响所有节点。我想要这样的输出:
答案 0 :(得分:0)
您可以使用d.depth
过滤值:
nodes.forEach(function(d) {
// value of y differ if d.depth === 1
d.y = d.depth === 1 ? d.depth * maxLabel : d.depth * 200;
});
请参阅http://jsfiddle.net/zmozdpum/
注意:如果您希望奇数节点具有不同的大小,请查看此fiddle
答案 1 :(得分:0)
演示this
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) { d.y = d.depth * maxLabel; });
normalizeNodesDepth(root);