Macaw.Draw.Axis (macaw_draw v0.1.0)

Copy Markdown View Source

A reference axis as scene elements: its domain line, tick marks and labels (spec/06 §17), d3-axis's geometry (visualize Visualize.Axis, D-23, D-55, D-56).

{:ok, scale} = Macaw.Draw.Scale.new(:linear, domain: [0, 100], range: [0, 200])
{:ok, axis} = Macaw.Draw.Axis.bottom(scale)
{:ok, svg} = axis |> Macaw.Draw.Axis.ticks(5) |> Macaw.Draw.Axis.render()

An axis takes only a position scale: a continuous one, a Band, or an Ordinal whose range is all numbers; any other is {:not_positional, kind}. Its colors and font are the theme's slots "axis", "text", "font_size" and "font_family", written as CSS references (var(--mw-axis, #666666)) or, with resolve: :literal, as plain values; nothing is currentColor, and the group is aria-hidden. An axis built by hand is d3's: ten ticks and the default label. Every setter answers the struct, and an argument of the wrong type its fault.

Summary

Types

A refusal: faults with their paths.

An axis's side.

t()

How a tick value becomes its label.

Functions

An axis below the plot: new(scale, :bottom).

bottom/1, answering the axis or raising Macaw.Draw.Error.

The default label (spec/06 §17.5): a float rounded to six decimals in plain notation, trailing zeros and point removed, never an exponent and never -0; any other value to_string/1, or inspected where it has none.

generate(axis, resolve: :css).

The axis as a scene group (spec/06 §17.4): {:ok, group}, or the faults of resolving its theme's slots at [:theme, slot]. :resolve is :css (the default) or :literal.

An axis left of the plot: new(scale, :left).

left/1, answering the axis or raising Macaw.Draw.Error.

An axis of a position scale on a side, the orientation an atom or its string: {:ok, axis}, or {:error, [{[], {:not_positional, kind}}]} for a scale that maps to range elements rather than positions.

new/2, answering the axis or raising Macaw.Draw.Error.

An offset added to every tick position (crisp half-pixel alignment).

render(axis, []).

The axis written by a target (spec/06 §17.6): generate/2 then Macaw.Draw.Render.to_string/2 with the same options, :resolve defaulting to the target's theme mode. {:ok, string} or the faults of either.

render/2, answering the string or raising Macaw.Draw.Error.

An axis right of the plot: new(scale, :right).

right/1, answering the axis or raising Macaw.Draw.Error.

The theme the colors, font and size resolve from, or nil for the default theme.

How a tick value becomes its label: a Macaw.Draw.Format, a Macaw.Format, a Macaw.Format.Time, a Macaw.Format.Measure, a 1-arity function, or nil for the default label.

The gap between a tick line's end and its label.

The position of a tick value (spec/06 §17.3): Scale.apply/2 plus the offset and d3's band centering, max(0, bandwidth − 2·offset) / 2, rounded when the band scale rounds; nil for a value the scale places nowhere. A frame's grid lines are drawn here too.

The inner and outer tick sizes together.

The length of each tick line.

The length of the domain line's end caps.

Explicit tick values, bypassing the scale's ticks, or nil.

The tick count hint given to the scale's ticks/2; ignored when tick_values is set.

An axis above the plot: new(scale, :top).

top/1, answering the axis or raising Macaw.Draw.Error.

Types

error()

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

A refusal: faults with their paths.

orient()

@type orient() :: :top | :right | :bottom | :left

An axis's side.

t()

@type t() :: %Macaw.Draw.Axis{
  offset: number(),
  orient: orient(),
  scale: struct() | nil,
  theme: Macaw.Layers.Theme.t() | nil,
  tick_format: tick_format(),
  tick_padding: number(),
  tick_size_inner: number(),
  tick_size_outer: number(),
  tick_values: [term()] | nil,
  ticks: number() | nil
}

tick_format()

@type tick_format() ::
  Macaw.Draw.Format.t()
  | Macaw.Format.t()
  | Macaw.Format.Time.t()
  | Macaw.Format.Measure.t()
  | (term() -> String.t())
  | nil

How a tick value becomes its label.

Functions

bottom(scale)

@spec bottom(struct()) :: {:ok, t()} | error()

An axis below the plot: new(scale, :bottom).

bottom!(scale)

@spec bottom!(term()) :: t()

bottom/1, answering the axis or raising Macaw.Draw.Error.

default_label(value)

@spec default_label(term()) :: String.t()

