Macaw.Frame.Layer behaviour (macaw_frame v0.1.0)

Copy Markdown View Source

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.Faults, 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.

Summary

Types

A style part a kind draws (§1.7.3).

t()

A checked layer (§1.6).

Callbacks

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

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

Whether a polar frame bends it.

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

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

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

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

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

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

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

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

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

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

Which key its series color and default color reach.

The style parts the kind draws (§1.7.3).

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

Its text-summary contribution (§1.11).

The renderers it can draw on.

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

Functions

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

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

Whether a module implements every required callback of the behavior.

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.

Types

part()

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

A style part a kind draws (§1.7.3).

t()

@type t() :: %{required(String.t()) => term()}

A checked layer (§1.6).

Callbacks

anchor(map)

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

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

assets(arg1)

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

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

bend?()

@callback bend?() :: boolean()

Whether a polar frame bends it.

budget(arg1, non_neg_integer)

@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()

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

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

closed?()

@callback closed?() :: boolean()

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

draw(t, t, t)

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

extent(map, term)

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

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

legend(t, t)

@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()

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

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

name()

@callback name() :: atom()

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

options()

@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()

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

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

paint()

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

Which key its series color and default color reach.

parts()

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

The style parts the kind draws (§1.7.3).

scales(t, t)

@callback scales(t(), Macaw.Frame.Coordinates.Model.t()) :: %{
  required(String.t()) => String.t() | :pixels
}

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

summary(t, t, t)

Its text-summary contribution (§1.11).

targets()

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

The renderers it can draw on.

validate(map, map)

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

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

Functions

callbacks()

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

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

checked(node, opts)

@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?(module)

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

Whether a module implements every required callback of the behavior.

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

reading(name)

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