d3-scale icon indicating copy to clipboard operation
d3-scale copied to clipboard

Immutable scales.

Open mbostock opened this issue 7 years ago • 3 comments

What if scales were immutable, and all methods that currently mutate scales instead returned new scales? It would eliminate the need for scale.copy, simplify the logic in d3-axis, and generally make scales easier to understand.

It would require a change to the implicit domain behavior of ordinal scales: these scales mutate on use! It’s not too onerous to require you to initialize the scale’s domain, however, and that is already a strongly recommended pattern for deterministic behavior and generating keys.

mbostock avatar Oct 26 '17 14:10 mbostock

FWIW, The mutation on use with ordinal scales really confused me the first time I came across it.

curran avatar Oct 27 '17 10:10 curran

I totally agree, I think immutability should be the way to go for simple data structures.

hhalex avatar Mar 17 '18 12:03 hhalex

I'm working on a charting "library" and was finding that immutable scales are pretty much a necessity to keep my sanity. Especially as I implement zooming which has to muck with the domain.

Have you thought about this further?

The other thing that I am thinking about is that the tick marks should probably also be separated. As is, you can't "wrap" a scale because if the original scales domain is not updates the tick marks are not generated appropriately. The log and linear tick marks would probably be roughly the same if separated. Have a tick mark generator and pass it a scale, and it can work it's magic.

thomassuckow avatar Apr 22 '20 00:04 thomassuckow