---
title: "Graph Explorer: Node Renderers"
description: "How the Graph Explorer draws nodes: chips and cards per zoom level, layers of information - nodes, relations, labels, health - each a filter in its own colour, and renderers of your own - one node type, or a layer of your own - written with an escaping html template."
slug: "graph-nodes"
---

import { DocPage, PageHeader, SourceFiles, SourceNote, DocText, DocLink, H2 } from "../../lib/components/index";

<DocPage meta={meta}>

<PageHeader slug="graph-nodes" title="Node Renderers" component="graph-explorer">
  A node gets DOM once it is big enough to read: first a chip with its name, then a card with its actions. What a chip or a card holds is layers of information: the node itself, then a part for every other layer shown - its links, its counts, its health. Each part is a renderer's - a pure function from the node to <code>ctx.html</code> markup -, and a plugin can re-skin one node type or add a layer of its own.
</PageHeader>

<H2 id="layers">Layers</H2>

<DocText><code>data-layers</code> names the layers shown; each is a filter of its own. The base layers are shown while the attribute is absent: <code>nodes</code> (the chips and cards, the names beside small nodes) and <code>relations</code> (the lines, the signposts, a part with a node's links). <code>labels</code> adds the link labels and a part with what a node is and how big, <code>health</code> the circuit breakers and a part with a cluster's links by breaker state or an entity's freshness. With none shown the clusters' circles remain. A <code>layers</code> outlet is the switch - a Toggle Group with every layer's colour as an Indicator dot, its tint when shown, and Compact (every node a one-line chip) after a Separator; the keys <code>1</code> to <code>9</code> switch a layer too. The colours are the theme's chart colours; when those are one hue (the default theme's are five blues) the explorer turns chart-1 around the hue circle.</DocText>

```html example label="Nodes, relations, labels, health" schema="none" hint="Show or hide a layer above the graph, or focus the graph and press 1 to 4. With none shown only the circles remain. Zoom into a domain to see the systems' health bars, and into a system for the entities' freshness." previewStyle="align-items:stretch;"
<div class="grid gap-2">
  <div data-graph-explorer-for="uis" data-graph-explorer-outlet="layers"></div>
  <div
    class="graph-explorer"
    id="uis"
    data-nodes="100000"
    data-layers="nodes relations labels health"
    data-frame
    aria-label="Layers"
    style="block-size: 26rem"
  ></div>
</div>
```

<H2 id="renderers">A renderer of your own</H2>

<DocText><code>df$.shadcn.graphExplorer.register(scope, plugin)</code> adds a node renderer for every explorer (<code>'global'</code>) or one element (its plugins win). <code>match(node)</code> returns a score or <code>false</code> - the highest wins -, <code>sizes</code> give the chip's and the card's box, <code>render(node, ctx)</code> the markup for <code>ctx.tier</code>. A renderer without a <code>layer</code> draws the node itself and puts <code>ctx.parts</code> - the other layers' parts - where they belong (the explorer appends them after a renderer that leaves them out). Markup is only what <code>ctx.html</code> makes: every interpolation is escaped, and <code>on*</code> attributes, URLs that run code and active elements are stripped before it reaches the page. Anything asynchronous goes through <code>ctx.request(key, load)</code>, which renders the node again when the value arrives.</DocText>

```html example label="Datasets as cards of their own" schema="none" hint="Enter a domain, then a system: Dataset entities draw their row count and a freshness badge; the parts of the layers shown follow. The other entities keep the built-in renderer." previewStyle="align-items:stretch;"
<div
  class="graph-explorer"
  id="datasets"
  data-nodes="100000"
  data-focus="c8"
  data-frame
  aria-label="Datasets"
  style="block-size: 26rem"
></div>
<script type="module">
  document.addEventListener('DOMContentLoaded', () => {
    const ge = df$.shadcn.graphExplorer;
    ge.register(df$('#datasets').get(0), {
      kind: 'node',
      name: 'dataset',
      match: (n) => (n.kind === 'entity' && n.typeName === 'Dataset' ? 10 : false),
      sizes: { chip: [156, 26], card: [236, 74] },
      render: (n, c) => {
        const icon = c.icon('database', { cls: 'graph-explorer-type-icon', type: n.type });
        if (c.tier === 'chip')
          return c.html`<span class="graph-explorer-chip" data-kind="entity">${icon}<span class="graph-explorer-label">${n.label}</span><span class="graph-explorer-count">${n.attrs.rows}k</span>${c.parts}</span>`;
        const stale = n.attrs.freshness > 24;
        return c.html`<article class="graph-explorer-card" data-kind="entity">
          <header class="graph-explorer-card-head">${icon}<strong class="graph-explorer-label">${n.label}</strong></header>
          <p class="graph-explorer-card-flags"><span class="badge" data-variant="${stale ? 'destructive' : 'secondary'}">${stale ? 'stale' : 'fresh'} · ${n.attrs.freshness} h</span><span class="badge" data-variant="outline">${n.attrs.rows.toLocaleString('en-US')}k rows</span></p>
          ${c.parts}
        </article>`;
      },
    });
  });
</script>
```

<DocText>These explorers have no details outlet, so their cards carry no Expand button: a page needs somewhere to show. With an outlet the built-in cards offer it, and a detail plugin's <code>page(node)</code> can say a node has none.</DocText>

<H2 id="layer-plugin">A layer of your own</H2>

<DocText>A renderer with a <code>layer</code> draws that layer's part of a node: a chip part adds its width to the chip, a card part its height to the card (<code>sizes</code> may be a function of the node). <code>layerLabel</code>, <code>layerIcon</code> and <code>layerColor</code> name the layer in the switch - without a colour it takes the next chart colour. The explorer borders and tints every part in its layer's colour. Here an Owners layer gives every entity its owner and every cluster a team.</DocText>

```html example label="An Owners layer" schema="none" hint="Owners is a plugin layer, shown here: entities show their owner, clusters a team. Hide it in the switch; enter a system to see the entities." previewStyle="align-items:stretch;"
<div class="grid gap-2">
  <div data-graph-explorer-for="owners" data-graph-explorer-outlet="layers"></div>
  <div
    class="graph-explorer"
    id="owners"
    data-nodes="100000"
    data-focus="c8"
    data-layers="nodes relations owners"
    data-frame
    aria-label="Owners"
    style="block-size: 26rem"
  ></div>
</div>
<script type="module">
  document.addEventListener('DOMContentLoaded', () => {
    const OWNERS = ['mira', 'jonas', 'li', 'sam', 'ada', 'omar', 'noor', 'tomas'];
    const team = (id) => [0, 3, 5].map((k) => OWNERS[(Number(id.slice(1)) + k) % OWNERS.length]);
    df$.shadcn.graphExplorer.register(df$('#owners').get(0), {
      kind: 'node',
      name: 'owners',
      layer: 'owners',
      layerLabel: 'Owners',
      layerIcon: 'users',
      match: () => 0,
      sizes: (n) => ({ chip: [n.kind === 'entity' ? 30 : 58, 0], card: [0, 30] }),
      render: (n, c) => {
        const people = n.kind === 'entity' ? [n.attrs.owner] : team(n.id);
        const avatars = c.html`${people.map((p) => c.html`<span class="graph-explorer-avatar" data-type="${OWNERS.indexOf(p)}">${p.slice(0, 2).toUpperCase()}</span>`)}`;
        if (c.tier === 'chip') return avatars;
        return c.html`<p class="graph-explorer-card-meta">${avatars} ${n.kind === 'entity' ? 'owned by ' + people[0] : 'team ' + people.join(', ')}</p>`;
      },
    });
  });
</script>
```

<SourceFiles component="graph-explorer" section="css">
  <SourceNote for="css">The stylesheet and the script are the Graph Explorer's.</SourceNote>
</SourceFiles>

<SourceFiles component="graph-explorer" section="js" jsTitle="JS" />

</DocPage>
