当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。