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


d3.js scaleQuantile()用法及代碼示例

D3.js中的d3.scaleQuantile()函數用於創建並返回具有指定域和範圍的新分位數標度。如果未指定域或範圍,則默認情況下將每個值設置為空數組。

用法:

d3.scaleQuantile( domain, range )

參數:此函數接受上麵和下麵描述的兩個參數:

  • domain:它是僅整數的數組,用於定義域值的範圍。它是一個可選參數。默認值設置為空數組。
  • range:它是整數或字符串的數組。它是一個可選參數。默認值設置為空數組。

返回值:此函數不返回任何內容。

以下程序說明了D3.js中的d3.scaleQuantile()函數:



範例1:

HTML

<!DOCTYPE html> 
<html> 
  
<head> 
    <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> 
    <h1 style="color:green;">GeeksforGeeks</h1> 
  
    <p>d3.ScaleQuantile() Function </p> 
  
    <script> 
        // Using scaleQuantile function 
        var quantile = d3.scaleQuantile() 
            .domain([0, 1]) 
            .range(["blue", "yellow", "green"]); 
  
        document.write("<h3>" + 
            quantile(0) + "</h3>"); 
        document.write("<h3>" + 
            quantile(0.3) + "</h3>"); 
        document.write("<h3>" + 
            quantile(0.5) + "</h3>"); 
        document.write("<h3>" + 
            quantile(0.9) + "</h3>"); 
    </script> 
</body> 
  
</html>

輸出:

範例2:

HTML

<!DOCTYPE html> 
<html> 
  
<head> 
    <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> 
    <h1 style="color:green;"> 
        GeeksforGeeks 
    </h1> 
  
    <p>d3.ScaleQuantile() Function </p> 
  
    <script> 
        // Using the scaleQuantile function 
        var quantile = d3.scaleQuantile() 
            .domain([0, 1]) 
            .range([1, 100]); 
  
        document.write("<h3>" + 
            quantile(0) + "</h3>"); 
        document.write("<h3>" + 
            quantile(0.3) + "</h3>"); 
        document.write("<h3>" + 
            quantile(0.5) + "</h3>"); 
        document.write("<h3>" + 
            quantile(0.9) + "</h3>"); 
    </script> 
</body> 
  
</html>

輸出:




相關用法


注:本文由純淨天空篩選整理自tarun007大神的英文原創作品 D3.js scaleQuantile() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。