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

A discretizing scale by explicit thresholds (spec/06 §13): `n` thresholds divide the
numbers into `n + 1` buckets, one per range element.

    scale = Macaw.Draw.Scale.Threshold.new() |> Macaw.Draw.Scale.Threshold.domain!([60, 70, 80, 90])
    scale = Macaw.Draw.Scale.Threshold.range(scale, ["F", "D", "C", "B", "A"])
    Macaw.Draw.Scale.Threshold.apply(scale, 75)
    #=> "C"

A value equal to a threshold falls in the upper bucket. A range shorter than the
thresholds plus one answers `nil` for the highest buckets.

# `error`

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

A refusal: faults with their paths.

# `t`

```elixir
@type t() :: %Macaw.Draw.Scale.Threshold{domain: [number()], range: [term()]}
```

# `apply`

```elixir
@spec apply(t(), term()) :: term() | error()
```

The range element at `k`, the number of thresholds at or below the value, found by
binary search; `nil` for an empty range, a value that is no number, or a bucket past
the range's end.

# `bandwidth`

```elixir
@spec bandwidth(t()) :: 0 | error()
```

`0`: a threshold scale has no bandwidth.

# `clamp`

```elixir
@spec clamp(t(), boolean()) :: t() | error()
```

Identity: a threshold scale always maps into its range.

# `copy_with_domain`

```elixir
@spec copy_with_domain(t(), term(), term()) :: {:ok, t()} | error()
```

The scale with `n` thresholds taken from data (spec/06 §13.2): the de-duplicated sorted
values at indices `⌊i · len / (n + 1)⌋`, capped at `len − 1`, for `i` in `1 … n`; `[]` for
empty data. `n` at or below zero is read as 1; the range is untouched. `{:ok, scale}`,
or `{:error, faults}` with `{[i], {:type, :number}}` for a datum that is no number.

# `copy_with_domain!`

```elixir
@spec copy_with_domain!(t(), term(), term()) :: t()
```

`copy_with_domain/3`, answering the scale or raising `Macaw.Draw.Error`.

# `domain`

```elixir
@spec domain(t(), term()) :: {:ok, t()} | error()
```

Sets the thresholds, numbers, sorted on entry: `{:ok, scale}`, or `{:error, faults}`
with `{[i], {:type, :number}}` for an element that is not one.

# `domain!`

```elixir
@spec domain!(t(), term()) :: t()
```

`domain/2`, answering the scale or raising `Macaw.Draw.Error`.

# `invert`

```elixir
@spec invert(t(), term()) :: nil | error()
```

`nil`: a threshold scale maps many values to one bucket.

# `invert_extent`

```elixir
@spec invert_extent(t(), term()) ::
  {number() | :neg_infinity, number() | :infinity} | nil | error()
```

The bounds of the bucket a range element stands for: `{:neg_infinity, t_0}` for the
first (`{:neg_infinity, :infinity}` with no thresholds), `{t_last, :infinity}` for the
last, `{t_(k−1), t_k}` between; `nil` for an empty range or an element not in it.

# `new`

```elixir
@spec new() :: t()
```

A threshold scale with no thresholds and no range.

# `nice`

```elixir
@spec nice(t()) :: t() | error()
```

Identity.

# `padding`

```elixir
@spec padding(t(), number()) :: t() | error()
```

Identity: a threshold scale has no padding.

# `range`

```elixir
@spec range(t(), [term()]) :: t() | error()
```

Sets the range, a list, unchecked; answers the struct.

# `thresholds`

```elixir
@spec thresholds(t()) :: [number()] | error()
```

The thresholds, the domain.

# `ticks`

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

The thresholds; the count is ignored.

---

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