ConceptDiagram (ConceptDiagram v0.1.0)

Copy Markdown

Build concept diagrams as plain Elixir data, then render them to Mermaid or Graphviz DOT.

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, an AI concept diagram generator; this library is the code-first companion for generating diagram source programmatically.

Summary

Types

Layout direction for the rendered diagram.

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

t()

Functions

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

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

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

Create an empty diagram.

Render the diagram as Graphviz DOT source.

Render the diagram as Mermaid flowchart source.

Types

direction()

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

Layout direction for the rendered diagram.

id()

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

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

t()

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

Functions

add_edge(diagram, from, to, label \\ nil)

@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(diagram, id, label \\ nil)

@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(triples, opts \\ [])

@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(opts \\ [])

@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(diagram)

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

Render the diagram as Graphviz DOT source.

to_mermaid(diagram)

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

Render the diagram as Mermaid flowchart source.