سؤال

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top