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


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

time.range()函數用於將刻度範圍設置為指定的值數組。給定的數組包含兩個或多個元素。

用法:

time.range([range]);

參數:該函數接受上述和以下描述的單個參數:

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

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

以下示例說明了D3.js中的time.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>  
</head>  
<body>  
    <h2 style="color:green;"> 
        Geeksforgeeks 
    </h2> 
    <p>time.range() Function </p> 
    <script> 
        // Setting domain for the scale.  
        // Setting the range of the scale. 
        var time = d3.scaleTime()                 
                .domain([1, 100])                 
                .range([1, 10]); 
        document.write("<h3>time(10):" 
        +time(10)+"</h3>"); 
        document.write("<h3>time(20):" 
        +time(20)+"</h3>"); 
        document.write("<h3>time(30):" 
        +time(30)+"</h3>"); 
        document.write("<h3>time(40):" 
        +time(40)+"</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>  
<style> 
  
</style> 
<body>  
    <h2 style="color:green;"> 
        Geeksforgeeks 
    </h2> 
    <p>time.range() Function </p> 
    <script> 
        // Setting domain for the scale.  
        // Setting the range of the scale. 
        var time = d3.scaleTime()                 
                .domain([1, 10])                  
                .range(["red", "green"]); 
        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>"); 
    </script>  
</body>  
</html>

輸出:




相關用法


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