# `Visualize.Chart.Fact`
[🔗](https://github.com/dcoai/Visualize/blob/v0.8.3/lib/visualize/chart/fact.ex#L1)

A fact of a feed's schema in a design (spec/14 §7.4, D-136).

`Visualize.Chart.fact/3` builds one: the attribute `key` of `field` in the schema bound
to slot `source` — a field's `label`, its `unit`, the `order` of its states. The term is
allowed wherever a value can appear, and in a text as the hole
`#{fact(:source, :field, :key)}`; `Visualize.Chart.apply/2` replaces it by the
attribute, as it replaces a variable by its value. Reading a fact is a requirement: a
schema that does not give the attribute fails application, naming the slot, the field
and the attribute.

The keys are `keys/0`: the attributes of `Visualize.Data.Schema.attributes/0` and two
derived from a field's declared values, `:names` and `:colors` (D-225). `fetch/3` says
what a key gives, and `shape/1` what shape it is: a `:scalar`, a `:list` or a `:map`.
Only a scalar is a word (`word?/1`), so only a scalar key's fact is a text's hole.

## Examples

    iex> Visualize.Chart.fact(:primary, :latency, :unit)
    %Visualize.Chart.Fact{source: "primary", field: "latency", key: :unit}

# `shape`

```elixir
@type shape() :: :scalar | :list | :map
```

The shape of a fact key's value: one word, a list or a map.

# `t`

```elixir
@type t() :: %Visualize.Chart.Fact{field: String.t(), key: atom(), source: String.t()}
```

# `fetch`

```elixir
@spec fetch(Visualize.Data.Schema.t(), Visualize.Data.Schema.name(), atom()) ::
  {:ok, term()} | :error
```

What the fact key `key` gives `field` in `schema` (spec/14 §7.4, D-225): `{:ok, value}`,
or `:error` when it gives nothing.

An attribute is the description's as written, except `:values`, which is the bare
values (`Visualize.Data.Schema.values/2`). `:names` and `:colors` are the maps of
`Visualize.Data.Schema.value_names/2` and `value_colors/2`, given when not empty.

    iex> schema = Visualize.Data.Schema.new!(%{state: %{type: :category, values: [
    ...>   %{value: "ok", name: "OK"}, %{value: "down", color: "#c62828"}, "unknown"]}})
    iex> Visualize.Chart.Fact.fetch(schema, :state, :values)
    {:ok, ["ok", "down", "unknown"]}
    iex> Visualize.Chart.Fact.fetch(schema, "state", :names)
    {:ok, %{"ok" => "OK"}}
    iex> Visualize.Chart.Fact.fetch(schema, :state, :colors)
    {:ok, %{"down" => "#c62828"}}
    iex> Visualize.Chart.Fact.fetch(schema, :state, :unit)
    :error

# `keys`

```elixir
@spec keys() :: [atom(), ...]
```

The fact keys, in order: the description attributes, then `:names` and `:colors`
(spec/14 §7.4, D-225).

    iex> Visualize.Chart.Fact.keys() |> Enum.take(-3)
    [:values, :names, :colors]

# `shape`

```elixir
@spec shape(atom()) :: shape()
```

The shape of the fact key `key`'s value, whatever schema gives it (spec/14 §7.4):
`:scalar`, `:list` or `:map`.

    iex> Visualize.Chart.Fact.shape(:unit)
    :scalar
    iex> Visualize.Chart.Fact.shape(:values)
    :list
    iex> Visualize.Chart.Fact.shape(:names)
    :map

# `word?`

```elixir
@spec word?(atom()) :: boolean()
```

Whether a fact of the key `key` is written as a word — its shape is `:scalar` — and so
may stand in a text's hole (spec/14 §7.4). A list or a map is not a word.

    iex> Visualize.Chart.Fact.word?(:label)
    true
    iex> Visualize.Chart.Fact.word?(:order)
    false

---

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