Visualize.Scale.Power (Visualize v0.2.25)

Copy Markdown View Source

Power scale for non-linear transformations.

Maps a continuous input domain to a continuous output range using a power function. The exponent determines the curve shape:

  • exponent = 1: linear
  • exponent = 0.5: square root (sqrt)
  • exponent = 2: quadratic
  • exponent = 3: cubic

The transform is D3's signed power T(x) = sign(x) · |x|^e, applied to the domain bounds and the value alike, so domains that include negative values map symmetrically and invert/2 returns negative domain values.

Examples

# Square root scale (good for area-based sizing)
scale = Visualize.Scale.Power.new()
  |> Visualize.Scale.Power.exponent(0.5)
  |> Visualize.Scale.Power.domain([0, 100])
  |> Visualize.Scale.Power.range([0, 10])

Visualize.Scale.Power.apply(scale, 25)  # => 5.0
Visualize.Scale.Power.apply(scale, 100) # => 10.0

# Quadratic scale
scale = Visualize.Scale.Power.new()
  |> Visualize.Scale.Power.exponent(2)
  |> Visualize.Scale.Power.domain([0, 10])
  |> Visualize.Scale.Power.range([0, 100])

Visualize.Scale.Power.apply(scale, 5)   # => 25.0

Summary

Functions

Maps a value from the domain to the range using the signed power transform

Returns 0: a power scale has no bandwidth

Enables or disables clamping of the output to the range

Sets the domain from a two-element list; a two-tuple is accepted and stored as a list

Sets the exponent for the power function

Maps a value from the range back to the domain (inverse)

Creates a new power scale with exponent 1 (linear)

Extends the domain to nice round values, as Linear.nice/1 does

Identity: a power scale has no padding

Sets the range from a two-element list; a two-tuple is accepted and stored as a list

Alias of apply/2, kept for one release (spec/03 §1.3)

Creates a square root scale (exponent 0.5)

Generates nice tick values, linearly spaced over the domain

Types

t()

@type t() :: %Visualize.Scale.Power{
  clamp?: boolean(),
  domain: [number()],
  exponent: number(),
  range: [number()]
}

Functions

apply(scale, value)

@spec apply(t(), number()) :: number()

Maps a value from the domain to the range using the signed power transform

bandwidth(power)

@spec bandwidth(t()) :: 0

Returns 0: a power scale has no bandwidth

clamp(scale, clamp?)

@spec clamp(t(), boolean()) :: t()

Enables or disables clamping of the output to the range

domain(scale, arg2)

@spec domain(t(), [number()] | {number(), number()}) :: t()

Sets the domain from a two-element list; a two-tuple is accepted and stored as a list

exponent(scale, exp)

@spec exponent(t(), number()) :: t()

Sets the exponent for the power function

invert(power, value)

@spec invert(t(), number()) :: number()

Maps a value from the range back to the domain (inverse)

new()

@spec new() :: t()

Creates a new power scale with exponent 1 (linear)

nice(scale)

@spec nice(t()) :: t()

Extends the domain to nice round values, as Linear.nice/1 does

padding(scale, padding)

@spec padding(t(), number()) :: t()

Identity: a power scale has no padding

range(scale, arg2)

@spec range(t(), [number()] | {number(), number()}) :: t()

Sets the range from a two-element list; a two-tuple is accepted and stored as a list

scale(scale, value)

@spec scale(t(), number()) :: number()

Alias of apply/2, kept for one release (spec/03 §1.3)

sqrt()

@spec sqrt() :: t()

Creates a square root scale (exponent 0.5)

ticks(power, count \\ 10)

@spec ticks(t(), integer()) :: [number()]

Generates nice tick values, linearly spaced over the domain