Macaw.Draw.Scale.Behaviour behaviour (macaw_draw v0.1.0)

Copy Markdown View Source

The protocol every scale module implements, and normalize/3 (spec/06 §2.3).

Macaw.Draw.Scale dispatches each of these operations to the module the scale struct names (scale.__struct__). A module that declares this behavior and omits a callback fails to compile under --warnings-as-errors, which is what keeps the facade total: no scale reaches Macaw.Draw.Scale.apply/2 without an apply/2 of its own.

Operations that do not apply to a scale kind are still defined and behave as identity (nice/1, padding/2, clamp/2), answer nil (invert/2) or answer 0 (bandwidth/1). The discretizing scales answer their thresholds from ticks/2.

Every callback answers {:error, faults} for an argument of the wrong type (spec/06 §1.2); a refusable setter answers {:ok, scale} or {:error, faults} for one of the right type, and a setter that cannot refuse answers the struct.

Summary

Types

A refusal: faults with their paths.

Any scale struct.

Callbacks

Maps a domain value to a range value; total, nil for a value it cannot place.

The band width; 0 for every scale but Band.

Enables or disables clamping; identity where the scale never extrapolates.

Sets the input domain; the argument's shape is the module's own.

Maps a range value back to the domain; nil where the scale is not invertible.

Extends the domain outward to round values; identity where that has no meaning.

Sets band padding; identity for every scale but Band.

Sets the output range; the argument's shape is the module's own.

Representative domain values; count is a hint, never a guarantee (spec/06 §1.4).

Functions

The position of value between a and b as a fraction, 0.5 when a == b.

Types

error()

@type error() :: {:error, [Macaw.Base.Fault.t()]}

A refusal: faults with their paths.

scale()

@type scale() :: struct()

Any scale struct.

Callbacks

apply(scale, term)

@callback apply(scale(), term()) :: term() | nil

Maps a domain value to a range value; total, nil for a value it cannot place.

bandwidth(scale)

@callback bandwidth(scale()) :: number() | error()

The band width; 0 for every scale but Band.

clamp(scale, boolean)

@callback clamp(scale(), boolean()) :: scale() | error()

Enables or disables clamping; identity where the scale never extrapolates.

domain(scale, term)

@callback domain(scale(), term()) :: {:ok, scale()} | error() | scale()

Sets the input domain; the argument's shape is the module's own.

invert(scale, term)

@callback invert(scale(), term()) :: term() | nil

Maps a range value back to the domain; nil where the scale is not invertible.

nice(scale)

@callback nice(scale()) :: scale() | error()

Extends the domain outward to round values; identity where that has no meaning.

padding(scale, number)

@callback padding(scale(), number()) :: {:ok, scale()} | error() | scale()

Sets band padding; identity for every scale but Band.

range(scale, term)

@callback range(scale(), term()) :: {:ok, scale()} | error() | scale()

Sets the output range; the argument's shape is the module's own.

ticks(scale, number)

@callback ticks(scale(), number()) :: [term()] | error()

Representative domain values; count is a hint, never a guarantee (spec/06 §1.4).

Functions

normalize(value, a, b)

@spec normalize(number(), number(), number()) :: float() | error()

The position of value between a and b as a fraction, 0.5 when a == b.

d3-scale's normalize: a collapsed domain maps every value to the middle of the range and a collapsed range inverts to the middle of the domain, instead of dividing by zero (visualize D-49). Every continuous scale's apply/2 and invert/2 compute it. A non-number in any position is a type fault at its path.

iex> Macaw.Draw.Scale.Behaviour.normalize(5, 0, 10)
0.5
iex> Macaw.Draw.Scale.Behaviour.normalize(3, 7, 7)
0.5