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

How a stat computes a layer's table before it is positioned and drawn: counting rows, binning
them, summarising them, or anything a plugin's stat does, and the options it takes and the
aesthetics it reads, needs, writes and refuses, which a call is checked against.

A stat is a struct whose module adopts this behaviour. Named in a geom function's `stat:`, as
`Giggly.Plot.geom_point(plot, stat: MyPlugin.Scaled)`, it takes that function's options and its
own, and the plot runs it when it is assembled. Every fact is a function of the struct, so a fact
that depends on the stat's options reads them and one that does not ignores them.

`c:compute_layer/3` is handed the layer's whole table, every panel and group in it: a column per
aesthetic the stat reads (`c:read_aesthetics/1`), each named by the aesthetic (`:x`, `:y`,
`:group`), and `:panel`, with the positions already mapped by their scales, so a discrete value
is its level's position, and every other aesthetic's column under its name as a string
(`"color"`), which it may carry along or drop but never mistakes for a column of its own. It
returns the table the geom draws, keeping `:panel` and `:group` for each row, computed with
`Giggly.DataFrame`'s functions, as Giggly's own stats compute theirs; each column it carried is
put back under its own name, unless it computed one of that name.

## Writing a stat

`use Giggly.Stat` adopts the behaviour and gives every callback but `c:compute_layer/3` a
default: no aesthetics accepted, needed, written, consumed, refused or required to be numbers, and
those accepted the ones read; no
default mapping; no computed variables; no exclusive groups; no orientation to check; no nudge
dropped; no way it runs recorded; not computing through the frame's operations alone; no
options, none in conflict, and a struct built from the
options `c:options/0` names. A module states the facts its stat has and computes its table, so a
callback added to this behaviour later reaches it with its default:

    defmodule MyPlugin.Scaled do
      use Giggly.Stat

      alias Giggly.DataFrame

      defstruct [:by]

      @impl Giggly.Stat
      def options, do: [by: [type: :float, default: 2.0, doc: "How many times y."]]

      @impl Giggly.Stat
      def accepted_aesthetics(_stat), do: [:x, :y, :group]

      @impl Giggly.Stat
      def required_aesthetics(_stat), do: [:x, :y]

      @impl Giggly.Stat
      def compute_layer(stat, frame, _axes),
        do: DataFrame.mutate(frame, y: {:*, [:y, {:constant, stat.by}]})
    end

Giggly's tests hold a whole stat written this way, `test/support/scaled_stat.ex`.

# `axes`

```elixir
@type axes() :: %{optional(term()) =&gt; %{x: axis(), y: axis()}}
```

Each panel's x and y axes, by panel.

# `axis`

```elixir
@type axis() :: %{
  limits: {number(), number()} | :none,
  discrete: boolean(),
  binned: boolean(),
  transform: (list() -&gt; list()),
  inverse: (list() -&gt; list())
}
```

One of a panel's axes as a stat reads it: its extent, unwidened, in transformed positions, or
`:none` where the axis has no scale before the stats run; whether it is discrete; whether it is
binned, its positions bin numbers; a function putting values in data units into transformed
positions, as a stat transforms breaks it is given; and one putting positions back into data
units, a discrete axis's as given.

# `orientation_rule`

```elixir
@type orientation_rule() :: :none | {:runs_along, String.t()} | :stands_along
```

What an `orientation:` given at the call must name, checked against the layer's mapping before
the stat: `{:runs_along, verb}`, the one of `x` and `y` mapped, `verb` what the stat does along it
(`"count"`); `:stands_along`, the other of the one mapped where only one is; `:none`, nothing to
check.

# `t`

```elixir
@type t() :: struct()
```

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

# `accepted_aesthetics`

```elixir
@callback accepted_aesthetics(stat :: t()) :: [atom()]
```

The aesthetics the stat reads as it computes, which a layer may map for it.

# `compute_layer`

```elixir
@callback compute_layer(stat :: t(), Giggly.DataFrame.t(), axes()) :: Giggly.DataFrame.t()
```

The layer's table `frame` after `stat` has computed on it, handed each panel's axes, by panel.

# `computed_variables`

```elixir
@callback computed_variables(stat :: t()) :: [atom()]
```

The columns the stat computes that a mapping's `{:after_stat, ...}` can read; an after_stat
naming any other is refused, the layer's own at its call and the plot's when the plot runs.

# `default_mapping`

```elixir
@callback default_mapping(stat :: t()) :: %{optional(atom()) =&gt; atom()}
```

