I came across the log.copy()
in the API reference. What is the purpose of this? Can someone show an example?
If I use a scale in one function, can I save it to a global variable by using .copy()
and retrieve it later?
The purpose is, as the name suggests, to copy a scale. You essentially get the same type of scale, domain and range twice without having to set everything twice. For example, consider the brush demo here. Instead of
var x = d3.time.scale().range([0, width]),
x2 = d3.time.scale().range([0, width]),
the code could be
var x = d3.time.scale().range([0, width]),
x2 = x.copy(),
which I have done here. The only difference is that the code is very slightly shorter.
If you only want to retrieve a scale, there's no need to copy it. Only when you want to modify it in two different ways independently you need different scales.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With