# `Macaw.Draw.Layout.Sankey`
[🔗](https://github.com/dcoai/macaw_draw/blob/v0.1.0/lib/macaw/draw/layout/sankey.ex#L1)

Flow diagrams: the nodes and links of a directed weighted graph positioned so a link's
width is its value (spec/08 §8), a port of d3-sankey.

    iex> alias Macaw.Draw.Layout.{Graph, Sankey}
    iex> {:ok, graph} = Graph.new(["a", "a"], ["b", "c"], [3, 1], nil, 0)
    iex> {:ok, sankey} = Sankey.new(size: {100, 40}, node_padding: 0)
    iex> laid = Sankey.compute(sankey, graph)
    iex> Enum.map(laid.nodes, &{&1.id, &1.layer, &1.y1 - &1.y0})
    [{"a", 0, 40.0}, {"b", 1, 30.0}, {"c", 1, 10.0}]

Options (§8.1), checked by `new/1`:

| Option | Type | Default |
|---|---|---|
| `size` | `{w, h}` | `{400, 300}` |
| `node_width` | non-negative number | `24` |
| `node_padding` | non-negative number | `8` |
| `iterations` | non-negative integer | `6` |
| `node_align` | one of `alignments/0`, an atom or its spelling | `:justify` |
| `link_sort` | function of two links answering a boolean, or `nil` | `nil` |

`compute/2` gives every node its value (the greater of its outgoing and incoming
totals), its depth and height (longest paths from a source and to a sink), its column
by the alignment, and its rectangle; one scale sizes nodes and links alike, and
`iterations` relaxation passes move nodes toward straight ribbons with collisions
resolved. A cycle never settles: the walk stops after as many rounds as there are nodes,
and `cyclic?/1` says it did. This is visualize's `Visualize.Layout.Sankey` over a `Graph`,
with `Visualize.Chart.Transform.Layout` `sankey/3` and `sankey_link/1`, answering IR paths.

# `alignment`

```elixir
@type alignment() :: :left | :right | :center | :justify
```

A node alignment.

# `sankey_link`

```elixir
@type sankey_link() :: %{
  index: non_neg_integer(),
  row: non_neg_integer(),
  source: term(),
  target: term(),
  value: number(),
  source_index: non_neg_integer(),
  target_index: non_neg_integer(),
  source_node: map(),
  target_node: map(),
  y0: number(),
  y1: number(),
  width: number()
}
```

A laid-out link, its node snapshots carrying their positions.

# `sankey_node`

```elixir
@type sankey_node() :: %{
  index: non_neg_integer(),
  id: term(),
  row: non_neg_integer() | nil,
  depth: non_neg_integer(),
  height: non_neg_integer(),
  layer: non_neg_integer(),
  value: number(),
  source_links: [non_neg_integer()],
  target_links: [non_neg_integer()],
  x0: number(),
  x1: number(),
  y0: number(),
  y1: number()
}
```

A laid-out node: `source_links` and `target_links` are the positions of its links in
`links`, in the settled order.

# `t`

```elixir
@type t() :: %Macaw.Draw.Layout.Sankey{
  cyclic: boolean(),
  iterations: non_neg_integer(),
  link_sort: (map(), map() -&gt; boolean()) | nil,
  links: [sankey_link()],
  node_align: alignment(),
  node_padding: number(),
  node_width: number(),
  nodes: [sankey_node()],
  size: {number(), number()}
}
```

# `alignments`

```elixir
@spec alignments() :: [alignment()]
```

The node alignments: `[:left, :right, :center, :justify]`.

# `columns`

```elixir
@spec columns(t(), atom() | String.t()) ::
  %{required(String.t()) =&gt; [term()]} | Macaw.Draw.Layout.error()
```

The columns of a computed layout (§8.3). `:nodes`: one row per node in node order,
`"row"`, `"id"`, `"index"`, `"depth"`, `"height"`, `"layer"`, `"value"`, `"x0"`, `"x1"`,
`"y0"`, `"y1"`. `:links`: one row per link in input order, `"row"`, `"source"`,
`"target"`, `"value"`, `"width"`, `"x0"` (the source's right edge), `"y0"`, `"x1"` (the
target's left edge), `"y1"`, `"path"`.

# `compute`

```elixir
@spec compute(t(), Macaw.Draw.Layout.Graph.t()) :: t() | Macaw.Draw.Layout.error()
```

The flow diagram of `graph` (§8.2): the layout with `nodes` in node order and `links` in
input order. `link_sort`, when given, is called with two links as
`%{index, source, target, value}`, the ids of their ends.

# `cyclic?`

```elixir
@spec cyclic?(t()) :: boolean() | Macaw.Draw.Layout.error()
```

Whether the depth walk of a computed layout stopped on a cycle (§8.2).

# `link_path`

```elixir
@spec link_path(sankey_link()) :: Macaw.Draw.IR.Path.t() | Macaw.Draw.Layout.error()
```

The ribbon of a laid-out link (§8.3): a move to `{source.x1, y0}`, a cubic to
`{target.x0, y1}` with both control points at the midpoint x, a line down by `width`, a
cubic back, closed.

# `new`

```elixir
@spec new() :: {:ok, t()}
```

`new/1` with every option at its default.

# `new`

```elixir
@spec new(keyword() | map()) :: {:ok, t()} | Macaw.Draw.Layout.error()
```

The layout of `opts` (§8.1): `{:ok, sankey}` or `{:error, faults}`.

# `nodes_by_layer`

```elixir
@spec nodes_by_layer(t()) :: [[sankey_node()]] | Macaw.Draw.Layout.error()
```

The nodes of a computed layout grouped by ascending layer (§8.3).

---

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