# Graph views: Mermaid in 3D

Any text that is not a [story](language.md) is read as **Mermaid**, and drawn in space by a **model**. The text is
plain Mermaid, so the same file still renders as a normal 2D diagram wherever Mermaid does.

Mermaid must be loaded on the page first: version **12.3.0**, see [why it is pinned](architecture.md#mermaid).

## Mermaid types read

### `flowchart` / `graph`

| In Mermaid | In Tritone |
| --- | --- |
| Node `api[Orders API]` | A node, labelled with the text (the id when there is none) |
| `subgraph app[Application] … end` | A group |
| A subgraph inside a subgraph | Its nodes join the outermost group |
| Edge `a --> b`, `a -->|text| b` | An edge from `a` to `b`, with its label |
| An edge that starts or ends on a subgraph | Dropped |
| HTML in labels (`<br/>`) | Removed: 3D labels are plain text |
| Node shape, `style`, `classDef`, direction, `click` | Ignored |

### `architecture-beta`

| In Mermaid | In Tritone |
| --- | --- |
| `service api(server)[Orders API] in app` | A node in group `app` |
| `group app(server)[Application]` | A group |
| `a:R --> L:b` | An edge from `a` to `b` |
| `a:R <-- L:b` | An edge from `b` to `a` |
| `junction`, and the edges through it | Dropped |
| Icons, sides, `align` | Ignored |

### `sankey-beta` / `sankey`

| In Mermaid | In Tritone |
| --- | --- |
| A name, `Revenue` | A node: the name is its label, and its id too |
| A row, `Revenue,Operations,7000` | An edge from `Revenue` to `Operations` that carries 7000 |
| Groups | None: every node is in the colour of no group |
| The `sankey` configuration: colours, alignment, `showValues` | Ignored |

The amounts are what this type adds. Select a node, in any model, and its panel says its amount and its share of
what enters the diagram, and each of its links says its own amount and its share of the node it leaves:
`Infrastructure` is `2,000 · 20% of Revenue`, and its link from `Operations` is `2,000 · 28.6% of Operations`. The
amount of a node is the larger of what comes in and what goes out. The [`sankey`](models.md#sankey) model draws
them: a block as long as its amount, a band as wide as its own.

Any other Mermaid diagram type is refused with a message that names it.

## Notes

A Mermaid comment that starts with `@` and a node id fills the panel of that node:

```text
%% @api: Owns the order lifecycle. Validates input and writes to PostgreSQL.
%% @api: Never calls the worker directly.
```

Several lines for one node become several paragraphs. Mermaid skips `%%` lines, so the file stays valid Mermaid.

The id is everything up to the first colon. The id of a sankey node is its name, so it may hold spaces:
`%% @Set aside: Kept for a bad year.`

## Moved nodes

A model places every node. In a [canvas](embedding.md#the-canvas) a reader can take a node somewhere else: a drag
that starts on a node moves it, one that starts anywhere else turns the scene. Where the node went is one more
comment, which the live editor writes for you:

```text
%% @worker at 3.75,-1.5,0 in planes
```

- The three numbers are how far the node is from the place its model gave it, along `x`, `y` and `z`, in the
  units of the [scene](#writing-a-model). They are not a position: add a node to the graph, and the moved ones
  keep their distance from wherever the model now puts them.
- `in planes` names the model. Another model puts the node somewhere else, so each model has its own lines and
  ignores the others'.
- Where a model has a floor (`planes`, `city`, `ring`, `stages`, `lanes`, `sankey`, `matrix`) a node slides on it
  and keeps its height, and the floor reaches under it. Where it has none (`nodes`, `tree`, `dominators`) the node
  moves in the plane of the screen: turn the scene to move it in depth.
- A link follows its two ends. The model is not asked again, so a long move can leave a link passing where the
  model would not have drawn it.
- A node dragged back to its place loses its line.
- Only nodes move. A bar of `matrix` stands for the links of two groups: it stays where it is, and a drag that
  starts on it turns the scene.

## Models

A model decides where everything goes. There are ten, `planes`, `nodes`, `city`, `tree`, `ring`, `stages`,
`lanes`, `dominators`, `sankey` and `matrix`, and a diagram that names none gets `planes`. [Models](models.md)
says what each one is for, shows each one live, and lists the ways to choose one.

## Writing a model

A model is a pure function from a `Graph` to a `Scene`. Both types are in
`types.ts`, which is the source of truth.

A scene is a list of primitives in a space where `x` points right, `y` towards the viewer and `z` up, centred on
the `z` axis:

| Primitive | Fields | Drawn as |
| --- | --- | --- |
| `slab` | `at` (lowest corner), `size`, `group` | A thin plane, with the group's name lying in front |
| `box` | `at` (lowest corner), `size`, `node` | A selectable block, label on its roof |
| `ball` | `at` (centre), `r`, `node` | A selectable sphere, label below |
| `bar` | `at` (lowest corner), `size`, `group`, `edges`, `label` | A selectable column in the colour of the group, with the number of its `edges` on its roof |
| `link` | `path` (2, 3 or 4 points), `edge`, `width` if it is a band | A line, a quadratic or a cubic curve, with an arrowhead and moving packets. With a `width`, a band |

Primitives are drawn in ascending `layer`; inside a layer, from far to near. Use `layer` for what you know covers
what. The camera fields (`camera`, `yawRange`, `pitchRange`) set the opening view and how far it may turn; `fog`
fades what is far away.

A `box` or a `ball` is one node. A `bar` stands for links instead: `edges` are the places in `graph.edges` of the
ones it counts, and `label` says what they have in common, as in `Checkout to Accounts`. A reader who selects it
sees those links light up and reads them in the panel, under that label. Every node still needs a `box` or a
`ball` of its own: a link is drawn between two of them.

A link with a `width` is a **band**, for an edge that carries an amount: the `value` of a `GraphEdge`, there when
the text gives one. In space a width has a direction, so `width` is the way from one side of the band to the other:
`[0, 2, 0]` is a band 2 wide along `y`, lying flat. The `path` is its middle. A band is filled in the colour of the
group its edge leaves, it has no arrowhead, and its packets run along its middle. Everything else about it is a
link: it lights up with its nodes, and it follows them when they are moved.

A model does not have to know that nodes can be [moved](#moved-nodes): the moves are applied to the scene it
returns. Give a link's `path` its first point at the edge's `from` and its last at its `to`, and it will follow them.

Register a model from outside with `tritone.registerModel(name, model)`: see [plugins](plugins.md#models).