Giggly.Position behaviour (giggly v0.2.0)

Copy Markdown View Source

Where a layer's marks move once its stat has run: piled on one another, set side by side, nudged, jittered, or anything a plugin's position does, and the options it takes and the aesthetics it needs and reads.

A position is a struct whose module adopts this behaviour. Given to a layer function's position:, alone or with its options, as Giggly.Plot.geom_point(plot, position: {MyPlugin.Lifted, by: 2.0}), it moves the layer's marks when the plot is assembled, as Giggly's own positions do. compute_layer/3 and default_aesthetics/1 call the position's own module's, so one position moves marks through another by calling them.

compute_layer/3 is handed the layer's table once its geom has set it up, every panel and group in it, and whether each axis is discrete, continuous or binned, and returns the table with the marks where they now stand, computed with Giggly.DataFrame's functions. Each aesthetic the position reads (default_aesthetics/1) is in every row, the value the layer sets or maps, else the position's default.

Writing a position

use Giggly.Position adopts the behaviour and gives every callback but compute_layer/3 a default: no aesthetics needed or read, no options, and a struct built from the options options/0 names. A module states what its position has and moves its marks:

defmodule MyPlugin.Lifted do
  use Giggly.Position

  alias Giggly.DataFrame

  defstruct [:by]

  @impl Giggly.Position
  def options, do: [by: [type: :float, default: 1.0, doc: "How far each mark moves up."]]

  @impl Giggly.Position
  def required_aesthetics(_lifted), do: [y: "it moves each mark up"]

  @impl Giggly.Position
  def compute_layer(lifted, frame, _axis_types),
    do: DataFrame.mutate(frame, y: {:+, [:y, {:constant, lifted.by}]})
end

Summary

Types

Whether each axis's scale type is discrete, continuous or binned, a binned axis's positions bin numbers.

t()

A position's struct.

Callbacks

The layer's table frame with its marks moved by position, each axis's scale type in axis_types.

The aesthetics the position reads, each with the value it takes where a layer neither sets nor maps it, which is put into every row before the position runs.

The position's struct from the validated options a layer's position: gives it.

The options that set the position's fields, as a NimbleOptions schema written as data, each with its type, its documentation and its default. A layer's position: given the position with options is validated against it.

The aesthetics the position cannot move a mark without, each with why: met by a value mapped before the stat, an option setting it, the stat writing it, or the geom's setup writing it. A layer missing one is refused when the plot runs, giving the reason.

Functions

The layer's table frame moved by position's module's compute_layer/3.

The aesthetics position reads, by its module's default_aesthetics/1.

Types

axis_types()

@type axis_types() :: %{
  x: :continuous | :discrete | :binned,
  y: :continuous | :discrete | :binned
}

Whether each axis's scale type is discrete, continuous or binned, a binned axis's positions bin numbers.

t()

@type t() :: struct()

A position's struct.

Callbacks

compute_layer(position, t, axis_types)

@callback compute_layer(position :: t(), Giggly.DataFrame.t(), axis_types()) ::
  Giggly.DataFrame.t()

The layer's table frame with its marks moved by position, each axis's scale type in axis_types.

default_aesthetics(position)

@callback default_aesthetics(position :: t()) :: %{optional(atom()) => term()}

The aesthetics the position reads, each with the value it takes where a layer neither sets nor maps it, which is put into every row before the position runs.

new(options)

@callback new(options :: keyword()) :: struct()

The position's struct from the validated options a layer's position: gives it.

options()

@callback options() :: keyword()

The options that set the position's fields, as a NimbleOptions schema written as data, each with its type, its documentation and its default. A layer's position: given the position with options is validated against it.

required_aesthetics(position)

@callback required_aesthetics(position :: t()) :: [{atom(), String.t()}]

The aesthetics the position cannot move a mark without, each with why: met by a value mapped before the stat, an option setting it, the stat writing it, or the geom's setup writing it. A layer missing one is refused when the plot runs, giving the reason.

Functions

compute_layer(position, frame, axis_types)

@spec compute_layer(t(), Giggly.DataFrame.t(), axis_types()) :: Giggly.DataFrame.t()

The layer's table frame moved by position's module's compute_layer/3.

default_aesthetics(position)

@spec default_aesthetics(t()) :: %{optional(atom()) => term()}

The aesthetics position reads, by its module's default_aesthetics/1.