Skip to content
Source
Sign inSign up
TanStack/charts

A custom scale whose `range()` returns a new scale is accepted and maps to the unscaled range #177

Closedquantizor(GitHub profile, opens in a new tab) opened this issue 1 comment

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) // returns a copy
  fn.copy = () => immutableScale(domain, range)
  return fn
}
// as x: { scale: immutableScale() } with a dot mark, scene points land at x in [0, 1], not across the plot

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.

  1. 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.

  2. tannerlinsley closed this

Sign in to join this conversation.