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


d3.js band.rangeRound()用法及代碼示例

band.rangeRound()函數用於將小數位數的範圍設置為指定的兩個元素的數組,並將此範圍也設置為true。

用法:

band.rangeRound([range]) 

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

  • range:此參數接受一個包含兩個元素的數字數組。

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

下麵給出的是上麵給出的函數的一些例子。



範例1:

<!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>  
    <script>  
    // Creating the band scale with 
    // specified domain and range. 
        var band = d3.scaleBand() 
                    .domain([10, 20, 30, 40, 50]) 
                    .range([0, 1]); 
  
        console.log("Without rangeRound function:"); 
        console.log("band(10):", band(10)); 
        console.log("band(20):", band(20)); 
        
        console.log("With rangeRound function:"); 
        
        var band = d3.scaleBand() 
                    .domain([10, 20, 30, 40, 50]) 
                    .rangeRound([0, 1]); 
        
        console.log("band(10):", band(10)); 
        console.log("band(20):", band(20)); 
    </script>  
</body>  
</html>

輸出:

範例2:

<!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>  
    <script>  
    // Creating the band scale with 
     // specified domain and range. 
        var band = d3.scaleBand() 
                    .domain([1, 2, 3, 4]) 
        // When range is in string 
                    .range(["0", "10"]); 
        console.log("Without rangeRound function:"); 
        console.log("band(1):", band(1)); 
        console.log("band(2):", band(2)); 
        
        console.log("With rangeRound function:"); 
        
        var band = d3.scaleBand() 
                    .domain([1, 2, 3, 4, 5]) 
                    .rangeRound(["0", "10"]); 
        
        console.log("band(1):", band(1)); 
        console.log("band(2):", band(2)); 
    </script>  
</body>  
</html>

輸出:




相關用法


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