# `ConceptDiagram`

Build concept diagrams as plain Elixir data, then render them to
[Mermaid](https://mermaid.js.org) or [Graphviz DOT](https://graphviz.org).

A concept diagram is a set of labelled nodes connected by labelled, directed
edges. Describe one with a small pipeline-friendly API and turn it into diagram
source you can drop straight into Markdown (Mermaid) or render with Graphviz (DOT).

    ConceptDiagram.new(direction: :left_right)
    |> ConceptDiagram.add_edge("Idea", "Draft", "becomes")
    |> ConceptDiagram.add_edge("Draft", "Diagram", "rendered as")
    |> ConceptDiagram.to_mermaid()

Nodes referenced by an edge are created automatically, so the snippet above is
enough to produce a complete `flowchart LR`.

Maintained by the team behind [Vizcept](https://vizcept.com), an AI concept
diagram generator; this library is the code-first companion for generating
diagram source programmatically.

# `direction`

```elixir
@type direction() :: :top_down | :bottom_up | :left_right | :right_left
```

Layout direction for the rendered diagram.

# `id`

```elixir
@type id() :: String.t() | atom()
```

A node id; atoms and other terms are coerced to strings.

# `t`

```elixir
@type t() :: %ConceptDiagram{
  direction: direction(),
  edges: [{String.t(), String.t(), String.t() | nil}],
  nodes: [{String.t(), String.t()}],
  title: String.t() | nil
}
```

# `add_edge`

```elixir
@spec add_edge(t(), id(), id(), String.t() | nil) :: t()
```

Add a directed edge `from -> to` with an optional relationship label. Endpoints
that are not yet present are added as nodes automatically.

# `add_node`

```elixir
@spec add_node(t(), id(), String.t() | nil) :: t()
```

Add a node, or relabel it if the id already exists. The label defaults to the
stringified id.

# `from_triples`

```elixir
@spec from_triples(
  [{id(), String.t() | nil, id()}],
  keyword()
) :: t()
```

Build a diagram from a list of `{from, relation, to}` triples. `relation` may be
`nil` for an unlabelled edge.

# `new`

```elixir
@spec new(keyword()) :: t()
```

Create an empty diagram.

Options:

  * `:direction` — one of `:top_down` (default), `:bottom_up`, `:left_right`,
    `:right_left`.
  * `:title` — optional title (rendered as a graph label in DOT output).

# `to_dot`

```elixir
@spec to_dot(t()) :: String.t()
```

Render the diagram as [Graphviz DOT](https://graphviz.org) source.

# `to_mermaid`

```elixir
@spec to_mermaid(t()) :: String.t()
```

Render the diagram as [Mermaid](https://mermaid.js.org) flowchart source.

---

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