當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


d3.js node.leaves()用法及代碼示例

d3.js中的node.leaves()函數用於按遍曆順序返回給定層次數據的葉節點數組。

用法:

node.leaves();

參數:該函數不接受任何參數。

返回值:該函數返回一個數組。

注意:葉節點是沒有子節點的那些節點。



下麵給出的是上麵給出的函數的一些例子。

範例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_leaf1" 
                }, 
                { 
                    name:"child2", 
                    children:[ 
                        { 
                            name:"grandchild1", 
                            children:[ 
                                { name:"leaf2" } 
                            ] 
                        }, 
                    ] 
                }, 
                { 
                    name:"child3", 
                    children:[ 
                        { name:"leaf3" }, 
                        { name:"leaf4" }, 
                    ] 
                } 
            ] 
        }; 
  
        var obj = d3.hierarchy(tree); 
          
        // Leaf nodes of the above given tree 
        console.log("Leaf nodes are:"); 
        console.log(obj.leaves());         
    </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", 
            children:[ 
                { 
                    name:"child1_leaf1" 
                }, 
                { 
                    name:"child2", 
                    children:[ 
                        { 
                            name:"grandchild1", 
                            children:[ 
                                { name:"leaf2" } 
                            ] 
                        }, 
                    ] 
                }, 
                { 
                    name:"child3", 
                    children:[ 
                        { name:"leaf3" }, 
                        { name:"leaf4" }, 
                    ] 
                } 
            ] 
        }; 
  
        var obj = d3.hierarchy(tree); 
          
        // Leaf nodes of the above given tree are:
        console.log(" Accessing Leaf nodes:"); 
        let leafArr = obj.leaves(); 
        leafArr.forEach((e) => { 
            console.log(e.data); 
            console.log("depth:", e.depth); 
        })         
    </script> 
</body> 
  
</html>

輸出:




相關用法


注:本文由純淨天空篩選整理自tarun007大神的英文原創作品 D3.js node.leaves() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。