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


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

d3.stratify()函數用於構造並返回新的分層運算符。該運算符具有其自己的默認設置。此函數用於將樹形鏈接表示形式轉換為層次結構。

用法:

d3.stratify();

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

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

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



範例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> 
        // This is the data of the CSV file 
        // child, parent 
        // a, 
        // b, a 
        // c, a 
        // d, a 
        // e, b 
        // f, c 
        // g, c 
        // h, d 
        // i, h 
  
        // Fetching the csv File 
        d3.csv("./data.csv",(links)=>{ 
            var child = links.columns[0]; 
            var parent = links.columns[1]; 
            stratify = d3.stratify() 
                        .id(d => d[child]) 
                        .parentId(d => d[parent]); 
            var root=stratify(links) 
            console.log(root) 
        }); 
    </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"/>  
    <title>GeekforGeeks</title>  
    <script src = 
    "https://d3js.org/d3.v4.min.js"> 
    </script> 
</head>  
  
<body>  
    <script> 
        // This is the data of the CSV file 
        // child, parent 
        // 0, 
        // 1, 0 
        // 2, 0 
        // 3, 0 
        // 4, 2 
        // 5, 3 
          
        // Fetching the csv File 
        d3.csv("./data.csv",(links)=>{ 
            var child = links.columns[0]; 
            var parent = links.columns[1]; 
            stratify = d3.stratify() 
                        .id(d => d[child]) 
                        .parentId(d => d[parent]); 
            var root=stratify(links); 
            console.log(root.children[0].data); 
            console.log(root.children[1].data); 
            console.log(root.children[2].data); 
        }); 
    </script>  
</body>  
</html>

輸出:




相關用法


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