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


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


d3.scalepoint()函数用于创建和返回具有特定域和范围,无舍入,无填充和居中对齐的新点比例尺。

用法:

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

参数:该函数采用上面给出和下面描述的两个参数:

  • domain:它定义了刻度的最小值和最大值。默认情况下,域包含一个空数组。
  • 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> 
</head> 
  
<body> 
    <script> 
        // Creating the point scale with 
        // specified domain and range. 
        var point = d3.scalePoint() 
  
            // Setting domain for the scale 
            .domain(["red", "green", "black", "blue"]) 
          
            // Setting the range for the scale. 
            .range([0.1, 0.2, 0.3, 0.4, 0.5]); 
  
        console.log("point(red)", point("red")); 
        console.log("point(black)", point("black")); 
        console.log("point(green)", point("green")); 
        console.log("point(blue)", point("blue")); 
    </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> 
</head> 
  
<body> 
    <script> 
        // Creating the point scale with 
        // specified domain and range. 
        var point = d3.scalePoint() 
  
            // Setting domain for the scale 
            .domain([0, 1, 2, 3, 4, 5, 6]) 
          
            // Setting the range for the scale. 
            .range([3, 4, 0.5]); 
  
        console.log("point(1)", point("1")); 
        console.log("point(3)", point("3")); 
        console.log("point(5)", point("5")); 
    </script> 
</body> 
  
</html>

输出:




相关用法


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