The computed column each aesthetic takes by default once the stat has run: `%{y: :count}` puts
the count in `y`; `:none` puts nothing in and titles the aesthetic's guide by its own name. Empty
for a stat that maps nothing by default. A value the layer maps for one of them replaces it.

# `dropped_aesthetics`

```elixir
@callback dropped_aesthetics(stat :: t()) :: [atom()]
```

The aesthetics the stat consumes as it computes, so one mapped after it reaches nothing.

# `drops_nudges?`

```elixir
@callback drops_nudges?(stat :: t()) :: boolean()
```

Whether the stat drops a nudge mapped before it, so a layer nudging its marks refuses one its
own mapping maps there and takes one mapped after the stat: true for a stat that keeps only the
aesthetics it computes by, as counting at each place does.

# `exclusive_aesthetics`

```elixir
@callback exclusive_aesthetics(stat :: t()) :: [[atom()]]
```

Groups of aesthetics of which a layer maps at most one before the stat, as a stat that runs
along one of `x` and `y` and computes the other refuses both.

# `frame_operations?`

```elixir
@callback frame_operations?(stat :: t()) :: boolean()
```

Whether the stat computes through `Giggly.DataFrame`'s operations alone, never reading a column
of the table it is handed into lists, only columns of tables it has summarised from it, so the
table may be held in any frame that runs those operations. A stat that says not is handed its
table in Giggly's own frame.

# `new`

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

The stat's struct from a call's validated options, reading only its own; the options of the geom
and the layer are there too.

# `numeric_aesthetics`

```elixir
@callback numeric_aesthetics(stat :: t()) :: [{atom(), String.t()}]
```

The aesthetics the stat reads that must hold numbers, each with why: a column of another type,
or a constant, mapped before the stat is refused when the plot runs, giving the reason.

# `option_conflicts`

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

What a call's validated options say that the stat cannot take together, each a reason that
follows the function's name (`"takes center: or boundary:, not both, ..."`), `[]` for none. It
reads the options, not the struct, since a struct records a default where nothing was given.
The call raises the first.

# `options`

```elixir
@callback options() :: keyword()
```

The options that set the stat's fields, as a `NimbleOptions` schema written as data, each with
its type, its documentation and its default. A geom function given the stat in `stat:` takes
them beside its own, and validates the call against both; where both take a name, the stat's
definition of it stands.

# `orientation_rule`

```elixir
@callback orientation_rule(stat :: t()) :: orientation_rule()
```

What an orientation given at the call must name (`t:orientation_rule/0`).

# `read_aesthetics`

```elixir
@callback read_aesthetics(stat :: t()) :: [atom()]
```

The columns the stat reads as it computes. Every other column of the layer's table is handed to
`c:compute_layer/3` under its name as a string, so a column named as one the stat computes, or
as one it works with along the way, is never taken for it; once the stat has run, each is put
back under its own name, unless the stat computed a column of that name, which stands in its
place.

# `records_orientation?`

```elixir
@callback records_orientation?(stat :: t()) :: boolean()
```

Whether the stat's table records which way it runs, in a `flipped_aes` column, as counting
and binning do, so a geom drawing it runs that way whatever it would read on its own, and a
layer dodging its marks refuses dodging's `orientation:`, which the table's overrides.

# `refused_aesthetics`

```elixir
@callback refused_aesthetics(stat :: t()) :: [{atom(), String.t()}]
```

The aesthetics the stat refuses mapped before it runs, by the layer's own mapping or the plot's,
each with why. An after_stat reads the stat's output, so it is never refused here.

# `required_aesthetics`

```elixir
@callback required_aesthetics(stat :: t()) :: [atom() | [atom()]]
```

The aesthetics the stat cannot compute without, mapped before it runs: each a name, or a list of
names of which one is enough.

# `written_aesthetics`

```elixir
@callback written_aesthetics(stat :: t()) :: [atom()]
```

The aesthetics the stat writes itself, over any value mapped before it, so its table holds them
for the geom after it. A layer's own mapping may not map one of them before the stat unless the
stat also reads it.

# `compute_layer`

```elixir
@spec compute_layer(t(), Giggly.DataFrame.t(), axes()) :: Giggly.DataFrame.t()
```

The layer's table `frame` computed on by `stat`'s module's `c:compute_layer/3`.

# `frame_operations?`

```elixir
@spec frame_operations?(t()) :: boolean()
```

Whether `stat` computes through the frame's operations alone, by its module's `c:frame_operations?/1`.

---

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