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


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

d3.InterpolateArray()用於對兩個數組進行插值,並在它們之間返回一個插值器。

用法:

d3.interpolateArray(a, b);

參數:它帶有兩個參數。

  • a:它是一個數組。
  • b:它是一個數組。

返回值:它返回插值器函數。

下麵給出了一些InterpolateArray()函數的示例。



範例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> 
</style> 
<body> 
  <!--fetching from CDN of D3.js -->
  <script type = "text/javascript" 
          src =  
"https://d3js.org/d3.v4.min.js"> 
   </script> 
  <script> 
    console.log("Type of the function is:",  
typeof(d3.interpolateArray([12, 3], [4, 5, 6]))) 
    console.log(d3.interpolateArray([12, 3], [4, 5, 6])(0.2)) 
    console.log(d3.interpolateArray([12, 3], [4, 5, 6])(0.6)) 
    console.log(d3.interpolateArray([12, 3], [4, 5, 6])(0.2)) 
  </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> 
</style> 
<body> 
  <!--fetching from CDN of D3.js -->
  <script type = "text/javascript"
          src =  
"https://d3js.org/d3.v4.min.js"> 
  </script> 
  <script> 
    console.log("Type of the function is:",  
typeof(d3.interpolateArray([12, 3], [4, 5, 6]))) 
  
    console.log( 
d3.interpolateArray([12.25, 3.36], [0.54, 5, .636])(0.2)); 
  
    // This will give error as it is not an array   
    try{ 
      console.log(d3.interpolateArray(1452, 63244)(0.6)) 
    } 
    catch(err){ 
        console.log(err) 
    } 
    console.log( 
d3.interpolateArray(["a", "b", "d"], ["a", "c"])(0.2)); 
  </script> 
</body> 
</html>

輸出:




相關用法


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