# graph

A list or a table of links drawn as a network graph: partners, integrations, teams, knowledge maps.

Every item becomes a round node in its group color, bigger the more links it has (or the bigger its value), with thin links in the text color between them and the names beside the nodes. The layout is worked out once from a seed, so the picture is the same on every visit. When a third of the graph is in view the nodes come in from the first item outward. The pointer or keyboard focus on a node rings it, keeps its neighbors and their links and dims the rest, and a tooltip names it and what it links to.

Module `data`, category Maps and data. Import `@dioramajs/data/graph`. [Source](https://github.com/MasterKurosava/diorama/blob/main/packages/data/src/graph.ts)

The integrations of an invoicing app as a network around it, grouped by kind, that grows out of the middle on scroll.

```html title="index.html"
<div class="gr-frame">
  <p class="gr-title" id="gr-integrations">Works with the tools you already use</p>
  <ul class="gr-graph" aria-labelledby="gr-integrations" data-dio="graph" data-dio-mode="radial">
    <li>Ledgerly
      <ul data-dio-group="Payments">
        <li><a href="/stripe">Stripe</a> cards, wallets and payouts</li>
        <li><a href="/paypal">PayPal</a></li>
        <li><a href="/adyen">Adyen</a></li>
      </ul>
      <ul data-dio-group="Accounting">
        <li><a href="/xero">Xero</a> two-way sync of invoices</li>
        <li><a href="/quickbooks">QuickBooks</a></li>
      </ul>
      <ul data-dio-group="Chat">
        <li><a href="/slack">Slack</a> alerts for late payments</li>
        <li><a href="/teams">Teams</a></li>
      </ul>
      <ul data-dio-group="Storage">
        <li><a href="/drive">Google Drive</a></li>
        <li><a href="/dropbox">Dropbox</a></li>
      </ul>
    </li>
  </ul>
</div>
```

## Usage

Put it on: a `<ul>` or `<ol>` whose items are the nodes (nested lists link an item to the one it sits in, `data-dio-to` links any two), or a `<table>` whose rows link the names in their first two cells, sized with CSS.

`start()` registers `graph`, so the attribute alone puts it on an element.

```html
<ul class="graph" data-dio="graph" data-dio-mode="radial"><li>Acme Cloud<ul data-dio-group="Payments"><li><a href="/integrations/stripe">Stripe</a></li><li><a href="/integrations/paypal">PayPal</a></li></ul><ul data-dio-group="Chat"><li><a href="/integrations/slack">Slack</a></li><li><a href="/integrations/teams">Teams</a></li></ul></li></ul>
```

- `data-dio-to`: On a list item: ids or names of the items it links to, separated by spaces.

- `data-dio-group`: On a list item, a list or a table cell: the group that colors the node and the items inside.

- `data-dio-value`: On a list item: a number that sizes the node instead of its count of links.

In page transitions: Fades with the page.

## Params

| Name | Attribute | Type | Default | Description |
| --- | --- | --- | --- | --- |
| `mode` (rebuilds) | `data-dio-mode` | `'force' \| 'radial' \| 'tree' \| 'circle'` | `'force'` | Layout: force spreads linked nodes as a network, for ecosystems and knowledge maps; radial puts the first item in the middle and the rest in rings around it, for partners around a product; tree hangs the list top down by its nesting, for team structure and sitemaps; circle sets the nodes on a ring grouped by color with links bowed through the middle, for who works with whom. |
| `motion` (rebuilds) | `data-dio-motion` | `'grow' \| 'spread' \| 'fade'` | `'grow'` | Entrance: grow pops the nodes up and draws each link from its start; spread flies the nodes out of the first item; fade fades everything in place. |
| `colors` (rebuilds) | `data-dio-colors` | `string` | `''` | Group colors in the order groups first appear, separated by spaces; empty uses a colorblind-safe set. |
| `seed` (rebuilds) | `data-dio-seed` | `number` 1 to 1000 | `1` | Seed of the force layout: another number gives another arrangement of the same graph. |
| `radius` | `data-dio-radius` | `number` 2 to 24 px | `6` | Radius of an average node; nodes with more links or a bigger value grow to 1.6 times it. |
| `thickness` | `data-dio-thickness` | `number` 0.5 to 6 px | `1.25` | Width of a link. |
| `labels` | `data-dio-labels` | `boolean` | `true` | Draws the names beside the nodes, leaving out the ones that would overlap. |
| `arrows` | `data-dio-arrows` | `boolean` | `false` | Draws an arrowhead where a link ends, for a graph with direction. |
| `dim` | `data-dio-dim` | `number` 0 to 1 | `0.75` | How much a lit node dims the nodes it does not link to; 0 keeps them as they are. |
| `duration` | `data-dio-duration` | `number` 0 to 3 s | `0.6` | Time one node takes to come in; 0 shows the graph at once. |
| `stagger` | `data-dio-stagger` | `number` 0 to 0.5 s | `0.12` | Delay between one step away from the first item and the next; 0 brings every node in at once. |

Changing a param marked rebuilds re-runs the effect setup.

### Variants

- `motion`: `grow`, `spread`, `fade`
  - `grow` pops the nodes up and draws each link from its start.
  - `spread` flies the nodes out of the first item.
  - `fade` fades everything in place.
- `mode`: `force`, `radial`, `tree`, `circle`
  - `force` spreads linked nodes as a network, for ecosystems and knowledge maps.
  - `radial` puts the first item in the middle and the rest in rings around it, for partners around a product.
  - `tree` hangs the list top down by its nesting, for team structure and sitemaps.
  - `circle` sets the nodes on a ring grouped by color with links bowed through the middle, for who works with whom.

## Inputs and frames

Reads [`delta`](/docs/api/inputs#input-delta), [`height`](/docs/api/inputs#input-height), [`opacity`](/docs/api/inputs#opacity), [`reducedMotion`](/docs/api/inputs#input-reducedMotion), [`width`](/docs/api/inputs#input-width), [`y`](/docs/api/inputs#input-y). Draws only while something on it moves, then stops. Only while the nodes come in and for a few frames as a lit node fades in or out; none at rest. See [how a frame is decided](/docs/concepts/frames).

## Reduced motion and touch

With reduced motion it shows a still frame: motion inputs are zero and time stands still. The graph stands complete from the first frame; hover, focus and the tooltip still work.

Runs on every device. A tap on a node lights it and shows its tooltip, a second tap follows its link, a tap elsewhere clears it.

```ts
import { start } from '@dioramajs/diorama'

start({ effects: { graph: { reducedMotion: 'off' } } })
```

## Cost and quality

Loads 8.0 KB the first time a page uses it. GPU: Four draw calls: all links, all arrowheads, all nodes as one instanced call, and the labels texture. Memory: One canvas texture the size of the box at up to 2x pixel ratio, plus about 100 bytes per node and link. The layout runs once on the CPU: about 30 ms for 200 nodes.

See [the quality plugin](/docs/effects/quality).

## Recipes

### A team structure

A nested list already is a tree: each item links to the one it sits in. The `tree` mode hangs it top down, a level per step, with each name under its node. `data-dio-group` on an item colors it and everything inside it.

A studio's teams as a tree from the lead down, colored by department.

```html title="index.html"
<div class="gr-frame">
  <p class="gr-title" id="gr-team">How the studio is organized</p>
  <ul class="gr-graph" aria-labelledby="gr-team" data-dio="graph" data-dio-mode="tree" data-dio-motion="fade">
    <li>Studio lead
      <ul>
        <li data-dio-group="Design">Design<ul><li>Brand</li><li>Product</li><li>Motion</li></ul></li>
        <li data-dio-group="Build">Engineering<ul><li>Web</li><li>Apps</li></ul></li>
        <li data-dio-group="Clients">Accounts<ul><li>New business</li><li>Support</li></ul></li>
      </ul>
    </li>
  </ul>
</div>
```

### A map of topics

Any item can link to any other with `data-dio-to`, by id or by name. The `force` mode spreads linked items as a network: related topics cluster, bridges sit between clusters. `arrows` marks the direction, here from a lesson to what it builds on. Each item keeps its link, so a click on a node opens the lesson and the keyboard reaches every one.

A photography course as a network of lessons with arrows to what each builds on.

```html title="index.html"
<div class="gr-frame">
  <p class="gr-title" id="gr-topics">A course map: what each lesson builds on</p>
  <ul class="gr-graph" aria-labelledby="gr-topics" data-dio="graph" data-dio-arrows="">
    <li id="light"><a href="#light">Light</a></li>
    <li id="exposure" data-dio-to="light"><a href="#exposure">Exposure</a></li>
    <li id="aperture" data-dio-to="exposure"><a href="#aperture">Aperture</a></li>
    <li id="shutter" data-dio-to="exposure"><a href="#shutter">Shutter speed</a></li>
    <li id="iso" data-dio-to="exposure"><a href="#iso">ISO</a></li>
    <li id="depth" data-dio-to="aperture"><a href="#depth">Depth of field</a></li>
    <li id="motion" data-dio-to="shutter"><a href="#motion">Motion blur</a></li>
    <li id="color" data-dio-to="light"><a href="#color">Color temperature</a></li>
    <li id="raw" data-dio-to="color iso"><a href="#raw">Shooting raw</a></li>
    <li id="portrait" data-dio-to="depth color"><a href="#portrait">Portraits</a></li>
    <li id="night" data-dio-to="iso motion shutter"><a href="#night">Night scenes</a></li>
  </ul>
</div>
```

### Links with weights from a table

A table works too: each row links the names in its first two cells, and a number in the third makes the link thicker. The `circle` mode sets the nodes on a ring grouped by color, with links bowed through the middle, so who works with whom reads at a glance.

```html
<table
  class="flows"
  data-dio="graph"
  data-dio-mode="circle"
  data-dio-arrows
>
  <thead>
    <tr>
      <th>From</th>
      <th>To</th>
      <th>Orders</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td data-dio-group="hub">Warehouse</td>
      <td>Berlin</td>
      <td>420</td>
    </tr>
    <tr><td>Warehouse</td><td>Paris</td><td>310</td></tr>
    <tr><td>Berlin</td><td>Prague</td><td>90</td></tr>
  </tbody>
</table>
```

## Notes

The list or table is the source of truth. It stays in the page for screen readers, search engines and the keyboard, hidden only while the graph is drawn, and it shows as is without WebGL. Focus on an item’s link lights its node and shows its tooltip, so a keyboard user sees where they are. Size the element with CSS; `overflow: auto` keeps a long list inside the same box when it shows.

The layout is worked out once on the CPU from `seed` and the box’s proportions, so the picture is the same on every visit and does not drift. A resize lays it out again only when the proportions change noticeably. Nodes grow with their number of links, or with `data-dio-value` when the items give one. Names that would overlap are left out of the picture and appear when their node is lit.

It draws only while the nodes come in and for a few frames as a lit node fades in or out. Under reduced motion the graph stands complete from the first frame. A tap on a node lights it, a second tap follows its link. Style the tooltip with `::part(tooltip)`.

`mode`, `motion`, `colors` and `seed` rebuild the graph when they change; sizes, `labels`, `arrows` and the timing change live. One graph draws up to 400 nodes.

## Related

- [chart](/docs/effects/chart.md): A plain HTML table or definition list drawn as an animated chart that grows in on scroll.
- [globe](/docs/effects/globe.md): An interactive globe of land dots with markers and arcs for a list of places.
- [heatmap](/docs/effects/heatmap.md): A table drawn as a heatmap: a matrix of rows and columns, or a calendar of days like a contributions chart.
- [map](/docs/effects/map.md): A flat map of the world or a region with markers for a list of places and arcs between them.
- [assemble](/docs/effects/assemble.md): Thousands of lit particles fly out of the depth around the page and gather into a heading.
- [bend](/docs/effects/bend.md): Scroll speed bends the image like paper and splits its color channels.
- [blinds](/docs/effects/blinds.md): A heading printed across real 3D slats that turn over in a wave, lit and casting shadows, to show it.
- [clouds](/docs/effects/clouds.md): A sky of soft layered clouds for the time of day, daylight, a sunset or a moonlit night, drifting in the wind and parting on scroll.
- [Reduced motion](/docs/guides/reduced-motion.md)
- [Performance and quality](/docs/guides/performance.md)
