当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


d3.js node.count()用法及代码示例


D3.js库的node.count()函数用于计算特定节点下的叶子数,并将其作为value属性附加到对象。如果给定的节点本身是叶节点,则计数为1。

用法:

node.count();

参数:此函数不带任何参数。

返回值:此函数返回一个对象。

下面给出的是上面给出的函数的一些例子。



范例1:以下示例演示了计数根节点的叶子。

HTML

<!DOCTYPE html> 
<html lang="en"> 
  
<head> 
    <meta charset="UTF-8" /> 
    <meta name="viewport" path1tent= 
    "width=device-width, initial-scale = 1.0" /> 
  
    <script src="https://d3js.org/d3.v4.min.js"> 
    </script> 
</head> 
  
<body> 
    <script> 
        // Constructing a tree 
        var tree = { 
            name:"rootNode", 
            children:[ 
                { 
                    name:"child1" 
                }, 
                { 
                    name:"child2", 
                    children:[ 
                        { 
                            name:"grandchild1", 
                            children:[ 
                                { name:"grand_granchild1_1" }, 
                                { name:"grand_granchild1_2" } 
                            ] 
                        }, 
                        { 
                            name:"grandchild2", 
                            children:[ 
                                { name:"grand_granchild2_1" }, 
                                { name:"grand_granchild2_2" } 
                            ] 
                        }, 
                    ] 
                } 
            ] 
        }; 
  
        var obj = d3.hierarchy(tree); 
  
        // Using node.count() Function 
        var count = obj.count(); 
        console.log("The number of leaves for root" 
                + " node are:", count.value); 
    </script> 
</body> 
  
</html>

输出:

范例2:以下代码演示了计数任何节点的叶子数。

HTML

<!DOCTYPE html> 
<html lang="en"> 
  
<head> 
    <meta charset="UTF-8" /> 
    <meta name="viewport" path1tent= 
        "width=device-width, initial-scale = 1.0" /> 
    <script src="https://d3js.org/d3.v4.min.js"> 
    </script> 
</head> 
  
<body> 
    <script> 
        // Constructing a tree 
        var tree = { 
            name:"rootNode", // Root node 
            children:[ 
                { 
                    name:"child1" // Child of root node 
                }, 
                { 
                    name:"child2", // Child of root node 
                    children:[ 
                        { 
                            // Child of child2 
                            name:"grandchild1", 
                            children:[ 
                                // Child of grandchild1 
                                { name:"grand_granchild1_1" }, 
                                // Child of grandchild1  
                                { name:"grand_granchild1_2" } 
                            ] 
                        }, 
                        { 
                            name:"grandchild2", 
                            children:[ 
                                // Child of grandchild2 
                                { name:"grand_granchild2_1" }, 
                                // Child of grandchild2 
                                { name:"grand_granchild2_2" } 
                            ] 
                        }, 
                    ] 
                } 
            ] 
        }; 
  
        var obj = d3.hierarchy(tree); 
        var grandchild2 = obj.children[1].children[1]; 
  
        // Using node.count() function 
        var count = grandchild2.count(); 
        console.log("The number of leaves for " 
            + "grandchild2 are:", count.value); 
    </script> 
</body> 
  
</html>

输出:




相关用法


注:本文由纯净天空筛选整理自tarun007大神的英文原创作品 D3.js node.count() Function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。