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


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

D3.js中的nest.key()函數用於注冊一個新的鍵函數,該鍵函數將為輸入數組中的每個元素調用。每當注冊新 key 時,都會將其推入數組的末尾。

用法:

nest.keys(key)

參數:

  • 它僅采用一個參數,即鍵函數。

返回:它返回一個字符串標識符。

下麵給出了一些示例,可以更好地解釋該函數。



範例1:

<!DOCTYPE html> 
<html lang="en"> 
<head> 
  <meta charset="UTF-8"> 
  <meta name="viewport" 
        content="width=device-width,  
                 initial-scale=1.0"> 
  <title>Document</title> 
</head> 
<style> 
  .originalColor{ 
    height:100px; 
    width:100px; 
  } 
  .darkerColor{ 
    height:100px; 
    width:100px; 
  } 
</style> 
<body> 
    
  <!-- Fetching from CDN of D3.js -->
  <script type = "text/javascript" 
          src =  
"https://d3js.org/d3.v4.min.js"></script> 
  <script> 
    let collection=[ 
      {val:"val1"}, 
      {val:"val2"}, 
      {val:"val3"}, 
      {val:"val4"} 
    ] 
    let data=d3.nest().key((d)=>{return d.val}) 
                      .entries(collection) 
    console.log(data); 
  </script> 
</body> 
</html>

輸出:

範例2:

<!DOCTYPE html> 
<html lang="en"> 
<head> 
  <meta charset="UTF-8"> 
  <meta name="viewport" 
        content="width=device-width, 
                 initial-scale=1.0"> 
  <title>Document</title> 
</head> 
<style> 
  .originalColor{ 
    height:100px; 
    width:100px; 
  } 
  .darkerColor{ 
    height:100px; 
    width:100px; 
  } 
</style> 
<body> 
    
  <!--fetching from CDN of D3.js -->
  <script type = "text/javascript"
          src =  
"https://d3js.org/d3.v4.min.js"> 
   </script> 
  <script> 
    let collection=[ 
      {val:"val1", data:"data1", anotherData:"data1"}, 
      {val:"val2", data:"data2", anotherData:"data2"}, 
      {val:"val3", data:"data3", anotherData:"data3"}, 
      {val:"val4", data:"data4", anotherData:"data4"} 
    ] 
    //key is added 
    let data=d3.nest().key((d)=>{return d.val}) 
                      //another key is nested and added 
                      .key((d)=>{return d.data}) 
                      //another key is added and nested 
                      .key((d)=>{return d.anotherData}) 
                      .entries(collection) 
    console.log(data); 
  </script> 
</body> 
</html>

輸出:




相關用法


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