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


d3.js d3.scaleBand()用法及代码示例


D3.js中的d3.scaleBand()函数用于构造新的带刻度,其域指定为值的数组,范围指定为带的最小和最大范围。此函数将范围划分为n个波段,其中n是域数组中值的数量。

用法:

d3.scaleBand().domain(array of values).range(array of values);

参数:该函数不接受任何参数。


返回值:此函数返回域值的相应范围。

以下程序说明了D3.js中的d3.scaleBand()函数:

示例1:

<!DOCTYPE html> 
<html> 
  
<head> 
    <title> 
        D3.js | d3.scaleBand() Function 
    </title> 
      
    <script src = 
        "https://d3js.org/d3.v4.min.js"> 
    </script> 
</head> 
  
<body> 
    <script> 
          
        // Calling the scaleBand() function 
        var A = d3.scaleBand() 
            .domain(['January', 'February', 'March', 'April', 'May']) 
            .range([10, 50]); 
          
        // Getting the corresponding range for 
        // the domain value 
        console.log(A('January')); 
        console.log(A('February')); 
        console.log(A('March')); 
        console.log(A('April')); 
        console.log(A('May')); 
    </script> 
</body> 
  
</html>

输出:

10
18
26
34
42

示例2:

<!DOCTYPE html> 
<html> 
  
<head> 
    <title> 
        D3.js | d3.scaleBand() Function 
    </title> 
      
    <script src = 
        "https://d3js.org/d3.v4.min.js"> 
    </script> 
</head> 
  
<body> 
    <script> 
          
        // Calling the scaleBand() function 
        var A = d3.scaleBand() 
            .domain(['January', 'February', 'March', 'April', 'May']) 
            .range([1, 2]); 
           
        // Getting the corresponding range for 
        // the domain value 
        console.log(A('January')); 
        console.log(A('February')); 
        console.log(A('March')); 
        console.log(A('April')); 
        console.log(A('May')); 
    </script> 
</body> 
  
</html>

输出:

1
1.2
1.4
1.6
1.8

参考: https://devdocs.io/d3~5/d3-scale#scaleBand



相关用法


注:本文由纯净天空筛选整理自Kanchan_Ray大神的英文原创作品 D3.js | d3.scaleBand() Function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。