Giggly.Geom behaviour (giggly v0.2.0)

Copy Markdown View Source

How a layer's rows are drawn: the options a geom takes and the aesthetics it draws with, which a call is checked against, and setting its rows up during the build and drawing each panel's rows, each legend's keys and the room each key needs.

A geom is a struct whose module adopts this behaviour. Given to Giggly.Plot.layer/3's geom:, or any stat_ function's, a geom outside Giggly draws a layer as Giggly's own do. The functions of this module call the geom's own (draw_panel/3 calls the module's draw_panel/3), so one geom draws through another by calling them.

Writing a geom

use Giggly.Geom adopts the behaviour and gives every callback but draw_panel/3 a default: no aesthetics drawn with, needed, written or required to be numbers; no options, none in conflict, and a struct built from the options options/0 names; the rows as they are once set up; and a key of nothing, needing no room. A geom states what it has and draws its panels:

defmodule MyPlugin.Band do
  use Giggly.Geom

  alias Giggly.{DataFrame, Geom}

  defstruct []

  @impl Giggly.Geom
  def accepted_aesthetics(_band), do: [:x, :y, :height, :group, :fill, :color, :alpha]

  @impl Giggly.Geom
  def required_aesthetics(_band), do: [:x, :y, :height]

  @impl Giggly.Geom
  def default_aesthetics(_band, element), do: Geom.default_aesthetics(ribbon(), element)

  @impl Giggly.Geom
  def setup_data(_band, frame, axis_types) do
    tops =
      Enum.zip_with(DataFrame.pull(frame, :y), DataFrame.pull(frame, :height), &+/2)

    ranged = frame |> DataFrame.put(:ymin, DataFrame.pull(frame, :y)) |> DataFrame.put(:ymax, tops)
    Geom.setup_data(ribbon(), ranged, axis_types)
  end

  @impl Giggly.Geom
  def draw_panel(_band, frame, context), do: Geom.draw_panel(ribbon(), frame, context)

  @impl Giggly.Geom
  def draw_key(_band, key, context), do: Geom.draw_key(ribbon(), key, context)

  defp ribbon, do: Geom.new(Geom.Ribbon, orientation: :x)
end

Drawing by delegation

A geom draws its rows by handing them to one of Giggly's geoms: Giggly.Geom.Point, Giggly.Geom.Path, Giggly.Geom.Segment, Giggly.Geom.Polygon, Giggly.Geom.Rect, Giggly.Geom.Ribbon and Giggly.Geom.Text. Each documents the rows it takes and how it is built. Three rules make the delegation work:

  • Set up and draw through the same geom. A geom's setup ends by handing its rows to the delegate's setup_data/3, and its drawing hands them to the delegate's draw_panel/3: a delegate's drawing reads what its own setup writes.
  • The rows hold every aesthetic the delegate draws with. A geom's default_aesthetics/2 gives the delegate's, replacing those it draws otherwise, so the layer's table holds each.
  • Build the delegate with new/2, so the options a geom gives it are checked as a layer's are and those it leaves out take their defaults, as above.

A geom may draw some of its rows through one delegate and others through another, or a panel's rows a group at a time, joining what each draws.

Summary

Types

An arrowhead a layer puts on each of its lines, as its arrow: option gives one: the angle of each arm from the line, in degrees, each arm's length, in points, or {:npc, share}, a share of the shorter side of the panel it is drawn in, which ends get one, whether it is the arms alone or a triangle, and a triangle's fill, a colour, :none, or :color, the line's own.

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

The layers' defaults a plot's complete theme gives, which a geom's default_aesthetics/2 reads: ink, paper and accent, colours as "#RRGGBB" or "#RRGGBBAA"; linewidth and borderwidth, the lines' and borders' widths in millimetres; linetype and bordertype, their line types as dash patterns ({} solid); family and fontsize, the text's font family and size in millimetres; and pointsize and pointshape, the points' size in millimetres and shape.

What a geom draws: a value a geom outside Giggly takes from the geom it delegates to and hands back unread.

t()

A geom: the struct of a module adopting this behaviour, its fields the geom's parameters.

Callbacks

The aesthetics the geom draws with, which a layer may map or set for it.

The aesthetics geom draws with, each with the value it takes where the layer neither maps nor sets it, worked out from element, the layers' defaults the plot's complete theme gives (element/0). Each becomes a column of the layer's table, in this order, and a value of nil is the table's missing value.

What geom draws a legend's key with, key holding the key's value of each aesthetic, nil for none, and flipped_aes, whether its layer's marks run across. A key's context places nothing: a key is drawn in no panel.

What geom draws frame's rows with in one panel, frame holding the panel's rows once set up, with every aesthetic it draws with. A geom places its rows through context (Giggly.Geom.Context) and draws them by handing them to one of Giggly's geoms, as the module documentation says.

The room, {width, height} in points, that what draw_key/3 draws for key needs in its key; a legend makes each key at least as big as the theme's key size.

The geom's struct from a call's validated options, reading only its own.

The aesthetics the geom draws with that must hold numbers, each with why, which a call mapping one to a column of another type is refused with.

What a call's validated options say that the geom cannot take together, each a reason that follows the function's name ("takes center: or boundary:, not both"), [] for none.

The options that set the geom's fields, as a NimbleOptions schema written as data, each with its type, its documentation and its default. A layer given the geom takes them beside its stat's, and validates the call against both.

The aesthetics the geom cannot be drawn without: each a name, or a list of names of which one is enough; {:family, families}, one family of positions whole, such as a range's ends along x or along y; or {name, reason}, a column no layer maps that only some stat writes, refused with reason where none does. Each is met by a value mapped before the stat, an option setting it, or a column the stat writes.

frame with each row given the extent geom draws it over, in the data's positions, after the layer's stat and before its position, so a position moves that extent and the axes cover it. axis_types says whether each axis is discrete, continuous or binned; a geom sees an axis's scale type, never its range. A geom drawn at each row's x and y alone returns the frame as it is.

The aesthetics the geom's own setup writes (Giggly.Geom.setup_data/3), such as a band's ends, which the layer's position then finds; [] for a geom whose setup writes none.

Functions

geom's defaults, its module's default_aesthetics/2, its colour and its fill replaced by the theme's where element gives them, as every ggplot2 geom reads colour %||% and fill %||% its own.

What geom's module's draw_key/3 draws a legend's key with.

What geom's module's draw_panel/3 draws frame's rows with in one panel.

The room geom's module's key_size/3 says a key needs.

component's geom built from options, the way a geom drawing by delegation builds the one it draws through: options checked against the component's options/0, each given once and an option it does not take refused, those left out taking their defaults, what it cannot take together (option_conflicts/1) refused, and its struct built by its new/1.

frame set up by geom's module's setup_data/3.

Types

arrow()

@type arrow() :: %{
  angle: float(),
  length: float() | {:npc, float()},
  ends: :first | :last | :both,
  type: :open | :closed,
  fill: String.t() | :none | :color
}

An arrowhead a layer puts on each of its lines, as its arrow: option gives one: the angle of each arm from the line, in degrees, each arm's length, in points, or {:npc, share}, a share of the shorter side of the panel it is drawn in, which ends get one, whether it is the arms alone or a triangle, and a triangle's fill, a colour, :none, or :color, the line's own.

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.

element()

@type element() :: %{required(atom()) => term()}

The layers' defaults a plot's complete theme gives, which a geom's default_aesthetics/2 reads: ink, paper and accent, colours as "#RRGGBB" or "#RRGGBBAA"; linewidth and borderwidth, the lines' and borders' widths in millimetres; linetype and bordertype, their line types as dash patterns ({} solid); family and fontsize, the text's font family and size in millimetres; and pointsize and pointshape, the points' size in millimetres and shape.

primitive()

@opaque primitive()

What a geom draws: a value a geom outside Giggly takes from the geom it delegates to and hands back unread.

t()

@type t() :: struct()

A geom: the struct of a module adopting this behaviour, its fields the geom's parameters.

Callbacks

accepted_aesthetics(geom)

@callback accepted_aesthetics(geom :: t()) :: [atom()]

The aesthetics the geom draws with, which a layer may map or set for it.

default_aesthetics(geom, element)

@callback default_aesthetics(geom :: t(), element()) :: keyword()

The aesthetics geom draws with, each with the value it takes where the layer neither maps nor sets it, worked out from element, the layers' defaults the plot's complete theme gives (element/0). Each becomes a column of the layer's table, in this order, and a value of nil is the table's missing value.

draw_key(geom, key, t)

@callback draw_key(
  geom :: t(),
  key :: %{optional(atom()) => term()},
  Giggly.Geom.Context.t()
) :: [
  primitive()
]

What geom draws a legend's key with, key holding the key's value of each aesthetic, nil for none, and flipped_aes, whether its layer's marks run across. A key's context places nothing: a key is drawn in no panel.

draw_panel(geom, t, t)

@callback draw_panel(geom :: t(), Giggly.DataFrame.t(), Giggly.Geom.Context.t()) :: [
  primitive()
]

What geom draws frame's rows with in one panel, frame holding the panel's rows once set up, with every aesthetic it draws with. A geom places its rows through context (Giggly.Geom.Context) and draws them by handing them to one of Giggly's geoms, as the module documentation says.

key_size(geom, key, t)

@callback key_size(
  geom :: t(),
  key :: %{optional(atom()) => term()},
  Giggly.Geom.Context.t()
) ::
  {float(), float()}

The room, {width, height} in points, that what draw_key/3 draws for key needs in its key; a legend makes each key at least as big as the theme's key size.

new(options)

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

The geom's struct from a call's validated options, reading only its own.

numeric_aesthetics(geom)

@callback numeric_aesthetics(geom :: t()) :: [{atom(), String.t()}]

The aesthetics the geom draws with that must hold numbers, each with why, which a call mapping one to a column of another type is refused with.

option_conflicts(options)

@callback option_conflicts(options :: keyword()) :: [String.t()]

What a call's validated options say that the geom cannot take together, each a reason that follows the function's name ("takes center: or boundary:, not both"), [] for none.

options()

@callback options() :: keyword()

The options that set the geom's fields, as a NimbleOptions schema written as data, each with its type, its documentation and its default. A layer given the geom takes them beside its stat's, and validates the call against both.

required_aesthetics(geom)

@callback required_aesthetics(geom :: t()) :: [
  atom() | [atom()] | {:family, [[atom()]]} | {atom(), String.t()}
]

The aesthetics the geom cannot be drawn without: each a name, or a list of names of which one is enough; {:family, families}, one family of positions whole, such as a range's ends along x or along y; or {name, reason}, a column no layer maps that only some stat writes, refused with reason where none does. Each is met by a value mapped before the stat, an option setting it, or a column the stat writes.

setup_data(geom, t, axis_types)

@callback setup_data(geom :: t(), Giggly.DataFrame.t(), axis_types()) ::
  Giggly.DataFrame.t()

frame with each row given the extent geom draws it over, in the data's positions, after the layer's stat and before its position, so a position moves that extent and the axes cover it. axis_types says whether each axis is discrete, continuous or binned; a geom sees an axis's scale type, never its range. A geom drawn at each row's x and y alone returns the frame as it is.

written_aesthetics(geom)

@callback written_aesthetics(geom :: t()) :: [atom()]

The aesthetics the geom's own setup writes (Giggly.Geom.setup_data/3), such as a band's ends, which the layer's position then finds; [] for a geom whose setup writes none.

Functions

default_aesthetics(geom, element)

@spec default_aesthetics(t(), element()) :: keyword()

geom's defaults, its module's default_aesthetics/2, its colour and its fill replaced by the theme's where element gives them, as every ggplot2 geom reads colour %||% and fill %||% its own.

draw_key(geom, key, context)

@spec draw_key(t(), %{optional(atom()) => term()}, Giggly.Geom.Context.t()) :: [
  primitive()
]

What geom's module's draw_key/3 draws a legend's key with.

draw_panel(geom, frame, context)

@spec draw_panel(t(), Giggly.DataFrame.t(), Giggly.Geom.Context.t()) :: [primitive()]

What geom's module's draw_panel/3 draws frame's rows with in one panel.

key_size(geom, key, context)

@spec key_size(t(), %{optional(atom()) => term()}, Giggly.Geom.Context.t()) ::
  {float(), float()}

The room geom's module's key_size/3 says a key needs.

new(component, options)

@spec new(module(), keyword()) :: t()

component's geom built from options, the way a geom drawing by delegation builds the one it draws through: options checked against the component's options/0, each given once and an option it does not take refused, those left out taking their defaults, what it cannot take together (option_conflicts/1) refused, and its struct built by its new/1.

iex> ribbon = Giggly.Geom.new(Giggly.Geom.Ribbon, orientation: :x, outline_type: :upper)
iex> is_struct(ribbon, Giggly.Geom.Ribbon)
true

Raises NimbleOptions.ValidationError naming an option the component does not take or one of another type, and ArgumentError for one given twice or options it cannot take together.

setup_data(geom, frame, axis_types)

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

frame set up by geom's module's setup_data/3.