Version: @tanstack/charts 1.0.0.
ConfiguredScaleLike.range is typed (values: Iterable<number>) => ConfiguredScaleLike, which reads as if an immutable implementation is fine. The chart calls scale.range(range) and ignores the return value (configured-scale.ts). A hand-written scale whose range(values) returns a copy keeps its original range, and positions land in that unscaled range with no error. d3-scale mutates and returns itself, so it works.
function immutableScale(domain = [0, 100], range = [0, 1]) {
const fn = (v: number) => range[0] + ((v - domain[0]) / (domain[1] - domain[0])) * (range[1] - range[0])
fn.domain = (...a: any[]) => (a.length ? immutableScale([...a[0]], range) : domain)
fn.range = (...a: any[]) => (a.length ? immutableScale(domain, [...a[0]]) : range)
fn.copy = () => immutableScale(domain, range)
return fn
}
Would it be possible to use the returned scale (scale = scale.range(range)), or to document that range() must mutate and return this? Throwing when a scale does not map to the range just applied would catch it either way. This surfaced while writing a small custom symlog scale.
Fixed in 1.1.0 through #184. We now retain the scale returned by
range(). Regression coverage checks pixel mapping, inversion, ticks, responsive dimensions, and that the original immutable scale remains unchanged.