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

The layered DAG (spec/08 §11): a directed graph laid out in ranks with routed edges,
Sugiyama-style, the layout of the second cut's flowcharts, state, class and ER diagrams.

    iex> alias Macaw.Draw.Layout.{Graph, Layered}
    iex> {:ok, graph} = Graph.new(["a", "a"], ["b", "c"], nil, nil, 1)
    iex> {:ok, layered} = Layered.new(routing: "polyline")
    iex> {:ok, result} = Layered.generate(layered, graph, [{40, 20}, {40, 20}, {40, 20}], [])
    iex> Enum.map(result.nodes, &{&1.id, &1.rank})
    [{"a", 0}, {"b", 1}, {"c", 1}]

Its algorithms are Graphviz `dot`'s and dagre's (Gansner, Koutsofios, North and Vo 1993;
Brandes and Köpf 2001; Barth, Jünger and Mutzel 2002), every tie broken by input order so
a drawing is a function of its input alone (spec/08 decision 9):

- **Normalizing** (§11.3): self-loops set aside, parallel edges merged for ranking, cycles
  broken by a depth-first search in node and edge order (a reversed edge is drawn in its
  own direction and marked `reversed`), and with any edge label every minimum length
  doubled and `rank_sep` halved, the label on its edge's middle dummy.
- **Ranks** (§11.4): `:network_simplex` (default) or `:longest_path`, per connected
  component, the components side by side in the order of their first node. Network
  simplex makes at most `simplex_pivots` pivots, past which the feasible ranking is kept
  and the result says `bounded: true`.
- **Order** (§11.5): dummies on long edges, a breadth-first initial order, barycenter
  sweeps down and up with a transpose after each, crossings counted by an accumulator
  tree, the best order kept.
- **Coordinates** (§11.6): Brandes–Köpf's four alignments, balanced by default; ranks
  `rank_sep` apart by their tallest node; the drawing starts at `margin`.
- **Routing** (§11.7): `:spline` (default), `:polyline` or `:orthogonal`.

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

| Option | Type | Default |
|---|---|---|
| `direction` | one of `directions/0` | `:tb` |
| `rank_sep`, `node_sep`, `edge_sep` | non-negative numbers | `50`, `20`, `10` |
| `ranker` | one of `rankers/0` | `:network_simplex` |
| `crossing_passes` | positive integer | `24` |
| `align` | one of `aligns/0` | `:balanced` |
| `rank_align` | one of `rank_aligns/0` | `:center` |
| `routing` | one of `routings/0` | `:spline` |
| `margin` | non-negative number | `0` |
| `simplex_pivots` | positive integer or `nil` | `nil`, read as `max(1000, 10 · edges)` |

# `t`

```elixir
@type t() :: %Macaw.Draw.Layout.Layered{
  align: :balanced | :ul | :ur | :dl | :dr,
  crossing_passes: pos_integer(),
  direction: :tb | :bt | :lr | :rl,
  edge_sep: number(),
  margin: number(),
  node_sep: number(),
  rank_align: :center | :start | :end,
  rank_sep: number(),
  ranker: :network_simplex | :longest_path,
  routing: :orthogonal | :spline | :polyline,
  simplex_pivots: pos_integer() | nil
}
```

# `aligns`

```elixir
@spec aligns() :: [atom()]
```

The Brandes–Köpf alignments: `[:balanced, :ul, :ur, :dl, :dr]`.

# `columns`

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

The columns of a layered drawing (§11.8). `:nodes`: `"row"`, `"id"`, `"x"`, `"y"` (the
center), `"x0"`, `"y0"`, `"x1"`, `"y1"` (the box), `"rank"`, `"order"`, in node order.
`:edges`: `"row"`, `"source"`, `"target"`, `"path"`, `"reversed"`, `"label_x"`,
`"label_y"` (`nil` without a label), and the arrowhead's place and bearing at the target
end, `"end_x"`, `"end_y"`, `"end_angle"` (radians, 0 along +x, y down), in edge order.

# `directions`

```elixir
@spec directions() :: [atom()]
```

The rank axes: `[:tb, :bt, :lr, :rl]`.

# `generate`

```elixir
@spec generate(
  t(),
  Macaw.Draw.Layout.Graph.t(),
  [{number(), number()}],
  keyword() | map()
) ::
  {:ok, Macaw.Draw.Layout.Layered.Result.t()} | Macaw.Draw.Layout.error()
```

The layered drawing of `graph` (§11.1): `sizes` one `{width, height}` per node in node
order; `opts` the per-edge `labels:` (a `{w, h}` box or `nil` per edge) and
`min_lengths:` (positive integers, `1` each by default).

Answers `{:ok, %Layered.Result{}}` or `{:error, faults}`: a size that is no box is
`{[:sizes, i], {:type, :size}}`, a label that is no box `{[:labels, i], {:type, :size}}`,
a minimum length below 1 `{[:min_lengths, i], {:type, :positive_integer}}`, a list of
another length `{[key], {:length, n}}`. An empty graph is an empty result of size
`2 · margin`.

# `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` (§11.2): `{:ok, layered}` or `{:error, faults}`.

# `rank_aligns`

```elixir
@spec rank_aligns() :: [atom()]
```

Where a node sits across its rank's thickness: `[:center, :start, :end]`.

# `rankers`

```elixir
@spec rankers() :: [atom()]
```

The rank assignments: `[:network_simplex, :longest_path]`.

# `routings`

```elixir
@spec routings() :: [atom()]
```

The edge routings: `[:orthogonal, :spline, :polyline]`.

---

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