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


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

D3.js中的intepolateString()函數用於返回兩個字符串之間的插值器函數。對於字符串“b”中的每個數字,該函數在字符串“a”中為其找到一個數字,然後使用數字插值器函數返回這些數字的插值,並將字符串“b”的其餘部分用作模板。

用法:

d3.intepolateString(a, b);

參數:它帶有兩個參數:

  • a:它是由字符和數字組成的字符串。
  • b:它也是一串字符和數字。

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

下麵給出了上述函數的一些示例。



範例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> 
    let s1="42 geeks 16"; 
    let s2="500 Geeks 10 for 20 geeks" 
    let interpolatoreFunc=d3.interpolateString(s1, s2); 
    /* Note that the string alphabets of string b are 
      same as output but the Numbers are changed.*/ 
    console.log(interpolatoreFunc(0.25)) 
    console.log(interpolatoreFunc(5)) 
    console.log(interpolatoreFunc(0)) 
    console.log(interpolatoreFunc()) 
  </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> 
    try{ 
     // Trying numbers with this function 
      console.log(d3.interpolateString(24, 15)(0.26)) 
      // If oly stirng is given and no Number 
      console.log( 
d3.interpolateString("geeks", "for Geeks")(0.5)) 
      // If a is a number and b is a string 
      console.log( 
d3.interpolateString(24, "Geeks for geeks")(0.8)) 
      // If a is not given  
      console.log( 
d3.interpolateString("13 geeks")(0.46)) 
      console.log( 
typeof d3.interpolateString("2 asda", "13 geeks")) 
    } 
    catch(err){ 
      throw err; 
    } 
      
  </script> 
</body> 
</html>

輸出:




相關用法


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