Saturday, December 28, 2024
Google search engine
HomeLanguagesJavascriptD3.js scaleOrdinal ordinal() Function

D3.js scaleOrdinal ordinal() Function

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:

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments