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


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


d3.scaleOrdinal()函数用于创建和返回具有指定范围和域的序数标度。如果没有给出域和范围,则两者都设置为空数组。这些类型的秤具有离散的域和范围。

用法:

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

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

  • domain:它定义了刻度的最小值和最大值。
  • 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> 
</head> 
  
<body> 
    <script> 
  
        // Creating the Ordinal scale. 
        var ordinal = d3.scaleThreshold() 
  
            // Setting domain for the scale 
            .domain([1, 2, 3, 4]) 
            .range([10, 20, 30, 40]); 
          
        console.log("The value of ordinal(1) is:", 
                ordinal(1)); 
        console.log("The value of ordinal(2) is:", 
                ordinal(2)); 
        console.log("The value of ordinal(3) is:", 
                ordinal(3)); 
    </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 Ordinal scale. 
        var ordinal = d3.scaleThreshold() 
  
            // Setting domain for the scale 
            .domain([1, 2, 3, 4]) 
            .range(["String1", "string2",  
                    "string3", "string4"]); 
  
        console.log("The value of ordinal(1) is:", 
                ordinal(1)); 
        console.log("The value of ordinal(2) is:", 
                ordinal(2)); 
        console.log("The value of ordinal(3) is:", 
                ordinal(3)); 
        console.log("When value given is not" 
                + " in the domain:") 
        console.log("The value of ordinal(4) is:", 
                ordinal(10)); 
    </script> 
</body> 
  
</html>

输出:




相关用法


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