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


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

d3.js庫中的node.descendants()函數用於生成並返回後代節點數組。

用法:

node.descendants();

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

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

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



範例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> 
        var obj = d3.hierarchy({ 
            name:"rootNode", 
            children:[ 
                { 
                    name:"child1" 
                }, 
                { 
                    name:"child2", 
                    children:[ 
                        { name:"grandChild1" }, 
                    ] 
                }, 
                { 
                    name:"child3", 
                    children:[ 
                        { name:"grandChild5" }, 
                        { name:"grandChild6" }, 
                        { name:"grandChild7" }, 
                        { name:"grandChild8" }, 
                    ] 
                } 
            ] 
        }); 
        // Descendant of child3 
        console.log("Descendant of child3 are:"); 
        console.log(obj.children[2] 
            .descendants()[0].data.name); 
  
        console.log(obj.children[2] 
            .descendants()[1].data.name); 
  
        console.log(obj.children[2] 
            .descendants()[2].data.name); 
  
        console.log(obj.children[2] 
            .descendants()[3].data.name); 
  
        console.log(obj.children[2] 
            .descendants()[4].data.name); 
    </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> 
        var obj = d3.hierarchy({ 
            name:"rootNode", 
            children:[ 
                { 
                    name:"child1" 
                }, 
                { 
                    name:"child2", 
                    children:[ 
                        { name:"grandChild1" }, 
                    ] 
                }, 
                { 
                    name:"child3", 
                    children:[ 
                        { name:"grandChild5" }, 
                        { name:"grandChild6" }, 
                        { name:"grandChild7" }, 
                        { name:"grandChild8" }, 
                    ] 
                } 
            ] 
        }); 
  
        // Descendant of child3 
        console.log("Descendant of child3 are:"); 
        console.log(obj.children[2].descendants()); 
    </script> 
</body> 
  
</html>

輸出:




相關用法


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