The default label (spec/06 §17.5): a float rounded to six decimals in plain notation, trailing zeros and point removed, never an exponent and never -0; any other value to_string/1, or inspected where it has none.

iex> Enum.map([2.0, 0.1 + 0.2, 1000.0, 0.00001, -0.0], &Macaw.Draw.Axis.default_label/1)
["2", "0.3", "1000", "0.00001", "0"]

generate(axis)

@spec generate(t()) :: {:ok, Macaw.Draw.IR.Element.t()} | error()

generate(axis, resolve: :css).

generate(axis, opts)

@spec generate(t(), keyword()) :: {:ok, Macaw.Draw.IR.Element.t()} | error()

The axis as a scene group (spec/06 §17.4): {:ok, group}, or the faults of resolving its theme's slots at [:theme, slot]. :resolve is :css (the default) or :literal.

generate!(axis, opts \\ [resolve: :css])

@spec generate!(t(), keyword()) :: Macaw.Draw.IR.Element.t()

generate/2, answering the group or raising Macaw.Draw.Error.

left(scale)

@spec left(struct()) :: {:ok, t()} | error()

An axis left of the plot: new(scale, :left).

left!(scale)

@spec left!(term()) :: t()

left/1, answering the axis or raising Macaw.Draw.Error.

new(scale, orient)

@spec new(struct(), orient() | String.t()) :: {:ok, t()} | error()

An axis of a position scale on a side, the orientation an atom or its string: {:ok, axis}, or {:error, [{[], {:not_positional, kind}}]} for a scale that maps to range elements rather than positions.

new!(scale, orient)

@spec new!(term(), term()) :: t()

new/2, answering the axis or raising Macaw.Draw.Error.

offset(axis, n)

@spec offset(t(), number()) :: t() | error()

An offset added to every tick position (crisp half-pixel alignment).

render(axis)

@spec render(t()) :: {:ok, String.t()} | error()

render(axis, []).

render(axis, opts)

@spec render(t(), keyword()) :: {:ok, String.t()} | error()

The axis written by a target (spec/06 §17.6): generate/2 then Macaw.Draw.Render.to_string/2 with the same options, :resolve defaulting to the target's theme mode. {:ok, string} or the faults of either.

render!(axis, opts \\ [])

@spec render!(t(), keyword()) :: String.t()

render/2, answering the string or raising Macaw.Draw.Error.

right(scale)

@spec right(struct()) :: {:ok, t()} | error()

An axis right of the plot: new(scale, :right).

right!(scale)

@spec right!(term()) :: t()

right/1, answering the axis or raising Macaw.Draw.Error.

theme(axis, t)

@spec theme(t(), Macaw.Layers.Theme.t() | nil) :: t() | error()

The theme the colors, font and size resolve from, or nil for the default theme.

tick_format(axis, f)

@spec tick_format(t(), tick_format()) :: t() | error()

How a tick value becomes its label: a Macaw.Draw.Format, a Macaw.Format, a Macaw.Format.Time, a Macaw.Format.Measure, a 1-arity function, or nil for the default label.

tick_padding(axis, n)

@spec tick_padding(t(), number()) :: t() | error()

The gap between a tick line's end and its label.

tick_position(axis, value)

@spec tick_position(t(), term()) :: number() | nil | error()

The position of a tick value (spec/06 §17.3): Scale.apply/2 plus the offset and d3's band centering, max(0, bandwidth − 2·offset) / 2, rounded when the band scale rounds; nil for a value the scale places nowhere. A frame's grid lines are drawn here too.

tick_size(axis, n)

@spec tick_size(t(), number()) :: t() | error()

The inner and outer tick sizes together.

tick_size_inner(axis, n)

@spec tick_size_inner(t(), number()) :: t() | error()

The length of each tick line.

tick_size_outer(axis, n)

@spec tick_size_outer(t(), number()) :: t() | error()

The length of the domain line's end caps.

tick_values(axis, v)

@spec tick_values(t(), [term()] | nil) :: t() | error()

Explicit tick values, bypassing the scale's ticks, or nil.

ticks(axis, n)

@spec ticks(t(), number()) :: t() | error()

The tick count hint given to the scale's ticks/2; ignored when tick_values is set.

top(scale)

@spec top(struct()) :: {:ok, t()} | error()

An axis above the plot: new(scale, :top).

top!(scale)

@spec top!(term()) :: t()

top/1, answering the axis or raising Macaw.Draw.Error.