Open In App

D3.js ordinal.range() Function

The ordinal.range() function in d3.js is used to set the range for the ordinal scale.  If in the range there are less number of elements as compared to domain the values then the scale reuse values of the specified range from starting.

Syntax:



ordinal.range([range]);

Parameters: This function takes only one parameter as given above and described below.

Return Values: This function does not return anything.



Example 1:




<!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>
    <h2 style="color:green">
        GeeksforGeeks
    </h2>
  
    <p>ordinal.range() Function </p>
  
    <script>
        var ordinal = d3.scaleThreshold()
  
            // Setting domain for the scale
            .domain([1, 2, 3, 4])
          
            // Range for the scale
            .range(["red", "green", "blue"]);
  
        document.write("<h3>ordinal(1): "
                + ordinal(1) + "</h3>");
        document.write("<h3>ordinal(3): "
                + ordinal(3) + "</h3>");
    </script>
</body>
  
</html>

Output:

Example 2:




<!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>
    <h2 style="color:green">
        GeeksforGeeks
    </h2>
  
    <p>ordinal.range() Function </p>
  
    <script>
        var ordinal = d3.scaleThreshold()
  
            // Setting domain for the scale
            .domain([-2, 3, 4])
  
        // 10 does not lie in the domain
        document.write("<h3>ordinal(-3): "
                + ordinal(-3) + "</h3>");
        document.write("<h3>ordinal(10): " 
                + ordinal(10) + "</h3>");
    </script>
</body>
  
</html>

Output:


Article Tags :