Macaw.Draw.Format (macaw_draw v0.1.0)

Copy Markdown View Source

The tick format derived from a realized scale, as data (spec/06 §18).

A realized scale knows its domain and its tick count, so it knows how its ticks should read: for_scale/2 answers %Macaw.Draw.Format{kind, format, zone, labels}, kind one of :time, :number and :quantity, format the macaw_format struct that writes a value (Macaw.Format.Time or Macaw.Format.Measure), zone a time scale's display zone, and labels the round-number labels of a quantity's placed ticks (Macaw.Draw.Quantity, spec/06 §19.3). It holds no function, so two derivations from one scale, count and locale are equal, and a compiled frame's axes can be compared.

Macaw.Draw never writes digits itself: every label is macaw_format's (Macaw.Format.time/2 or measure/2).

  • Time: one strftime pattern for every tick, from the span and the interval (time_pattern/2).
  • Numbers: one notation for the axis from the domain's magnitude (notation/1): :plain below 1,000, :grouped below a million, {:si, e} from a million, one SI prefix for every tick; Log and Symlog take :si, each value its own prefix, once the domain reaches a million.

Summary

Types

A refusal: faults with their paths.

t()

Functions

for_scale(scale, []).

The tick format of a realized scale (spec/06 §18.2): {:ok, format} for a time or a numeric scale, {:ok, nil} for one whose ticks take the default label, or {:error, faults}. Options: :count, the tick count (default 10), and :locale, a Macaw.Format.Locale (English by default).

for_scale/2, answering the format or raising Macaw.Draw.Error.

The notation a numeric domain's magnitude, its greatest |d|, calls for: :plain below 1,000, :grouped below a million, else {:si, e}, e the exponent of the largest SI prefix from M to Y not above it. Elements that are no number are passed over.

time_pattern(domain, []).

The strftime pattern for a time domain [t0, t1] (any of a time scale's values, either order) by its span and the interval count: ticks use over it (spec/06 §18.2): {:ok, pattern}, or a fault at [i] for an element that is no time value.

A value written by a derived format: a time by its pattern in the format's zone, a number by its notation, a quantity's placed tick by its round number's label. Never raises: a value of the wrong kind is written by to_string/1, or inspected.

Types

error()

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

A refusal: faults with their paths.

t()

@type t() :: %Macaw.Draw.Format{
  format: Macaw.Format.Time.t() | Macaw.Format.Measure.t() | nil,
  kind: :time | :number | :quantity,
  labels: %{optional(number()) => String.t()},
  zone: String.t() | nil
}

Functions

for_scale(scale)

@spec for_scale(Macaw.Draw.Scale.t()) :: {:ok, t() | nil} | error()

for_scale(scale, []).

for_scale(scale, opts)

@spec for_scale(Macaw.Draw.Scale.t(), keyword()) :: {:ok, t() | nil} | error()

The tick format of a realized scale (spec/06 §18.2): {:ok, format} for a time or a numeric scale, {:ok, nil} for one whose ticks take the default label, or {:error, faults}. Options: :count, the tick count (default 10), and :locale, a Macaw.Format.Locale (English by default).

for_scale!(scale, opts \\ [])

@spec for_scale!(Macaw.Draw.Scale.t(), keyword()) :: t() | nil

for_scale/2, answering the format or raising Macaw.Draw.Error.

notation(domain)

@spec notation([term()]) :: :plain | :grouped | {:si, 6..24} | error()

The notation a numeric domain's magnitude, its greatest |d|, calls for: :plain below 1,000, :grouped below a million, else {:si, e}, e the exponent of the largest SI prefix from M to Y not above it. Elements that are no number are passed over.

iex> Macaw.Draw.Format.notation([0, 2_500_000])
{:si, 6}

time_pattern(domain)

@spec time_pattern([term()]) :: {:ok, String.t()} | error()

time_pattern(domain, []).

time_pattern(domain, opts)

@spec time_pattern([term()], keyword()) :: {:ok, String.t()} | error()

The strftime pattern for a time domain [t0, t1] (any of a time scale's values, either order) by its span and the interval count: ticks use over it (spec/06 §18.2): {:ok, pattern}, or a fault at [i] for an element that is no time value.

iex> Macaw.Draw.Format.time_pattern([~D[2024-01-01], ~D[2024-03-01]])
{:ok, "%b %d"}

write(format, value)

@spec write(t(), term()) :: String.t()

A value written by a derived format: a time by its pattern in the format's zone, a number by its notation, a quantity's placed tick by its round number's label. Never raises: a value of the wrong kind is written by to_string/1, or inspected.