Visualize.Scale.Quantize (Visualize v0.2.25)

Copy Markdown View Source

Quantize scale for mapping continuous values to discrete buckets.

Divides the continuous domain into uniform segments, each mapped to a discrete value from the range. Useful for choropleth maps and heat maps where you want to bin continuous data into categories.

Examples

# Map values 0-100 to colors
scale = Visualize.Scale.Quantize.new()
  |> Visualize.Scale.Quantize.domain([0, 100])
  |> Visualize.Scale.Quantize.range(["#f7fbff", "#c6dbef", "#6baed6", "#2171b5", "#084594"])

Visualize.Scale.Quantize.apply(scale, 10)  # => "#f7fbff"
Visualize.Scale.Quantize.apply(scale, 50)  # => "#6baed6"
Visualize.Scale.Quantize.apply(scale, 90)  # => "#084594"

# Get the thresholds
Visualize.Scale.Quantize.thresholds(scale)  # => [20, 40, 60, 80]

Summary

Functions

Maps a continuous value to a discrete range value

Returns 0: a quantize scale has no bandwidth

Returns the number of buckets

Identity: a quantize scale always clamps

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

Returns nil: a quantize scale maps many values to one bucket

Returns the extent of values in the domain that map to a given range value.

Creates a new quantize scale

Identity

Identity: a quantize scale has no padding

Sets the discrete output range

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

Returns the threshold values that separate the buckets

Returns the bucket thresholds (thresholds/1); the count is ignored

Types

t()

@type t() :: %Visualize.Scale.Quantize{domain: [number()], range: [any()]}

Functions

apply(quantize, value)

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

Maps a continuous value to a discrete range value

bandwidth(quantize)

@spec bandwidth(t()) :: 0

Returns 0: a quantize scale has no bandwidth

bucket_count(scale)

@spec bucket_count(t()) :: non_neg_integer()

Returns the number of buckets

clamp(scale, clamp?)

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

Identity: a quantize scale always clamps

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

invert(quantize, value)

@spec invert(t(), any()) :: nil

Returns nil: a quantize scale maps many values to one bucket

invert_extent(quantize, value)

@spec invert_extent(t(), any()) :: {number(), number()} | nil

Returns the extent of values in the domain that map to a given range value.

Returns {min, max} for the continuous values that map to the specified discrete value.

new()

@spec new() :: t()

Creates a new quantize scale

nice(scale)

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

Identity

padding(scale, padding)

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

Identity: a quantize scale has no padding

range(scale, values)

@spec range(t(), [any()]) :: t()

Sets the discrete output range

scale(scale, value)

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

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

thresholds(quantize)

@spec thresholds(t()) :: [number()]

Returns the threshold values that separate the buckets

ticks(scale, count)

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

Returns the bucket thresholds (thresholds/1); the count is ignored