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


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

d3.scaleTime()函數用於創建和返回具有特定域和範圍的新時標。在此函數中,默認情況下禁用夾緊。

用法:

d3.scaleTime([[domain, ]range]);

參數:該函數接受上麵提到和下麵描述的兩個參數。

  • Domain:它是定義域值範圍的整數數組。如果未指定,則默認值為[2000-01-01、2000-01-02]。
  • Range:它是整數或字符串的數組。如果未指定,則默認值為[0,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> 
    <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> 
</head> 
  
<body> 
    <h2 style="color:green">Geeks for geeks</h2> 
    <p>d3.scaleTime() Function</p> 
    <script> 
        var time = d3.scaleTime() 
            .domain([1, 10]) 
            .range([10, 5]); 
        document.write("<h3>Type of d3.scaleTime() is:" 
            + typeof (time) + "</h3>"); 
              
        document.write("<h3>time(10):"  
            + time(10) + "</h3>"); 
    </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> 
</head> 
  
<body> 
    <h2 style="color:green;">Geeks for geeks</h2> 
    <p>d3.scaleTime() Function </p> 
    <script> 
        var time = d3.scaleTime() 
            .domain([1, 10]) 
            .range([1, 100]) 
        document.write("<h3>time(1):" + time(1) + "</h3>") 
        document.write("<h3>time(2):" + time(2) + "</h3>") 
        document.write("<h3>time(3):" + time(3) + "</h3>") 
        document.write("<h3>time(4):" + time(4) + "</h3>") 
    </script> 
</body> 
  
</html>

輸出:




相關用法


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