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


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。