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


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。