Macaw.Draw.Ticks (macaw_draw v0.1.0)

Copy Markdown View Source

The one tick rule, and the tick count from an axis length (spec/06 §16).

The rule is d3-array's ticks, tickIncrement and tickStep and d3-scale's linear nice, ported exactly from visualize's Visualize.Ticks (visualize spec/03 §1.5, #878, D-308). Macaw.Draw.Scale.Linear (and through it Power, Symlog and Color), Macaw.Draw.Scale.Log and Macaw.Draw.Scale.Time's year step call it; no other module computes a nice step.

A step is an increment: {:step, s}, a step of one or more, or {:per_unit, k}, k whole steps per unit for a step below one. Holding the small step by its inverse is d3's way to exact decimals: the tick 3 / 10 is the double nearest 0.3, where 3 * 0.1 is 0.30000000000000004. Ticks are whole numbers of steps, never a running sum, so no rounding pass follows.

A count is a hint: any positive number, a count at or below zero read as 1 (spec/06 §1.4). An argument of the wrong type is {:error, faults}, the first argument at [] and the others at [n].

test/support/golden/ticks.jsonl holds this module and its callers to d3's own answers.

Summary

Types

A refusal: faults with their paths.

Functions

d3-scale's linear nice(count) over [start, stop]: widened to whole steps until the step settles, in the span's own orientation. A collapsed span, and one that has not settled after ten passes, is answered as it was.

d3's tickStep(start, stop, count): the step itself as a float, negative for a descending span, and 0.0 for a collapsed one.

The tick count an axis length pixels long asks for (spec/06 §16, visualize D-134): ⌊|length| / spacing⌋ held to 2..10, the spacing Observable Plot's: 80 px a tick across a :top or :bottom axis, 35 px down a :left or :right one. The orientation is an atom or its string.

d3's ticks(start, stop, count): about count nice values inside the span, ascending, or descending when start > stop; [start] for a zero span.

Types

error()

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

A refusal: faults with their paths.

Functions

nice(start, stop, count)

@spec nice(number(), number(), number()) :: {number(), number()} | error()

d3-scale's linear nice(count) over [start, stop]: widened to whole steps until the step settles, in the span's own orientation. A collapsed span, and one that has not settled after ten passes, is answered as it was.

iex> Macaw.Draw.Ticks.nice(10.3, 0.2, 10)
{11.0, 0.0}

step(start, stop, count)

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

d3's tickStep(start, stop, count): the step itself as a float, negative for a descending span, and 0.0 for a collapsed one.

iex> Macaw.Draw.Ticks.step(0, 24, 10)
2.0
iex> Macaw.Draw.Ticks.step(1, 0, 5)
-0.2

tick_count(length, orient)

@spec tick_count(number(), atom() | String.t()) :: 2..10 | error()

The tick count an axis length pixels long asks for (spec/06 §16, visualize D-134): ⌊|length| / spacing⌋ held to 2..10, the spacing Observable Plot's: 80 px a tick across a :top or :bottom axis, 35 px down a :left or :right one. The orientation is an atom or its string.

iex> Macaw.Draw.Ticks.tick_count(600, :bottom)
7
iex> Macaw.Draw.Ticks.tick_count(300, "left")
8

ticks(start, stop, count)

@spec ticks(number(), number(), number()) :: [number()] | error()

d3's ticks(start, stop, count): about count nice values inside the span, ascending, or descending when start > stop; [start] for a zero span.

iex> Macaw.Draw.Ticks.ticks(0, 1, 5)
[0.0, 0.2, 0.4, 0.6, 0.8, 1.0]
iex> Macaw.Draw.Ticks.ticks(0, 1, 10) |> Enum.at(3)
0.3