我有一个基于以下内容的 d3 树...
http://bl.ocks.org/mbostock/1093025 http://bl.ocks.org/mbostock/1093025
我如何计算所有孩子的数量?我已经尝试过这个,但是它计算了树中的所有行......
$(".tree_badge").text(tree.links(nodes).length);
因此,在示例中,它应该对所有子项进行计数,其中子项将是树中的橙色行(如簇、图形等中的行)。
感谢您的任何见解!
实际上,我遇到了类似的问题,我必须从特定节点下方的树中获取所有描述。我和你的例子的答案是递归地下降树并在下降的过程中做一些事情。应该看起来像这样。
var count;
function count_leaves(node){
if(node.children){
//go through all its children
for(var i = 0; i<node.children.length; i++){
//if the current child in the for loop has children of its own
//call recurse again on it to decend the whole tree
if (node.children[i].children){
count_leaves(node.children[i]);
}
//if not then it is a leaf so we count it
else{
count++;
}
}
}
注意:如果您想计算节点下方的所有节点,而不仅仅是树末尾的节点,只需在 if 和 else 内添加 count++ 即可。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)