# `Macaw.Draw.Format`
[🔗](https://github.com/dcoai/macaw_draw/blob/v0.1.0/lib/macaw/draw/format.ex#L1)

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.

# `error`

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

A refusal: faults with their paths.

# `t`

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

# `for_scale`

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

`for_scale(scale, [])`.

# `for_scale`

```elixir
@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!`

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

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

# `notation`

```elixir
@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`

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

`time_pattern(domain, [])`.

# `time_pattern`

```elixir
@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`

```elixir
@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.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
