Skip to content
Related Articles

Related Articles

Improve Article
D3.js ordinal.copy() Function
  • Last Updated : 18 Sep, 2020

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=
    </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=
    </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:




My Personal Notes arrow_drop_up
Recommended Articles
Page :