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


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

time.rangeRound()函數用於將刻度範圍設置為指定的值數組,並將插值器設置為intepolateRound。

用法:

time.rangeRound([range]);

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

  • range:此參數接受數字或字符串數​​組。

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

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



範例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> 
    <script src="https://d3js.org/d3-color.v1.min.js"> 
    </script> 
    <script src="https://d3js.org/d3-interpolate.v1.min.js"> 
    </script> 
    <script src="https://d3js.org/d3-scale-chromatic.v1.min.js"> 
    </script> 
  
    <style> 
        body { 
            margin-left:40px; 
        } 
  
        h2 { 
            color:green; 
        } 
  
        div { 
            float:left; 
            width:280px; 
        } 
    </style> 
</head> 
  
<body> 
    <h2>Geeks for geeks</h2> 
    <p>time.rangeRound() Function </p> 
      
    <script> 
        var time = d3.scaleTime() 
            // Setting domain for the scale. 
            .domain([1, 10]) 
            // Setting the range of the scale. 
            .range([1, 100]); 
  
        document.write( 
            "<div><h3>Without time.rangeRound()</h3>"); 
        document.write( 
            "<h3>time(1.5):" + time(1.5) + "</h3>"); 
        document.write( 
            "<h3>time(2):" + time(2) + "</h3>"); 
        document.write( 
            "<h3>time(3.5):" + time(3.5) + "</h3>"); 
        document.write( 
            "<h3>time(4.5):" + time(4.5) + "</h3></div>"); 
  
        var time = d3.scaleTime() 
            // Setting domain for the scale. 
            .domain([1, 10]) 
            // Setting the range of the scale. 
            .rangeRound([1, 100]); 
        document.write( 
            "<div><h3>With time.rangeRound()</h3>"); 
        document.write( 
            "<div><h3>time(1.5):" + time(1.5) + "</h3>"); 
        document.write( 
            "<h3>time(2):" + time(2) + "</h3>"); 
        document.write( 
            "<h3>time(3.5):" + time(3.5) + "</h3>"); 
        document.write( 
            "<h3>time(4.5):" + time(4.5) + "</h3></div>"); 
    </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> 
    <script src="https://d3js.org/d3-color.v1.min.js"> 
    </script> 
    <script src= 
"https://d3js.org/d3-interpolate.v1.min.js"> 
    </script> 
    <script src= 
"https://d3js.org/d3-scale-chromatic.v1.min.js"> 
    </script> 
  
    <style> 
        body { 
            margin-left:40px; 
        } 
  
        h2 { 
            color:green; 
        } 
    </style> 
</head> 
  
<body> 
    <h2>Geeks for geeks</h2> 
    <p>time.rangeRound() Function </p> 
    <script> 
  
        var time = d3.scaleTime() 
  
            // Setting domain for the scale. 
            .domain([2001 - 01 - 01, 2000 - 01 - 02]) 
            .rangeRound([1, 10]) 
        document.write("<h3>time(2.4):"  
                + time(2.4) + "</h3>"); 
  
        document.write("<h3>time(2):"  
                + time(2) + "</h3>"); 
  
        document.write("<h3>time(6.4):"  
                + time(6.4) + "</h3>"); 
                  
        document.write("<h3>time(4.5):"  
                + time(4.5) + "</h3>"); 
    </script> 
</body> 
  
</html>

輸出:




相關用法


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