当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。