# `Visualize.Data`
[🔗](https://github.com/dcoai/Visualize/blob/v0.8.3/lib/visualize/data.ex#L1)

Data transformation and statistical utilities.

Provides functions for summarizing data, computing statistics,
and transforming data for visualization.

## Examples

    data = [
      %{category: "A", value: 10},
      %{category: "A", value: 20},
      %{category: "B", value: 15}
    ]

    Visualize.Data.extent(data, & &1.value)
    # => {10, 20}

    Visualize.Data.group(data, & &1.category)
    # => %{"A" => [...], "B" => [...]}

# `bin`

```elixir
@spec bin([number()], keyword()) :: [map()]
```

Creates histogram bins from continuous data.

## Options

- `:thresholds` - number of bins or list of threshold values
- `:domain` - [min, max] range to bin

## Examples

    Visualize.Data.bin([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], thresholds: 5)
    # Returns list of bins with :x0, :x1, :values keys

# `cross`

```elixir
@spec cross([any()], [any()], (any(), any() -&gt; any()) | nil) :: [any()]
```

Creates a cross product of two arrays.

## Examples

    Visualize.Data.cross([1, 2], ["a", "b"])
    # => [{1, "a"}, {1, "b"}, {2, "a"}, {2, "b"}]

# `delta`

```elixir
@spec delta([any()], (any() -&gt; number()) | nil) :: number() | nil
```

Returns the last value minus the first (spec/08 §5.1); `nil` for `[]`.

## Examples

    iex> Visualize.Data.delta([3, 8, 5])
    2

    iex> Visualize.Data.delta([%{v: 1.5}, %{v: 4.0}], & &1.v)
    2.5

    iex> Visualize.Data.delta([7])
    0

    iex> Visualize.Data.delta([])
    nil

# `deviation`

```elixir
@spec deviation([any()], (any() -&gt; number()) | nil) :: float() | nil
```

Returns the standard deviation.

# `extent`

```elixir
@spec extent([any()], (any() -&gt; number()) | nil) :: {number(), number()} | nil
```

Returns {min, max} as a tuple.

## Examples

    Visualize.Data.extent([3, 1, 4, 1, 5])
    # => {1, 5}

    Visualize.Data.extent(data, & &1.value)
    # => {min_value, max_value}

# `group`

```elixir
@spec group([any()], (any() -&gt; any())) :: map()
```

Groups data by a key function.

Returns a map where keys are the result of the key function
and values are lists of matching elements.

## Examples

    data = [%{cat: "A", val: 1}, %{cat: "A", val: 2}, %{cat: "B", val: 3}]
    Visualize.Data.group(data, & &1.cat)
    # => %{"A" => [%{cat: "A", val: 1}, %{cat: "A", val: 2}], "B" => [%{cat: "B", val: 3}]}

# `lttb`

```elixir
@spec lttb([any()], pos_integer(), (any() -&gt; {term(), term()}) | nil) :: [any()]
```

Largest-Triangle-Three-Buckets: about `n` of the elements, chosen to keep the series'
visual shape (spec/08 §5.2; Steinarsson 2013).

The accessor returns `{x, y}`; without one the elements are `{x, y}` tuples. A datum
whose `x` or `y` is not a number is a gap: the defined data are reduced run by run
between the gaps, and each run of gaps keeps its first element in place, so a line over
the result still breaks where the input broke. Input of `n` or fewer elements is
returned unchanged; without gaps the result is exactly `n` elements, the first and the
last among them. `n` must be at least 3.

## Examples

    iex> Visualize.Data.lttb([{0, 0}, {1, 1}, {2, 0}, {3, 5}, {4, 0}, {5, 1}, {6, 0}], 4)
    [{0, 0}, {2, 0}, {3, 5}, {6, 0}]

    iex> Visualize.Data.lttb([{0, 0}, {1, 3}, {2, nil}, {3, 4}, {4, 1}], 9)
    [{0, 0}, {1, 3}, {2, nil}, {3, 4}, {4, 1}]

# `m4`

```elixir
@spec m4([any()], pos_integer(), (any() -&gt; {term(), term()}) | nil) :: [any()]
```

M4: per pixel column, the first, last, least-`y` and greatest-`y` elements — the
elements a line drawn `width` pixels wide needs to draw the same pixels as all of them
(spec/08 §5.2; Jugel et al. 2014).

The columns split the extent of the defined `x` readings evenly. The accessor, the gaps
and the runs are as `lttb/3`'s: each run of gaps keeps its first element in place, and
each stretch of one run in one column keeps at most four elements, deduplicated and in
input order. `width` must be at least 1.

## Examples

    iex> Visualize.Data.m4([{0, 1}, {1, 5}, {2, 0}, {3, 2}, {4, 3}, {5, 9}, {6, 4}, {7, 6}], 2)
    [{0, 1}, {1, 5}, {2, 0}, {3, 2}, {4, 3}, {5, 9}, {7, 6}]

    iex> Visualize.Data.m4([{0, 1}, {1, 2}, {2, nil}, {3, nil}, {4, 3}], 1)
    [{0, 1}, {1, 2}, {2, nil}, {4, 3}]

# `max`

```elixir
@spec max([any()], (any() -&gt; number()) | nil) :: number() | nil
```

Returns the maximum value.

# `mean`

```elixir
@spec mean([any()], (any() -&gt; number()) | nil) :: float() | nil
```

Returns the arithmetic mean.

# `median`

```elixir
@spec median([any()], (any() -&gt; number()) | nil) :: number() | nil
```

Returns the median value.

# `min`

```elixir
@spec min([any()], (any() -&gt; number()) | nil) :: number() | nil
```

Returns the minimum value.

## Examples

    Visualize.Data.min([3, 1, 4, 1, 5])
    # => 1

    Visualize.Data.min(data, & &1.value)
    # => minimum value from data

# `quantile`

```elixir
@spec quantile([any()], float(), (any() -&gt; number()) | nil) :: number() | nil
```

Returns the p-quantile value.

`p` should be between 0 and 1.

# `range`

```elixir
@spec range(number(), number(), number()) :: [number()]
```

Generates a range of numbers.

## Examples

    Visualize.Data.range(0, 5)
    # => [0, 1, 2, 3, 4]

    Visualize.Data.range(0, 1, 0.2)
    # => [0, 0.2, 0.4, 0.6, 0.8]

# `rollup`

```elixir
@spec rollup([any()], (any() -&gt; any()), ([any()] -&gt; any())) :: map()
```

Groups data and applies a reducer to each group.

## Examples

    data = [%{cat: "A", val: 1}, %{cat: "A", val: 2}, %{cat: "B", val: 3}]
    Visualize.Data.rollup(data, & &1.cat, &length/1)
    # => %{"A" => 2, "B" => 1}

    Visualize.Data.rollup(data, & &1.cat, fn items ->
      Visualize.Data.sum(items, & &1.val)
    end)
    # => %{"A" => 3, "B" => 3}

# `slope`

```elixir
@spec slope([any()], (any() -&gt; number() | {number(), number()}) | nil) ::
  float() | nil
```

Returns the least-squares slope of the points the elements read as (spec/08 §5.1).

A `{x, y}` tuple is a point; a number `v` at index `i` is the point `{i, v}`, so a
series of numbers gives its trend per sample. `nil` for fewer than two points, or when
every `x` is the same.

## Examples

    iex> Visualize.Data.slope([1, 3, 5])
    2.0

    iex> Visualize.Data.slope([{0, 1}, {2, 2}, {4, 3}])
    0.5

    iex> Visualize.Data.slope([%{t: 10, load: 4}, %{t: 20, load: 2}], &{&1.t, &1.load})
    -0.2

    iex> Visualize.Data.slope([{1, 2}, {1, 5}])
    nil

# `sort`

```elixir
@spec sort([any()], (any() -&gt; any()), :asc | :desc) :: [any()]
```

Sorts data by an accessor function.

# `sum`

```elixir
@spec sum([any()], (any() -&gt; number()) | nil) :: number()
```

Returns the sum of values.

# `ticks`

```elixir
@spec ticks(number(), number(), integer()) :: [number()]
```

Generates evenly-spaced tick values for a domain.

Useful for creating axis ticks without a scale.

# `unique`

```elixir
@spec unique([any()], (any() -&gt; any()) | nil) :: [any()]
```

Returns unique values by accessor.

# `variance`

```elixir
@spec variance([any()], (any() -&gt; number()) | nil) :: float() | nil
```

Returns the variance of values.

---

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