# `Macaw.Frame.Layer`
[🔗](https://github.com/dcoai/macaw_frame/blob/v0.1.0/lib/macaw/frame/layer.ex#L1)

The layer-kind behavior (spec/04 §1.1) and the checked layer every callback is given
(§1.6).

A frame composes **layers**. A layer is visualize's *mark*: it binds fields of its data
to the frame's scales and draws them with `Macaw.Draw`'s generators in the frame's
coordinate model. **A layer is never a component**: it has no region, no budget of its
own on the page, no state and no failure isolation. What a layer can be is a **layer
kind**, a registered module implementing this behavior (`Macaw.Frame.Layers`).
visualize held its sixteen mark types in one schema module (`Visualize.Chart.Schema
.Mark`); here each kind declares its own channels, options, scales, legend and summary
contributions, budget, validation and drawing, and every kind passes the layer kit
(`Macaw.Frame.LayerKit`).

**Every callback is pure** (no process, clock, randomness or ETS) and none raises on any
input. A refusal is a list of `Macaw.Base.Fault`s, each at its path in the frame
document.

## The checked layer (§1.6)

Every callback after validation is given a **checked layer** (`checked/2`): a map with
string keys, `"kind"` the kind's module and `"name"` its name, `"channels"` each in its
checked form (`{:field, f}`, a number, `{:scale, :min | :max | :boundary}`), `"options"`
every option present with its default, `"style"` the resolved element style,
`"label"` and `"tooltip"` their checked nodes (`"label"` with its resolved style under
`"style"`), `"scales"` its renames, `"offset"`, `"render"`, `"transition"`, `"id"`,
`"frame"`, `"data"`, its position among its frame's layers (`"index"`) and its path
among the document's (`"path"`). Beside them, `"context"` holds what the delivery and
the Frame give the drawing: `theme`, `mode`, `schema` (the slot's), `served` (the
delivery's served facts), `resolution` (device pixels per pixel), `position` (the
one-based position among the document's layers) and `styles` (the design's named
styles). A kind never reads the unchecked document.

## `use Macaw.Frame.Layer`

`use Macaw.Frame.Layer` declares the behavior and writes the callbacks most kinds share,
each overridable: `scales/2` by the model's channel table and the layer's renames,
`validate/2` answering `[]`, `legend/2` its series field with its paint's swatch,
`closed?/0` `false`, `bend?/0` `true`, `order/0` `:normal`, `targets/0` every renderer,
`parts/0` from `paint/0`, `assets/1` none, and `budget/2` `w`.

# `part`

```elixir
@type part() :: :fill | :stroke | :marker | :font | :text | :curve
```

A style part a kind draws (§1.7.3).

# `t`

```elixir
@type t() :: %{required(String.t()) =&gt; term()}
```

A checked layer (§1.6).

# `anchor`
*optional* 

```elixir
@callback anchor(map()) :: {number(), number()} | {number(), number(), tuple()} | nil
```

The inline label's anchor point of one drawn element (§1.8).

# `assets`
*optional* 

```elixir
@callback assets(t() | map()) :: %{proxy_routes: [map()], hooks: [String.t()]}
```

Media and hooks the kind needs, folded into the Frame's declarations.

# `bend?`

```elixir
@callback bend?() :: boolean()
```

Whether a polar frame bends it.

# `budget`

```elixir
@callback budget(t() | map(), non_neg_integer()) :: pos_integer() | nil
```

Its budget hint: rows it needs at a plot width in pixels, `nil` for no rows (§1.10).

# `channels`

```elixir
@callback channels() :: [Macaw.Frame.Layer.Channel.t()]
```

Every channel the kind reads, in order (§1.2).

# `closed?`

```elixir
@callback closed?() :: boolean()
```

Whether its elements are closed shapes, for stroke placement (§1.7.4).

# `draw`

```elixir
@callback draw(t(), Macaw.Data.Table.t(), Macaw.Frame.Coordinates.Model.t()) ::
  {:ok, Macaw.Draw.IR.Element.t()} | {:error, [Macaw.Base.Fault.t()]}
```

Its draw into the scene: one group in the plot's coordinates, every element keyed.

# `extent`
*optional* 

```elixir
@callback extent(map(), term()) :: number() | :none
```

The width a label fits into on an element at the label's point (§1.8.2).

# `legend`

```elixir
@callback legend(t(), String.t()) :: %{fields: [String.t()], swatch: :fill | :stroke}
```

Its legend contribution: the fields it shows by a scale and how its swatch reads.

# `models`

```elixir
@callback models() :: [atom()] | :any
```

The coordinate models the kind draws in, or `:any`.

# `name`

```elixir
@callback name() :: atom()
```

The kind's name, unique in the registry; a document spells it as a string.

# `options`

```elixir
@callback options() :: [{String.t(), Macaw.Data.Operations.Option.t()}]
```

The kind's geometry scalars, each declared as a `macaw_data` option with its default.

# `order`

```elixir
@callback order() :: :beneath | :normal
```

`:beneath` draws before every `:normal` layer of its frame (the tiles).

# `paint`

```elixir
@callback paint() :: :fill | :stroke | nil
```

Which key its series color and default color reach.

# `parts`

```elixir
@callback parts() :: [part()]
```

The style parts the kind draws (§1.7.3).

# `scales`

```elixir
@callback scales(t(), Macaw.Frame.Coordinates.Model.t()) :: %{
  required(String.t()) =&gt; String.t() | :pixels
}
```

The scale each of the layer's channels reads, by the model's table and the renames.

# `summary`

```elixir
@callback summary(t(), Macaw.Data.Table.t(), Macaw.Frame.Coordinates.Model.t()) ::
  Macaw.Frame.Layer.Summary.t()
```

Its text-summary contribution (§1.11).

# `targets`

```elixir
@callback targets() :: [:svg | :canvas | :binary]
```

The renderers it can draw on.

# `validate`

```elixir
@callback validate(map(), map()) :: [Macaw.Base.Fault.t()]
```

Its own checks of a layer node beyond the generic ones (§1.5).

# `callbacks`

```elixir
@spec callbacks() :: keyword(non_neg_integer())
```

The behavior's required callbacks, `[{name, arity}]`.

# `checked`

```elixir
@spec checked(term(), keyword()) :: {:ok, t()} | {:error, [Macaw.Base.Fault.t(), ...]}
```

The checked layer of a layer node (§1.6): `{:ok, layer}`, or the faults of what cannot
be checked. The node is the frame document's (atom or string keys); options:

  * `kind:` the kind's module (required);
  * `index:` its position among its frame's layers (`0`), `path:` its path in the
    document (`[:layers, index]`), `position:` its one-based position among the
    document's layers (from `path`);
  * `theme:` (`Macaw.Layers.Theme.default()`), `mode:` (`:css`), `styles:` the design's
    named styles, `defaults:` the containers' defaults farthest first, or `style:` the
    already resolved element style, as compilation holds it (spec/02 §12.2);
  * `schema:`, `served:` and `resolution:` from the delivery.

The layer's style stack is laid over its containers' defaults for its kind and resolved
in the theme; its label's over the `label` style and the containers' part defaults,
never the layer's style (§1.7.1).

# `kind?`

```elixir
@spec kind?(term()) :: boolean()
```

Whether a module implements every required callback of the behavior.

    iex> Macaw.Frame.Layer.kind?(Enum)
    false

# `reading`

```elixir
@spec reading(term()) :: term()
```

A channel's reading in its checked form (§1.3): a field's name (a string or an atom,
read by spelling) is `{:field, name}`; a number, `{:field, f}` and a scale end stand.

---

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