D3.js | d3.median() function

The d3.median() function in D3.js is used to return the median or mid point of the given array’s elements. If the array is empty then it returns undefined.

Syntax:

d3.median(Array)

Parameters: This function accepts a parameters Array which is an array of elements whose median are to be calculated. Here elements should be integers not string otherwise it returns undefined.



Return Value: It returns the median of the given array’s element.

Below programs illustrate the d3.median() function in D3.js.
Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<html>
  
<head>
    <title>
      Getting median 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.median() function
        A = d3.median(Array1);
        B = d3.median(Array2);
        C = d3.median(Array3);
        D = d3.median(Array4);
  
        // Getting median 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>

chevron_right


Output:

35
1.5
1.5
0.08

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<html>
  
<head>
    <title>
      Getting median 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 = [];
        var Array2 = ["a", "b", "c"];
        var Array3 = [1, "B", "C"];
        var Array4 = ["Geek", "Geeks", 2, 3, 6, "GeeksforGeeks"];
  
        // Calling to d3.median() function
        A = d3.median(Array1);
        B = d3.median(Array2);
        C = d3.median(Array3);
        D = d3.median(Array4);
  
        // Getting median 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>

chevron_right


Output:

undefined
undefined
1
3

Note: In the above output, if the parameter is empty or strings then it returns undefined and if the parameter is string including some integers value then the median of the integer’s value is returned.

Ref: https://devdocs.io/d3~4/d3-array#median



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.