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


d3.js continuous.rangeRound()用法及代码示例


Continuous.rangeRound()函数用于将刻度范围设置为指定的值数组。还将插值器设置为interpolatorRound。舍入内插器对于避免抗锯齿伪像可能很有用。

用法:

continuous.rangeRound( [range] )

参数:该函数接受如上所述和以下描述的单个参数。

  • [范围]:它是一个数组,其中包含指定域的范围。

返回值:此函数不返回任何内容。

以下示例程序旨在说明D3.js中的Continuous.rangeRound()函数:



范例1:

HTML

<!DOCTYPE html> 
<html lang="en"> 
  
<head> 
    <meta charset="UTF-8" /> 
    <meta name="viewport" content= 
        "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>continuous.rangeRound() Function </p> 
  
    <script> 
        var continuous = d3.scaleLinear() 
            // Domain ranges -10, 0, 10 
            .domain([-10, 0, 10]) 
            // Range for the domain 
            .rangeRound([10, 20, 30, 40,  
                    50, 60, 70, 80, 90]); 
  
        document.write("<br/><div>"); 
        document.write("<p> With rangeRound()"); 
        document.write("<h3>" + continuous(10) + "</h3>"); 
        document.write("<h3>" + continuous(2) + "</h3>"); 
        document.write("<h3>" + continuous(11.5) + "</h3>"); 
        document.write("<h3>" + continuous(-1) + "</h3>"); 
        document.write("<h3>" + continuous(1.15) + "</h3>"); 
        document.write("<h3>" + continuous(5) + "</h3></div>"); 
  
        var continuous = d3.scaleLinear() 
  
            // Domain ranges -10, 0, 10 
            .domain([-10, 0, 10]) 
            // Range for the domain 
            .range([10, 20, 30, 40, 50, 60, 70, 80, 90]) 
  
        document.write("<br/><div>"); 
        document.write("<p> Without rangeRound()"); 
        document.write("<h3>" + continuous(10) + "</h3>"); 
        document.write("<h3>" + continuous(2) + "</h3>"); 
        document.write("<h3>" + continuous(11.5) + "</h3>"); 
        document.write("<h3>" + continuous(-1) + "</h3>"); 
        document.write("<h3>" + continuous(1.15) + "</h3>"); 
        document.write("<h3>" + continuous(5) + "</h3></div>"); 
    </script> 
</body> 
  
</html>

输出:

范例2:

HTML

<!DOCTYPE html> 
<html lang="en"> 
  
<head> 
    <meta charset="UTF-8" /> 
    <meta name="viewport" content="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> 
        div { 
            line-height:15px; 
            float:left; 
            padding:10px; 
            word-wrap:break-word; 
            margin-left:10px; 
            height:200px; 
            width:300px; 
        } 
    </style> 
</head> 
  
<body> 
    <h2 style="color:green;">Geeks for geeks</h2> 
  
    <p>continuous.rangeRound() Function </p> 
  
    <script> 
        var continuous = d3.scaleLinear() 
            // Domain ranges -10, 0, 10 
            .domain([-10, 0, 10]) 
            // Range for the domain 
            .range([10, 20, 30, 40, 50, 60, 70, 80, 90]) 
            .interpolate(d3.interpolateRound); 
  
        document.write("<br/><div>"); 
        document.write( 
            "<p> With range().interpolate(d3.interpolate.Round)"); 
        document.write("<h3>" + continuous(10) + "</h3>"); 
        document.write("<h3>" + continuous(2) + "</h3>"); 
        document.write("<h3>" + continuous(11.5) + "</h3>"); 
        document.write("<h3>" + continuous(-1) + "</h3>"); 
        document.write("<h3>" + continuous(1.15) + "</h3>"); 
        document.write("<h3>" + continuous(5) + "</h3></div>"); 
  
        var continuous = d3.scaleLinear() 
            // Domain ranges -10, 0, 10 
            .domain([-10, 0, 10]) 
            // Range for the domain 
            .rangeRound([10, 20, 30, 40, 50, 60, 70, 80, 90]) 
  
        document.write("<br/><div>"); 
        document.write("<p> With rangeRound()"); 
        document.write("<h3>" + continuous(10) + "</h3>"); 
        document.write("<h3>" + continuous(2) + "</h3>"); 
        document.write("<h3>" + continuous(11.5) + "</h3>"); 
        document.write("<h3>" + continuous(-1) + "</h3>"); 
        document.write("<h3>" + continuous(1.15) + "</h3>"); 
        document.write("<h3>" + continuous(5) + "</h3></div>"); 
    </script> 
</body> 
  
</html>

输出:




相关用法


注:本文由纯净天空筛选整理自tarun007大神的英文原创作品 D3.js continuous.rangeRound() Function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。