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


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

node.eachBefore()函數用於為每個節點調用特定函數,但順序為遍曆順序。它以遍曆前的順序訪問每個節點,並對那個特定節點及其每個後代執行操作。

用法:

node.eachBefore(function);

參數:該函數接受如上所述和以下描述的單個參數:

  • function:這是要為每個節點調用的函數。

返回值:此函數不返回任何內容。

範例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 = { 
            // Root node 
            name:"rootNode", 
            children:[ 
                { 
                    // Child of root node 
                    name:"child1", 
                    value:2 
                }, 
                { 
                    // Child of root node 
                    name:"child2", 
                    value:3, 
                    children:[ 
                        { 
                            // Child of child2 
                            name:"grandchild1", 
                            value:1, 
                            children:[ 
                                { 
                                    // Child of grandchild1 
                                    name:"grand_granchild1_1", 
                                    value:4 
                                }, 
                                { 
                                    // Child of grandchild1 
                                    name:"grand_granchild1_2", 
                                    value:5 
                                } 
                            ] 
                        }, 
                        { 
                            name:"grandchild2", 
                            children:[ 
                                { 
                                    // Child of grandchild2 
                                    name:"grand_granchild2_1" 
                                }, 
                                { 
                                    // Child of grandchild2 
                                    name:"grand_granchild2_2" 
                                } 
                            ] 
                        } 
                    ] 
                } 
            ] 
        }; 
  
        var obj = d3.hierarchy(tree); 
  
        const BFS = []; 
  
        obj.eachBefore(d => BFS.push 
            ( 
                " ".repeat(d.depth)  
                    + `${d.depth}:${d.data.name}` 
            )); 
  
        BFS.forEach((e) => { 
            console.log("level:", e); 
        }) 
    </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({ 
            // Root node 
            name:"rootNode", 
            children:[ 
                { 
                    // Child of root node 
                    name:"child1", 
                    value:2 
                }, 
                { 
                    // Child of root node 
                    name:"child2", 
                    value:3, 
                    children:[ 
                        { 
                            // Child of child2 
                            name:"grandchild1", 
                            value:1, 
                            children:[{ 
                                // Child of grandchild1 
                                name:"grand_granchild1_1", 
                                value:4 
                            }, 
  
                            { 
                                // Child of grandchild1 
                                name:"grand_granchild1_2", 
                                value:5 
                            }] 
                        } 
                    ] 
                } 
            ] 
        }); 
  
        const BFS = []; 
  
        obj.eachBefore(d => BFS.push 
            ( 
                " ".repeat(d.depth)  
                    + `${d.depth}:${d.data.name}` 
            )); 
  
        console.log(BFS); 
    </script> 
</body> 
  
</html>

輸出:




相關用法


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