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


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

threshold.range()函數用於設置閾值刻度的範圍。範圍數組中的值的數量總是比域數組大一,如果不這樣,則刻度的行為可能不確定。

用法:

threshold.range([range]);

參數:該函數接受上麵給定和下麵描述的單個參數。

  • range:此參數接受離散值數組。如果域中存在數量為“n”的值,則範圍必須具有數量為“n+1”的值。

返回值:此函數不返回任何值。

範例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> 
    <h2 style="color:green;">GeeksforGeeks</h2> 
  
    <p>threshold.range() Function </p> 
  
    <script> 
        var threshold = d3.scaleThreshold() 
  
            // Setting domain for the scale. 
            .domain([10, 20, 30, 40]) 
  
            // Setting the range for the scale. 
            // Number of elements is one more  
            // then domain array size. 
            .range(["red", "blue",  
                "green", "yellow", "white"]); 
  
        document.write("<h4>"  
            + threshold(10) + "</h4>"); 
              
        document.write("<h4>"  
            + threshold(20) + "</h4>"); 
    </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" /> 
  
    <script src="https://d3js.org/d3.v4.min.js"> 
    </script> 
</head> 
  
<body> 
    <h2 style="color:green;">GeeksforGeeks</h2> 
  
    <p>threshold.range() Function </p> 
  
    <script> 
        var threshold = d3.scaleThreshold() 
  
            // Setting domain for the scale. 
            .domain([10, 20, 30, 40]) 
          
            // Setting the range for the scale. 
            // Number of elements is one more  
            // then domain array size. 
            .range([0.01, 0.02, 0.03, 0.04, 0.05]); 
  
        document.write("<h4>"  
            + threshold(20) + "</h4>"); // 0.03 
  
        document.write("<h4>"  
            + threshold(40) + "</h4>"); // 0.05 
  
        // This value is outside the domain 
        document.write("<h4>"  
            + threshold(500) + "</h4>"); // 0.05 
    </script> 
</body> 
  
</html>

輸出:




相關用法


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