Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

d3 scale: How to use .copy()?

Tags:

d3.js

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?

like image 690
Boxuan Avatar asked Feb 13 '23 23:02

Boxuan


1 Answers

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.

like image 55
Lars Kotthoff Avatar answered Feb 16 '23 04:02

Lars Kotthoff