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


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


如果給出了範圍內的值,則使用D3.js中的d3.continuous.invert()函數從域中返回相應的值。

用法:

.invert( Value )

參數:此函數接受單個參數Value,該參數保存範圍的值。


返回值:如果給出了範圍內的值,則此函數從域中返回相應的值。

以下程序說明了D3.js中的d3.continuous.invert()函數:

示例1:

<!DOCTYPE html> 
<html> 
  
<head> 
    <title> 
        D3.js | d3.continuous.invert() Function 
    </title> 
      
    <script src =  
        "https://d3js.org/d3.v4.min.js"> 
    </script> 
</head> 
  
<body> 
    <script> 
          
        // Calling scaleLinear() function 
        var x = d3.scaleLinear().domain([10, 150]) 
                    .range([0, 1000]); 
  
        // Calling the .invert() function 
        // and getting the corresponding value 
        // from domain if value from range is given 
        console.log(x.invert(150));  
        console.log(x.invert(999)); 
    </script> 
</body> 
  
</html>

輸出:

31
149.86

示例2:

<!DOCTYPE html> 
<html> 
  
<head> 
    <title> 
        D3.js | d3.continuous.invert() Function 
    </title> 
      
    <script src =  
        "https://d3js.org/d3.v4.min.js"> 
    </script> 
</head> 
  
<body> 
    <script> 
          
        // Calling scaleLinear() function 
        var x = d3.scaleLinear().domain([0, 1]) 
                .range([10, 20]); 
  
        // Calling the .invert() function 
        // and getting the corresponding value 
        // from domain if value from range is given 
        console.log(x.invert(11));  
        console.log(x.invert(13)); 
        console.log(x.invert(15));  
        console.log(x.invert(19)); 
          
    </script> 
</body> 
</html>    

輸出:

0.1
0.3
0.5
0.9

參考: https://devdocs.io/d3~5/d3-scale#continuous_invert



相關用法


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