Giggly.Label protocol (giggly v0.2.0)

Copy Markdown View Source

A label another library lays out and draws, given wherever a plot takes a title's string: any Giggly.Plot.labs/2 title, a scale's name:, a secondary axis's name: or a guide's title:; a text or label annotation's label: (Giggly.Plot.annotate/3); a scale's labels:, in its list or map or returned by its labels function, a break's label on an axis or a legend's key's label; a facet's strip, given by a labeller: function or map; and a text or label layer's rows, each row's label handed to its label_with: function (Giggly.Plot.geom_text/3, Giggly.Plot.geom_label/3), as ggplot2's parse = TRUE parses it. A library that sets maths or other markup implements this protocol for a struct of its own, and that struct then goes where a string would.

When the plot is drawn, Giggly asks the label for its layout in the style the theme gives that title, or the annotation gives that label (Giggly.Label.Style: its size, colour, opacity, font family and face), and the label answers with its box and its drawing (Giggly.Label.Layout). Giggly then measures a title by that box and places it in its room, or places an annotation's label at its point, turning and justifying it as it would a string. The label is never told its angle or its justification.

Implementing a label

defmodule MyPlugin.Boxed do
  defstruct [:text]
end

defimpl Giggly.Label, for: MyPlugin.Boxed do
  def layout(%MyPlugin.Boxed{}, %Giggly.Label.Style{size: size, color: color}) do
    width = 4.0 * size
    height = 1.2 * size

    %Giggly.Label.Layout{
      width: width,
      ascent: 0.9 * size,
      descent: 0.3 * size,
      svg: [
        "<svg viewBox='0 0 #{width} #{height}'>",
        "<rect width='100%' height='100%' fill='#{color}' /></svg>"
      ]
    }
  end
end

Giggly.Plot.labs(plot, title: %MyPlugin.Boxed{text: "a title"}) then titles the plot with it.

How a label differs from a string

A string's box is its font's: as tall as the ascent of "M" above the baseline and the font's descent below it, whatever its letters. A label's box is whatever its layout says, usually its glyphs' own bounds, so a title holding a label can take a fraction of a point more or less room than the same text as a string. Its style carries the theme's face but not its line spacing: a label running over several lines breaks and spaces them by its own markup. Its drawing is written into the SVG document as it is, so it is drawn as its library draws it whether Giggly.to_svg/2 writes text as text or as outlines.

A layout must depend on the label and the style alone, since a title is laid out more than once as the plot is drawn.

Summary

Types

t()

All the types that implement this protocol.

Functions

The label's layout in style: its box, in points, and its drawing, whose top left is the box's.

Types

t()

@type t() :: term()

All the types that implement this protocol.

Functions

layout(label, style)

@spec layout(t(), Giggly.Label.Style.t()) :: Giggly.Label.Layout.t()

The label's layout in style: its box, in points, and its drawing, whose top left is the box's.