<t-graph>

Nodes and edges, laid out by a force simulation. The graph is a JavaScript expression over the page's variables, so it changes shape when they do, and the node the reader clicks or points at goes back into variables. d3 loads only on pages that have a <t-graph>.

Each corner of a cube in 4 dimensions is a string of bits, and an edge joins two corners that differ in one bit. Its color says which. That makes corners and edges.

The corners' ids are numbers, so going from 3 to 4 dimensions keeps corners 0 to 7 where they were, and the new ones grow out of them as a second cube.

Writing the graph

The expression goes in a <script type="text/plain"> inside the element, where the browser doesn't run it and Markdown leaves it alone. Or keep it in a file and point to it with src. It's an expression like the expr of a <t-let>, with d3 in reach, and it gives back an object:

KeyDefaultDescription
nodes[]The nodes. See below.
edges[]The edges. See below.
directedfalseDraw arrowheads at each edge's target.

The expression says what the graph is. How it's drawn (its height and how the layout spreads it out) is up to the attributes.

A node is a string or a number, which is both its id and its name, or an object:

KeyDescription
idWhat edges and the variables use to find the node. Defaults to the name if no other node has it, else node- and its index.
nameThe label under the node.
sizeThe radius in pixels. Defaults to 8.
colorSee Colors. Defaults to the accent color.
dataAn object shown in a tooltip when the pointer is on the node.
x, yWhere a new node starts. The simulation moves it from there.

An edge is a [source, target] pair, or an object:

KeyDescription
source, targetThe nodes it joins. Each is an id, a name that only one node has, or an index into nodes.
nameA label at the middle of the edge.
widthThe line width in pixels. Defaults to 1.5.
colorSee Colors. Defaults to a faint text color.
dataAn object shown in a tooltip when the pointer is on the edge.
idDefaults to edge- and its index.

Nodes and edges can carry other keys too. They're kept, and they're there when a node is picked or hovered. An edge to a node that doesn't exist, or a misspelled key in the object, shows an error in place of the graph.

Colors

A color can be any CSS color, or two separated by a space for light and dark mode: "#b45b1b #ffad66". It can also be a number from 1 to 8, for that color of the theme's palette, the same one <t-formula-dag> uses. The edges in the hypercube are color: bit + 1, so they follow the theme into dark mode.

Picking and hovering

Two attributes put what the reader does into variables:

Both hold the node object from your expression, with its id as a string: { id: "5", name: "0101", size: 6 }. When the expression runs again, they hold the node's new object, so picked.name above changes from 101 to 0101 when you add a dimension. A node that's gone sets them to null.

The expression can read these variables itself, for example to color the neighbours of the picked node. Only the colors change then, and nothing moves.

Zooming

With the zoom attribute, the reader can zoom in to read a crowded graph, as in the hypercube above:

The scroll wheel on its own still scrolls the page, so a graph never traps the reader halfway down it. Scrolling over the graph shows a short hint about the key instead. You can't zoom out past the whole graph or pan away from it. On a touch screen, a zoomable graph takes one-finger drags for panning, so the page scrolls from outside the graph.

Fitting the box

The layout spreads out as far as the graph needs, and the view scales it down until all of it fits in the box, then centers it. A graph that already fits keeps its size. The view holds still while the layout moves, and eases to the new fit once the motion has died down, so a wobbling chain of nodes doesn't shake the whole graph. Lines and outlines keep their width when the graph is scaled down, but nodes and labels get smaller, so a big graph reads best with zoom or a taller height.

With the bounded attribute, the graph is drawn at full size instead, and nodes that would leave the box are held at its edge. That suits a small graph whose node and label sizes matter, but a graph that needs more room than the box gives it piles up along the edges.

When the graph changes

The expression runs again whenever a variable it read changes, and the graph keeps what it can:

Give nodes ids that mean something, like the numbers in the hypercube, so the same node keeps the same id from one run to the next.

Styling

The graph is plain SVG, with these classes:

ClassWhat it is
.tangle-graphThe box around the SVG. .is-loading until d3 has loaded.
.tangle-graph-nodeA node: a circle and a .tangle-graph-label. .is-picked on the picked node.
.tangle-graph-edgeAn edge: a .tangle-graph-line and a .tangle-graph-head.
.tangle-graph-edge-labelAn edge's name.
.tangle-graph-tooltipThe tooltip with a node's or edge's data.
.tangle-graph-reset, .tangle-graph-hintWith zoom: the Reset zoom button, and the hint a plain scroll shows.
.tangle-graph-errorThe message shown in place of the graph when the expression throws.

Each node and edge sets --node or --edge to its color, and the stylesheet draws with it. To draw every node hollow:

t-graph .tangle-graph-node circle {
  fill: var(--tangle-surface);
  stroke: var(--node, var(--tangle-accent));
}

The labels use --tangle-chart-font, like the charts.

Attributes

AttributeDefaultDescription
height400The height in pixels. The width is the element's, and follows it.
distance60How long the layout tries to make each edge, in pixels.
charge-160How hard nodes push each other apart. More negative spreads the graph out.
zoomLet the reader zoom and pan. See Zooming.
boundedKeep the nodes inside the box at full size, instead of scaling the graph to fit. See Fitting the box.
srcA URL to load the expression from, instead of the <script> inside.
nameA variable for the picked node. See Picking and hovering.
hoverA variable for the node under the pointer.
labelGraphThe accessible name of the graph.

How d3 loads

The first <t-graph> on a page loads d3 7.9.0 from jsDelivr, about 280 KB of minified JavaScript. A page with a <t-obsplot> loads it anyway, and the two share it. If the page already has window.d3, that copy is used. To load it from somewhere else, call configure({ d3Url }). See Configuration.

Ported from GraphWidget in wigglystuff.