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

Chord diagrams: a square flow matrix laid out as arcs on a circle with ribbons between
them (spec/08 §7).

    iex> alias Macaw.Draw.Layout.Chord
    iex> {:ok, chord} = Chord.new()
    iex> {:ok, %{groups: groups, chords: chords}} = Chord.generate(chord, [[0, 1], [1, 0]])
    iex> {Enum.map(groups, & &1.value), length(chords)}
    {[1, 1], 1}

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

| Option | Type | Default |
|---|---|---|
| `pad_angle` | non-negative number, radians | `0` |
| `sort_groups` | function of two `%{value: v}` maps answering a boolean, or `nil` | `nil` |
| `sort_subgroups` | function of two numbers, or `nil` | `nil` |
| `sort_chords` | function of two chords, or `nil` | `nil` |
| `ring` | number in `(0, 1]` | `0.9` |

With `total` the sum of every entry, each unit spans `k = (2π − n · pad_angle) / total`
radians; groups are laid out consecutively, each spanning its row sum, and within group
`i` entry `j` spans `matrix[i][j] · k`. One chord for every pair `i ≤ j` with any flow, in
row-major order. Angles run clockwise from 12 o'clock. The shapes, `arc_path/3` and
`ribbon_path/2`, are about the origin; `columns/3` places them about a center, the radius
setting only the size (visualize #488). This is visualize's `Visualize.Layout.Chord` with
`Visualize.Chart.Transform.Layout` `chord/3` and `ribbon_path/2`, answering IR paths.

# `chord`

```elixir
@type chord() :: %{source: chord_end(), target: chord_end()}
```

A chord between two groups, a self-loop when both ends are one group.

# `chord_end`

```elixir
@type chord_end() :: %{
  index: non_neg_integer(),
  start_angle: number(),
  end_angle: number(),
  value: number()
}
```

One end of a chord: a subgroup arc.

# `group`

```elixir
@type group() :: %{
  index: non_neg_integer(),
  start_angle: number(),
  end_angle: number(),
  value: number()
}
```

A group: its arc on the circle.

# `result`

```elixir
@type result() :: %{groups: [group()], chords: [chord()]}
```

A laid-out matrix.

# `t`

```elixir
@type t() :: %Macaw.Draw.Layout.Chord{
  pad_angle: number(),
  ring: number(),
  sort_chords: (chord(), chord() -&gt; boolean()) | nil,
  sort_groups: (map(), map() -&gt; boolean()) | nil,
  sort_subgroups: (number(), number() -&gt; boolean()) | nil
}
```

# `arc_path`

```elixir
@spec arc_path(group(), number(), number()) ::
  Macaw.Draw.IR.Path.t() | Macaw.Draw.Layout.error()
```

The annulus sector of `group` between `inner_radius` and `outer_radius`, about the
origin (§7.3): an inner arc from the start to the end angle (sweep 1), a line to the
outer end, the outer arc back (sweep 0), closed; large-arc set when the span exceeds π.

# `columns`

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

The columns of a laid-out matrix (§7.3), with options `radius:` (the outer radius, `1` by
default), `center:` (`{0, 0}` by default) and `graph:` (the `Graph` the matrix came from,
for ids and rows). The radius sets the size, never the center.

- `:groups` (the default): one row per group in index order, `"row"` (the group node's
  nodes-slot row, or `nil`), `"index"`, `"id"`, `"start_angle"`, `"end_angle"`,
  `"value"`, `"path"` (the sector between `ring · radius` and `radius`, about the center).
- `:chords`: one row per chord, `"row"` (`nil`: a chord is a pair of groups, not an input
  row), `"source"`, `"target"` (the ids), `"source_value"`, `"target_value"`, `"path"`
  (the ribbon at `ring · radius`, about the center).

Without a graph, a group's id is its index.

# `generate`

```elixir
@spec generate(t(), [[number()]]) :: {:ok, result()} | Macaw.Draw.Layout.error()
```

The groups and chords of `matrix`, `n` rows of `n` non-negative numbers (§7.2):
`{:ok, %{groups: groups, chords: chords}}`; an empty matrix answers empty lists. A ragged
row is `{[:matrix, i], {:length, n}}`, a negative or non-numeric entry
`{[:matrix, i, j], {:type, :non_negative_number}}`.

# `matrix`

```elixir
@spec matrix(Macaw.Draw.Layout.Graph.t()) :: [[number()]] | Macaw.Draw.Layout.error()
```

The square flow matrix of a graph (§7.2): `matrix[i][j]` the sum of the values of the
links from node `i` to node `j`, in node order.

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

# `ribbon_path`

```elixir
@spec ribbon_path(chord(), number()) ::
  Macaw.Draw.IR.Path.t() | Macaw.Draw.Layout.error()
```

The ribbon of `chord` at `radius`, about the origin (§7.3): the source arc, a quadratic
curve through the origin to the target arc's start, the target arc, a quadratic curve
back to the source's start, closed. A self-loop is the source arc and one quadratic
return.

---

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