Open In App

D3.js scaleOrdinal ordinal() Function

Last Updated : 23 Sep, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

The ordinal() function in d3.js library is used to return a value from the specified range on giving an input corresponding to the specified domain.

Syntax:

ordinal(value);

Parameters: This function accepts a single parameter as given above and described below.

  • Value: This parameter accepts a value from the given domain.

Return Value: This function return a value from the range corresponding to the given input.

Below given are a few examples of the function given above.

Example 1:

HTML




<!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>
    <script>
        // Creating the Ordinal scale.
        var ordinal = d3.scaleThreshold()
          
            // Setting domain for the scale
            .domain([0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8])
          
            // Setting the range for the scale.
            .range([10, 20, 30, 40, 50, 60, 70, 80, 90]);
        console.log("ordinal(0.1)", ordinal(0.1));
        console.log("ordinal(0.2)", ordinal(0.2));
        console.log("ordinal(0.3)", ordinal(0.3));
        console.log("ordinal(0.4)", ordinal(0.4));
        console.log("ordinal(0.5)", ordinal(0.5));
        console.log("ordinal(0.6)", ordinal(0.6));
        console.log("ordinal(0.7)", ordinal(0.7));
        console.log("ordinal(0.8)", ordinal(0.8));
    </script>
</body>
  
</html>


Output:

Example 2:

HTML




<!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>
    <script>
        // Creating the Ordinal scale.
        var ordinal = d3.scaleThreshold()
  
            // Setting domain for the scale
            .domain([1, 2, 3, 4, 5, 6, 7, 8])
          
            // Setting the range for the scale.
            .range(["one", "two", "three", "four",
                50, 60, 70, 80, 90]);
        console.log("ordinal(1)", ordinal(1));
        console.log("ordinal(2)", ordinal(2));
        console.log("ordinal(3)", ordinal(3));
        console.log("ordinal(4)", ordinal(4));
    </script>
</body>
  
</html>


Output:



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads