# `Giggly`
[🔗](https://github.com/giggleton/giggly/blob/v0.2.0/lib/giggly.ex#L1)

Giggly is a grammar-of-graphics plotting library for Elixir, modelled on ggplot2.

A plot is described with `Giggly.Plot`, which builds a value and runs nothing, and the functions
here take a finished plot and run it: `layer_data/2` shows the table a layer computes,
`panel_scales/2` the scales a panel is drawn on, `to_svg/2` draws the plot, and
`get_alt_text/1` gives the text a page shows in place of the drawing.
`resolution/2`, ggplot2's `resolution()`, takes values of your own instead: how finely they are
spaced, which a mark given no width takes its width from.

# `panel_scale`

```elixir
@type panel_scale() ::
  Giggly.Scale.Continuous.t()
  | Giggly.Scale.Discrete.t()
  | Giggly.Scale.Binned.t()
```

A panel's trained position scale, continuous, discrete or binned.

# `resolution_value`

```elixir
@type resolution_value() ::
  number()
  | :infinity
  | :neg_infinity
  | :nan
  | nil
  | Date.t()
  | DateTime.t()
  | NaiveDateTime.t()
  | Time.t()
```

A value `resolution/2` spaces: a number, an infinity, NaN or `nil`, a date, a date-time or a
time of day.

# `get_alt_text`

```elixir
@spec get_alt_text(Giggly.Plot.t()) :: String.t()
```

The plot's alternative text, for a page to give in place of its drawing: the `alt:` label
`Giggly.Plot.labs/2` sets, or `""` where it sets none or `:none`. A function given as `alt:` is
handed the plot, its labels those `labs/2` sets, and returns the text; raises `ArgumentError`
where it returns anything else.

    iex> plot =
    ...>   %{"wt" => [2.62, 2.875], "mpg" => [21.0, 22.8]}
    ...>   |> Giggly.Plot.new(%{x: :wt, y: :mpg})
    ...>   |> Giggly.Plot.geom_point()
    iex> Giggly.get_alt_text(plot)
    ""
    iex> plot
    ...> |> Giggly.Plot.labs(alt: "Heavier cars go fewer miles on a gallon")
    ...> |> Giggly.get_alt_text()
    "Heavier cars go fewer miles on a gallon"

# `layer_data`

```elixir
@spec layer_data(Giggly.Plot.t(), integer()) :: Giggly.DataFrame.t()
```

The table the layer at `index` computes, counting from 0 in the order the layers were added; the
first layer when no index is given.

Its columns are named by aesthetic, plus the `:panel` each row is drawn in and its `:group`, the
series it belongs to; the data's own column names do not appear. Its x and y columns hold
positions on the panel's scales: a discrete value is its level's position, counting from 1.
Every aesthetic the layer draws with is a column: one the layer neither maps nor sets holds its
default under the plot's theme, a point's `size` the theme's points' size, `nil` where the
default is none, as a point's `fill`.
A plot with no layers is run with the layer `Giggly.Plot.geom_blank/3` adds, which draws
nothing and takes the plot's mapping, so its table is that layer's.
Raises `ArgumentError` when the plot has no layer at `index`; a negative index is refused too,
not counted from the end.

    iex> plot =
    ...>   %{"cyl" => ["6", "4", "6"], "mpg" => [21.0, 22.8, 21.4]}
    ...>   |> Giggly.Plot.new(%{x: :cyl, y: :mpg})
    ...>   |> Giggly.Plot.geom_point()
    iex> frame = Giggly.layer_data(plot)
    iex> Giggly.DataFrame.names(frame)
    [:x, :y, :panel, :group, :shape, :color, :fill, :size, :alpha, :stroke]
    iex> Giggly.DataFrame.pull(frame, :x)
    [2, 1, 2]
    iex> Giggly.DataFrame.pull(frame, :panel)
    [1, 1, 1]
    iex> Giggly.DataFrame.pull(frame, :group)
    [2, 1, 2]

# `panel_scales`

```elixir
@spec panel_scales(Giggly.Plot.t(), integer()) :: %{
  x: panel_scale(),
  y: panel_scale()
}
```

The trained x and y scales of the panel with id `panel`, counting from 1 as panel ids do; the
first panel when no id is given. Raises `ArgumentError` when the plot has no such panel.

    iex> plot =
    ...>   %{"cyl" => ["6", "4", "6"], "mpg" => [21.0, 22.8, 21.4]}
    ...>   |> Giggly.Plot.new(%{x: :cyl, y: :mpg})
    ...>   |> Giggly.Plot.geom_point()
    iex> Giggly.panel_scales(plot)
    %{
      x: %Giggly.Scale.Discrete{
        levels: ["4", "6"],
        factor: false,
        continuous_range: {1, 2},
        palette: :identity,
        na_value: nil,
        guide: :default
      },
      y: %Giggly.Scale.Continuous{
        range: {21.0, 22.8},
        transform: :identity,
        palette: :identity,
        na_value: nil,
        guide: :default
      }
    }

# `resolution`

```elixir
@spec resolution([resolution_value()], keyword()) :: float() | :infinity
```

How finely `values` are spaced: the smallest gap between two of them, ggplot2's `resolution()`.

A mark with a width in data units and no width given takes it from its positions' resolution,
as a bar is 0.9 of it wide, so neighbouring marks never overlap; this gives the same answer for
values of your own. Values whose range is zero wide, one value among them, are 1 apart, and so
are integers alone, which are taken to be discrete, so `[2, 10, 20, 50]` is 1 apart where
`[2.0, 10.0, 20.0, 50.0]` is 2. Gaps smaller than the square root of the machine epsilon are
rounding error and left out, and with none left the values are 1 apart. A missing value
(`nil`) or `:nan` is left out; `:infinity` and `:neg_infinity` are kept, so where every gap is
to an infinity the answer is `:infinity`. A `Date` counts as its days since 1970-01-01, a
`DateTime` or `NaiveDateTime` as its seconds since 1970-01-01 00:00 and a `Time` as its seconds
since midnight, so counting from zero counts from 1970 or from midnight.

## Options

* `:zero` (`t:boolean/0`) - Whether 0 counts among the values, so the gap from 0 to the nearest one counts too. The default value is `true`.

Raises `ArgumentError` for values of no type it takes, or of two, and
`NimbleOptions.ValidationError` for an option it does not take.

    iex> Giggly.resolution(Enum.to_list(1..10))
    1.0
    iex> Giggly.resolution(Enum.map(1..10, fn value -> value - 0.5 end))
    0.5
    iex> Giggly.resolution(Enum.map(1..10, fn value -> value - 0.5 end), zero: false)
    1.0
    iex> Giggly.resolution([2.0, 10.0, 20.0, 50.0])
    2.0
    iex> Giggly.resolution([2, 10, 20, 50])
    1.0
    iex> Giggly.resolution([~D[2020-01-01], ~D[2020-01-03], ~D[2020-01-06]])
    2.0
    iex> Giggly.resolution([1.0, :infinity], zero: false)
    :infinity

# `to_svg`

```elixir
@spec to_svg(Giggly.Plot.t(), keyword()) :: iodata()
```

The plot drawn as an SVG document, as iodata.

The drawing's size is given in pixels and written in the document in points, at 96 pixels to
the inch. Text, points and lines keep their sizes in points whatever the drawing's size, so a
smaller drawing gives them more of it. A plot with alt text (`get_alt_text/1`) is drawn as an
image a screen reader names by it, `role="img"` and `aria-label` on the document's root, since a
drawing set inline in a page has no `<img>` tag to carry `alt`.

## Options

* `:width` (`t:pos_integer/0`) - The drawing's width, in pixels. The default value is `700`.

* `:height` (`t:pos_integer/0`) - The drawing's height, in pixels. The default value is `433`.

* `:text` - How a label in a font Pica holds is written: `:outlines` draws it as the font's own shapes, keeping its words as unseen text; `:text` writes it as text naming its font. Every other label is written as text. The trade: the [fonts guide](fonts.md). The default value is `:outlines`.

* `:installed_fonts` (`t:boolean/0`) - In a project that lists Pica, whether a family no font is registered under with Pica is looked for among the machine's installed fonts too, by its exact name. `false` draws with the registered fonts alone. What the search costs, and which names it skips: the [fonts guide](fonts.md). The default value is `true`.

Raises `NimbleOptions.ValidationError` for an option it does not take or a size that is not a
positive whole number of pixels.

---

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