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


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。