The sequential.copy() function in D3.js is used to construct and return a copy of the original scale. Any changes in original scale will not affect the copy scale and vice-versa. Sequential scales are similar to continuous scales. In a continuous scale, mapping is done in a continuous way between domain and range. The only difference is that the output range of this scale is fixed by its interpolator and this range cannot be changed.
Syntax:
sequential.copy()
Parameters: This function does not accept any parameters.
Return Values: This function returns the copy of the original scale.
Below given are a few examples of sequential.copy() in D3.js:
Example 1:
<!DOCTYPE html> < html >
< head >
</ script >
</ head >
< body >
< h2 style = "color:green" >
GeeksforGeeks
</ h2 >
< h4 > D3.js | sequential.copy() Function </ h4 >
< script >
var sequential = d3.scaleSequential();
// Default scale is identity function
document.write("< h4 >From original scale:</ h4 >")
document.write("< p >sequential(0.2): ",
sequential(0.2) + "</ p >");
document.write("< p >sequential(0.5): ",
sequential(0.5) + "</ p >");
// Creating copy of the original scale
var sequentialCopy = sequential.copy();
document.write("< h4 >From copy scale:</ h4 >")
document.write("< p >sequentialCopy(0.2): ",
sequentialCopy(0.2) + "</ p >");
document.write("< p >sequentialCopy(0.5): ",
sequentialCopy(0.5) + "</ p >");
</ script >
</ body >
</ html >
|
Output:
Example 2: Any change in the copy scale does not affect the original scale.
<!DOCTYPE html> < html >
< head >
</ script >
</ head >
< body >
< h2 style = "color:green" >
GeeksforGeeks
</ h2 >
< h4 > D3.js | sequential.copy() Function </ h4 >
< script >
var sequential = d3.scaleSequential();
// Default scale is identity function
document.write("< h4 >From original scale:</ h4 >");
document.write("< p >sequential(0.4): ",
sequential(0.4) + "</ p >");
document.write("< p >sequential(0.6): ",
sequential(0.6) + "</ p >");
// Creating copy of the original scale
var sequentialCopy = sequential.copy();
sequentialCopy.domain([1, 2]);
document.write(
"< h4 >From copy scale after changing domain:</ h4 >");
document.write("< p >sequentialCopy(0.4): ",
sequentialCopy(0.4) + "</ p >");
document.write("< p >sequentialCopy(0.6): ",
sequentialCopy(0.6) + "</ p >");
</ script >
</ body >
</ html >
|
Output: