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


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


D3.js中的d3.deviation()函數用於返回給定數組元素的標準偏差。如果數組的元素少於兩個,則返回undefined。

用法:

d3.deviation(Array)

參數:此函數接受參數Array,該參數是要計算其標準偏差的元素的數組。這裏的元素應該是整數而不是字符串,否則返回未定義。


返回值:它返回給定數組元素的標準偏差。

以下程序說明了D3.js中的d3.deviation()函數。
示例1:

<html> 
  
<head> 
    <title> 
      Getting standard deviation of  
      the elements of given array 
  </title> 
</head> 
  
<body> 
    <script src='https://d3js.org/d3.v4.min.js'> 
  </script> 
  
    <script> 
        // initialising the array of elements 
        var Array1 = [10, 20, 30, 40, 50, 60]; 
        var Array2 = [1, 2]; 
        var Array3 = [0, 1.5, 6.8]; 
        var Array4 = [.8, .08, .008]; 
  
        // Calling to d3.deviation() function 
        A = d3.deviation(Array1); 
        B = d3.deviation(Array2); 
        C = d3.deviation(Array3); 
        D = d3.deviation(Array4); 
  
        // Getting standard deviation of the given array's element 
        document.write(A + "<br>"); 
        document.write(B + "<br>"); 
        document.write(C + "<br>"); 
        document.write(D + "<br>"); 
    </script> 
</body> 
  
</html>

輸出:

18.708286933869708
0.7071067811865476
3.572580766523457
0.4379589021814719

示例2:

<html> 
  
<head> 
    <title> 
      Getting standard deviation 
      of the elements of given array 
  </title> 
</head> 
  
<body> 
    <script src='https://d3js.org/d3.v4.min.js'> 
  </script> 
  
    <script> 
        // initialising the array of elements 
        var Array1 = [1]; 
        var Array2 = ["a", "b", "c"]; 
        var Array3 = [1, 2, "B", "C"]; 
        var Array4 = ["Geek", "Geeks", 2, 3, "GeeksforGeeks"]; 
  
        // Calling to d3.deviation() function 
        A = d3.deviation(Array1); 
        B = d3.deviation(Array2); 
        C = d3.deviation(Array3); 
        D = d3.deviation(Array4); 
  
        // Getting standard deviation of 
        // the given array's element 
        document.write(A + "<br>"); 
        document.write(B + "<br>"); 
        document.write(C + "<br>"); 
        document.write(D + "<br>"); 
    </script> 
</body> 
  
</html>

輸出:

undefined
undefined
0.7071067811865476
0.7071067811865476

注意:在上麵的輸出中,如果參數的元素或字符串少於一個,則返回未定義;如果參數的字符串包括一些整數值,則返回整數值的標準差。

參考: https://devdocs.io/d3~4/d3-array#deviation



相關用法


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