Open In App

D3.js ordinal.copy() Function

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

The ordinal.copy() function is used to create and return the exact copy of the ordinal scale. Any change in the original scale will not affect the copy scale and vice-versa.

Syntax:

ordinal.copy();

Parameters: This method does not take any parameters.

Return Values: This method returns a copy of the original scale.

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([1, 2, 3, 4])
            .range([10, 20, 30, 40]);
  
        // Making copy of the scale.
        var ordinalCopy = ordinal.copy();
        console.log("The value of ordinalCopy(1) is: ",
                ordinalCopy(1));
        console.log("The value of ordinalCopy(2) is: ",
                ordinalCopy(2));
        console.log("The value of ordinal(1) is: ",
                ordinal(1));
        console.log("The value of ordinal(2) is: ",
                ordinal(2));
    </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])
            .range([10, 20, 30, 40, 50]);
          
        console.log("The value of ordinal(3) is: ",
                ordinal(3));
        console.log("The value of ordinal(4) is: ",
                ordinal(4));
  
        // Making copy of the scale.
        var ordinalCopy = ordinal.copy();
  
        // making change in the original scale.
        ordinal.range(["The range of the "
                + "original scale is change"]);
        console.log("The value of ordinalCopy(3) is: ",
                ordinalCopy(3));
        console.log("The value of ordinalCopy(4) is: ",
                ordinalCopy(4));
        console.log("The value of ordinal(3) is: ",
                ordinal(3));
        console.log("The value of ordinal(4) is: ",
                ordinal(4));
    </script>
</body>
  
</html>


Output:



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads