Network
A network is two tables, nodes and the links between them, declared with structure: "network". node() draws a labelled dot, and link() draws one connector per link row, with arrowheads when directed; a ports channel marks where links can meet a node. edit.network.connect, rewire and reverse build the topology, and create, move, editText and remove work as on any other mark.
API reference
link(options)
Import plot from elicitjs. One line per row of the links table, drawn between the two nodes it references.
link({ channels, curve, curvature, spread, arrow, arrowSize, inset, sourceInset, targetInset, loopRadius, cornerRadius, sourceSide, targetSide, sourceOffset, targetOffset, labelBackground, labelPadding, labelRadius, labelOpacity, format, handles, handleSize, handleColor, table, edits, id }) → Mark| Option | Type · default | Description |
|---|---|---|
channels | objectdefault{} | All optional. source/target name the reference columns (default: the link table’s two ref fields).x/y say how a node is positioned (default: the node table’s x and y). text draws a label on the path. curve and arrow take a column, so one mark can draw several kinds of connector. Style channels (stroke, strokeWidth, opacity) read the link row. sourceSide/targetSide with a field name the columns a connection writes its sides to. |
curve | "line" | default"line" | The connector’s shape. step and its two variants are right-angled elbows drawn between node centres; orthogonalis the flowchart elbow, docked to each node’s box edge. arc is a quadratic bow and bezier a cubic with square tangents. A link whose source and target are the same node draws a loop whatever you set here. |
curvature | number | default"auto" | How far a link bows, as a fraction of its length. "auto" reads it off the data: links sharing a pair of nodes fan apart, and a pair joined by one link stays straight. A number bows every link by that amount. |
spread | numberdefault0.16 | The gap between adjacent links of one pair under curvature: "auto". |
arrow | "auto" | default"auto" | Which ends carry a filled triangle. "auto" draws one at the target end when the links table declares directed: true, and none otherwise. true and false are accepted for "target" and "none". |
arrowSize | numberdefault6 | Arrowhead length in px. The end it caps is inset by the same amount. |
loopRadius | numberdefault22 | How far a self-loop reaches out from its node, in px. More loops on one node take other edges, then nest. |
inset | numberdefault0 | The connection buffer: how far from a node’s shape a link stops, in px. sourceInset and targetInset set each end separately. An end at a port touches the port. With no node mark, it is measured from the node’s position. |
cornerRadius | numberdefault8 | Rounds the turns of a "orthogonal" connector, in px. 0 draws square corners. |
sourceSide | null | defaultnull | Which edge the connector leaves the source node by. null (the default) leaves it unpinned: it meets the facing port, or slides along a node that has no ports. A named edge pins it at that edge’s midpoint. targetSide does the same for the other end. Both read per row like curve. As a channel with a field, the column is also what connect and rewire write. |
sourceOffset | number | default0 | Where along its edge the source end docks, as a fraction of the edge from the midpoint, -0.4 to 0.4. Positive runs clockwise around the box. targetOffset does the same for the other end. Both read per row like a side, and neither writes to the dataset. |
labelBackground | boolean | defaultoff | A plate behind channels.text, so a label sitting on its own connector stays readable. true uses the theme’s backdrop, which reads as a gap in the line; a colour string sets it outright. |
labelOpacity | numberdefault0.9 | The plate’s fill opacity — lower it to let the connector show through. |
labelPadding | numberdefault3 | Space between the label and the plate’s edge, in px. |
labelRadius | numberdefault3 | The plate’s corner radius, in px. |
sourceInset / targetInset | numberdefaultinset | Override inset for one end only. |
targetOffset | number | default0 | Where along its edge the target end docks; see sourceOffset. |
targetSide | null | defaultnull | Which edge the connector enters the target node by; see sourceSide. |
format | string | defaultString | Display formatter for channels.text: a d3-format string, or (v) => string. It formats the label; the column keeps its value. |
handles | boolean | defaulttrue | Endpoint grips, drawn where a channel carries an edit. 'hit' keeps the grip grabbable and invisible. |
handleSize | numberdefault5 | Pixel radius of each endpoint grip. |
handleColor | stringdefaulttheme | Grip fill. Defaults to the theme’s handle colour. |
table | stringdefaultthe links-role table | Which table to draw, by name — the universal option every mark takes. You rarely need it here: link resolves the table filling the links role, whatever you called it. |
node(options)
Import plot from elicitjs. A dot with a label, as one mark — a preset over composite, so it desugars into an ordinary point and text. Not network-specific: point it at any table.
node({ channels, dy, shape, size, format, table, edits, id }) → Mark[]| Option | Type · default | Description |
|---|---|---|
channels | objectdefault{} | x/y are stated once for both parts. A text channel draws the label. Label channels (fontSize, textAnchor, dx) go to the text; every other channel (fill, stroke, size, symbol) goes to the dot. |
dy | numberdefault-16 | Label offset in px, positive downwards. |
shape | "circle" | default"circle" | Passed to the dot. |
format | string | defaultString | Display formatter for the label: a d3-format string, or (v) => string. Display-only. |
Add the edits you want. Put move() on x, editText() on text, and edit.network.connect() on a ports channel. An edit on x/y grabs the dot.
channels.ports
A channel on the mark that draws the nodes — point, ellipse, rect, text, sticker, node, face or a composite. It puts a dot on the edges a link may use, on the shape the mark draws. Put edit.network.connect() on it to drag new links from the dots.
ports: { value | fn | field, edit }| Option | Type · default | Description |
|---|---|---|
value | true | | Which edges get a dot. true is all four, either way. A side or a list of sides is those edges, either way. { out, in } splits them on a directed network: links leave by out and arrive by in. false is none. Use fn or field to vary it per node. |
edit | Editdefaultnone | edit.network.connect() starts a link from a dot. With no edit, the dots appear while you drag a link’s end, as places to drop it. |
Dots show where a link can go. Hover a node to see the edges a link can leave by. During a drag, the dots that accept the link light up.
edit.network.connect(options)
Drag from a dot to another node to create a link. Put it on the node mark’s ports channel, or on the node mark itself. A dashed band follows the pointer. The row gets the columns the link mark’s channels name. A second link between one pair needs multi: true on the links table, and a link from a node to itself needs selfLoops: true.
connect({ defaults, threshold, when, stage }) → Edit| Option | Type · default | Description |
|---|---|---|
defaults | objectdefault{} | Extra fields to seed the new link row with, over the schema’s defaults. |
threshold | numberdefault28 | How near a node’s centre a drop connects, in px, when the nodes draw no shape. With a shape, a drop anywhere on the node connects, and a dot pins its edge. |
when | (ctx) => defaultnull | Splits drags on a node mark that also carries move: pair when.shift with when.noShift. Leave it out on a ports channel. |
edit.network.rewire(options)
Drag a link’s end to reconnect it. Put it in a link mark’s edits. Hover a connector to see a handle at each end. Drop the end on a dot to re-point it and pin that edge, on a node’s body to re-point it, or on empty space to remove the link. It follows the links table’s multi and selfLoops like connect.
rewire({ channels, threshold, when, stage }) → Edit| Option | Type · default | Description |
|---|---|---|
channels | string[]default["source", | Which ends get a handle. |
threshold | numberdefault28 | How near the release must land to a node, in px. |
edit.network.reverse(options)
Swap a link’s two ends. Put it on the link mark; it fires on a click anywhere along the line. Where the link records its sides in columns, they swap with the ends, so each node keeps its port.
reverse({ when, stage }) → Edit| Option | Type · default | Description |
|---|---|---|
when | (ctx) => defaultnull | Needed when the link mark carries another click edit, such as remove. On a links table declared directed: false it logs a warning and leaves the row as it is. |
Declaring the structure
structure: "network" gives the dataset two tables in the roles nodes and links. The default, "table", is a single table declared as a bare field map.
schema: {structure: "network",tables: {nodes: {id: { type: "categorical", key: true, open: true },label: { type: "categorical", open: true },x: { type: "quantitative", domain: [0, 100] },y: { type: "quantitative", domain: [0, 100] },},links: {source: { type: "ref" },target: { type: "ref" },weight: { type: "quantitative", domain: [0, 1] },},},}
key: true marks the column that identifies a node. type: "ref" marks a column that names one; its domain is the key column's values. Pair key with open: true so a gesture that mints a node can give it a new identity.
data is keyed by table name. A table you leave out starts empty.
data: {nodes: [{ id: "a", label: "Ana", x: 20, y: 70 }],links: [],}
getData(), onChange and setData use the same shape.
Nodes, no links
links: [] — the topology is drawn by gesture.
const chart = Elicit({width: 460, height: 280,margins: { top: 24, right: 24, bottom: 24, left: 24 },axes: false,schema: {structure: "network",tables: {nodes: {id: { type: "categorical", key: true, open: true },label: { type: "categorical", open: true },x: { type: "quantitative", domain: [0, 100] },y: { type: "quantitative", domain: [0, 100] },},links: {source: { type: "ref" },target: { type: "ref" },},},},data: {nodes: [{ id: "n1", label: "Start here", x: 24, y: 72 },],links: [],},marks: [link({ inset: 3, stroke: "#94A5B3", strokeWidth: 1.5 }),node({size: 10,fill: "#50AEB7",edits: [edit.network.connect({ when: when.shift }),create({ defaults: { label: "New node" } }),remove({ when: when.alt }),],channels: {x: { field: "x", edit: move({ channels: ["x", "y"], when: when.noShift }) },y: { field: "y" },text: { field: "label", edit: editText() },},}),],});mount(chart.dom());
Kinds of network
Three flags on the links table say what its pairs mean. Declare them on the long form:
tables: {nodes: { fields: { … } },links: {directed: true,multi: true,selfLoops: true,fields: { source: { type: "ref" }, target: { type: "ref" } },},}
directed and multi together give the four usual kinds of network:
| kind | directed | multi | one pair may have |
|---|---|---|---|
| graph | false | — | one link |
| digraph | true | — | one link each way |
| multigraph | false | true | any number of links |
| multidigraph | true | true | any number, either way |
selfLoops: true adds links from a node to itself to any of the four. See Parallel links and loops.
directed: truedraws an arrowhead at the target and bows reciprocal links apart, soa → bandb → astay readable. It also enablesedit.network.reverse.directed: falsetreats a pair as unordered:b → ais the same link asa → b.- With
directedleft out, links draw without arrowheads, anda → bandb → aare two different links.
Directed links
directed: true — arrowheads, and a reciprocal pair that bows apart.
const chart = Elicit({width: 460, height: 280,margins: { top: 24, right: 24, bottom: 24, left: 24 },axes: false,schema: {structure: "network",tables: {nodes: {id: { type: "categorical", key: true, open: true },name: { type: "categorical", open: true },x: { type: "quantitative", domain: [0, 100] },y: { type: "quantitative", domain: [0, 100] },},links: {role: "links",directed: true,fields: {source: { type: "ref" },target: { type: "ref" },},},},},data: {nodes: [{ id: "a", name: "Ana", x: 20, y: 68 },],links: [{ source: "a", target: "b" },],},marks: [link({inset: 3,stroke: "#94A5B3",strokeWidth: 1.5,edits: [edit.network.reverse({ when: when.alt })],}),node({size: 10,fill: "#50AEB7",edits: [edit.network.connect({ when: when.shift })],channels: {x: { field: "x", edit: move({ channels: ["x", "y"], when: when.noShift }) },y: { field: "y" },text: { field: "name" },},}),],});mount(chart.dom());
Parallel links and loops
multi: true lets connect and rewire add a link between two nodes that already share one. Links between the same two nodes fan apart (curvature: "auto").
selfLoops: true lets them link a node to itself. Drag from a port to another port of the same node, or move off the port and release on the node's body.
A link from a node to itself draws as a loop. It leaves and returns by one edge, or goes round the node from an out port to an in port. More loops on the same node take the free edges first, then nest outward. loopRadius sets how far a loop reaches (default 22).
Rows holding a parallel pair or a loop always draw. With multi: false or selfLoops: false, they also log a warning.
A state machine
directed, multi and selfLoops: two events from Open to Closed, and events that leave a state where it was.
const chart = Elicit({width: 560, height: 240,margins: { top: 24, right: 24, bottom: 24, left: 24 },axes: false,schema: {structure: "network",tables: {states: {role: "nodes",fields: {id: { type: "categorical", key: true, open: true },label: { type: "categorical", open: true },x: { type: "quantitative", domain: [0, 100] },y: { type: "quantitative", domain: [0, 100] },},},transitions: {role: "links",// A multidigraph with loops: two events may join the same pair of// states, and an event may leave a state where it was.directed: true,multi: true,selfLoops: true,fields: {source: { type: "ref" },target: { type: "ref" },event: { type: "categorical", open: true },},},},},data: {states: [{ id: "locked", label: "Locked", x: 12, y: 62 },],transitions: [{ source: "closed", target: "open", event: "open" },],},marks: [link({stroke: "#46596B",strokeWidth: 1.5,labelBackground: true,channels: { text: { field: "event" } },edits: [edit.network.rewire(), editText()],}),sticker({fill: "#ECF3F9",stroke: "#1D4467",cornerRadius: 14,maxWidth: 90,channels: {x: { field: "x", edit: move({ channels: ["x", "y"], mode: "relative" }) },y: { field: "y" },text: { field: "label" },ports: { value: true, edit: edit.network.connect({ defaults: { event: "event" } }) },},}),],});mount(chart.dom());
Naming the tables
Name the tables for your data, and give each a role:
tables: {claims: { role: "nodes", fields: { … } },supports: { role: "links", fields: { … } },}
data and getData() are then keyed claims and supports. Marks and edits find their table by role, so link() draws supports by default. The argument map below uses these names.
Drawing a network
node() draws a dot with a label above it. It takes x and y once for both, and draws the label from a text channel. It is a preset over composite and works on any table. Any other mark can draw the nodes too; see What can be a node.
link() draws the links table. Each end sits at the node its ref column names.
Argument map
Nodes carry a claim; links point from a reason to what it supports.
const chart = Elicit({width: 460, height: 300,margins: { top: 24, right: 24, bottom: 24, left: 24 },axes: false,schema: {structure: "network",tables: {claims: {role: "nodes",fields: {id: { type: "categorical", key: true, open: true },text: { type: "categorical", open: true },x: { type: "quantitative", domain: [0, 100] },y: { type: "quantitative", domain: [0, 100] },},},supports: {role: "links",fields: {source: { type: "ref" },target: { type: "ref" },},},},},data: {claims: [{ id: "c1", text: "Ban cars downtown", x: 50, y: 78 },],supports: [{ source: "c2", target: "c1" },{ source: "c3", target: "c1" },],},marks: [link({ arrow: true, inset: 3, stroke: "#94A5B3" }),node({size: 11,fill: "#50AEB7",edits: [edit.network.connect({ when: when.shift }),create({ defaults: { text: "New claim" } }),remove({ when: when.alt }),],channels: {x: { field: "x", edit: move({ channels: ["x", "y"], when: when.noShift }) },y: { field: "y" },text: { field: "text", edit: editText() },},}),],});mount(chart.dom());
Connector styles
curve sets a link's shape:
curve | shape |
|---|---|
"line" | straight segment (the default) |
"step" | right-angled elbow, turning midway |
"stepBefore" / "stepAfter" | elbow turning at the source / at the target |
"orthogonal" | elbow that leaves each node square out of one of its edges |
"arc" | quadratic bow to one side |
"bezier" | cubic with square tangents |
The "step" kinds turn on the straight line between two node positions. "orthogonal" leaves and arrives perpendicular to an edge of the node's shape; see Where a link meets a node. cornerRadius rounds its turns (default 8; 0 is square).
Give curve a column to mix shapes in one mark:
link({ channels: { curve: { field: "kind" } } })
Any column works, or { fn: d => … } to map one. arrow reads a column the same way, so one field can drive both shape and arrowheads.
curvature sets how far a link bows. The default, "auto", fans out links that share a pair of nodes and keeps a lone link straight. On a directed network every link bows to the same side of its direction, so a → b and b → a separate. Pass a number to bow every link equally, and spread to change the gap "auto" leaves between siblings.
Under "auto", a lone "arc" draws straight. A "line" with curvature draws as an arc, so links sharing a pair fan apart on a plain link().
A link whose source and target are the same node draws a loop; see Parallel links and loops.
A click edit such as reverse or remove responds anywhere along the line.
Connector shapes
One link() mark, a kind column driving curve, at a fixed curvature: 0.1.
const chart = Elicit({width: 460, height: 330,margins: { top: 18, right: 34, bottom: 18, left: 34 },axes: false,schema: {structure: "network",tables: {nodes: {id: { type: "categorical", key: true, open: true },x: { type: "quantitative", domain: [0, 100] },y: { type: "quantitative", domain: [0, 100] },},links: {source: { type: "ref" },target: { type: "ref" },kind: {type: "categorical",domain: ["line", "step", "stepBefore", "stepAfter", "orthogonal", "arc", "bezier"],},},},},data: {nodes: [{ id: "l1", x: 6, y: 92 }, { id: "r1", x: 94, y: 82 },],links: [{ source: "l1", target: "r1", kind: "step" },],},marks: [link({inset: 4,stroke: "#50AEB7",strokeWidth: 2,// A fixed bow. These links share no pair of nodes, so "auto" would leave// every one of them flat — including the arc, whose only parameter this is.curvature: 0.1,fontSize: 11,labelBackground: true,channels: {curve: { field: "kind" },text: { field: "kind" },},edits: [cycle({ channels: ["curve"] })],}),point({ size: 4, fill: "#94A5B3", channels: { x: { field: "x" }, y: { field: "y" } } }),],});mount(chart.dom());
kind domain. The label is the column, so it changes with it — and stepping one to line draws an arc, since this mark sets a curvature.Where a link meets a node
A link meets the shape the node mark draws: a dot's circle, a rect's edges, a sticker's paper. With no node mark, it ends at each node's position.
How an end meets the shape depends on whether the node has ports:
- With ports, an end connects to a port. A pinned end uses the port on its side. An unpinned end uses the usable port facing the other node, and switches ports when the nodes pass each other.
- Without ports, an unpinned end slides. It sits where the line between the two centres leaves the shape, and moves smoothly as the nodes move. A pinned end sits at the middle of its edge.
inset sets how far from a node's shape a link stops, in px (default 0). sourceInset and targetInset set each end. An end at a port touches the port. An arrowhead fits in the gap.
An "orthogonal" connector leaves each end square to its edge and joins the two with the fewest turns. Two sliding ends nearly in line join with one straight leg; further apart, they join with a Z. It routes around a node in the way. Overlapping nodes get the plain route. "line" and "bezier" connectors also meet the shape, and a bezier leaves a pinned edge square-on.
A node turned by angle turns its edges and ports with it, and a side names the node's own edge. An "orthogonal" connector runs on the screen's axes, so it meets a turned node on the upright box around it.
sourceOffset and targetOffset move a pinned end along its edge, as a fraction from -0.4 to 0.4. Positive runs clockwise: right along the top, down the right, left along the bottom, up the left.
Routing
Notes without ports: one connector straightened, one detouring around a node, one taking the perpendicular edges.
const chart = Elicit({width: 520, height: 360,margins: { top: 20, right: 20, bottom: 20, left: 20 },axes: false,schema: {structure: "network",tables: {steps: {role: "nodes",fields: {id: { type: "categorical", key: true, open: true },label: { type: "categorical", open: true },x: { type: "quantitative", domain: [0, 480] },y: { type: "quantitative", domain: [0, 320] },w: { type: "quantitative", domain: [40, 200] },h: { type: "quantitative", domain: [24, 200] },},},flows: {role: "links",directed: true,fields: { source: { type: "ref" }, target: { type: "ref" } },},},},data: {steps: [// Collect sits 8px out of line with Score: close enough to join straight.{ id: "collect", label: "Collect", x: 60, y: 220, w: 92, h: 40 },],flows: [{ source: "collect", target: "score" },],},marks: [link({curve: "orthogonal",inset: 4,stroke: "#50AEB7",strokeWidth: 2,}),sticker({fill: "#EDF7F8",stroke: "#37919B",cornerRadius: 6,channels: {x: { field: "x", edit: move({ channels: ["x", "y"], mode: "relative" }) },y: { field: "y" },width: { field: "w", scale: null },height: { field: "h", scale: null },text: { field: "label" },},}),],});mount(chart.dom());
Ports
A port is a dot on a node's edge where a link starts or ends. Declare ports with a ports channel on the mark that draws the nodes (any mark in What can be a node), and put edit.network.connect() on it:
sticker({channels: {x: { field: "x", edit: move({ channels: ["x", "y"], mode: "relative" }) },y: { field: "y" },text: { field: "label" },ports: {value: { out: ["right"], in: ["left"] },edit: edit.network.connect(),},},})
The value sets which edges a link may use. Use fn or field instead of value to vary it per node.
value | ports |
|---|---|
true | all four edges, either way |
"right" or ["left", "right"] | those edges, either way |
{ out: ["right"], in: ["left"] } | links leave by the right and arrive by the left (directed networks) |
false | none |
Hover a node to see the ports a link can leave by. Start a drag and the ports that accept it light up. Both follow the out and in edges and the dataset's constraints.
Drag from a port and a dashed connector follows the pointer. Release on a port to connect there, or on a node's body to connect at its facing port. Release on empty space to cancel. Drag the body to move the node.
ports: true lets any edge connect to any edge, for a diagram you lay out freely.
Connecting from ports
Stickers whose links leave by the right and arrive by the left.
const chart = Elicit({width: 520, height: 320,margins: { top: 24, right: 24, bottom: 24, left: 24 },axes: false,schema: {structure: "network",tables: {notes: {role: "nodes",fields: {id: { type: "categorical", key: true, open: true },label: { type: "categorical", open: true },x: { type: "quantitative", domain: [0, 100] },y: { type: "quantitative", domain: [0, 100] },},},flows: {role: "links",directed: true,fields: {source: { type: "ref" },target: { type: "ref" },// The edge each end meets, or null to slide along the box.fromSide: { type: "categorical", domain: ["top", "right", "bottom", "left"] },toSide: { type: "categorical", domain: ["top", "right", "bottom", "left"] },},},},},data: {notes: [{ id: "idea", label: "Idea", x: 14, y: 72 },],flows: [{ source: "idea", target: "draft", fromSide: "right", toSide: "left" },{ source: "draft", target: "review", fromSide: "right", toSide: "left" },],},marks: [link({curve: "orthogonal",stroke: "#50AEB7",strokeWidth: 2,// The columns a connection writes.channels: {sourceSide: { field: "fromSide" },targetSide: { field: "toSide" },},edits: [edit.network.rewire(), remove({ when: when.alt })],}),sticker({fill: "#EDF7F8",stroke: "#37919B",cornerRadius: 6,maxWidth: 110,channels: {x: { field: "x", edit: move({ channels: ["x", "y"], mode: "relative" }) },y: { field: "y" },text: { field: "label" },// Links leave by the right edge and enter by the left.ports: {value: { out: ["right"], in: ["left"] },edit: edit.network.connect(),},},}),],});mount(chart.dom());
getData() gains a row with both sides.Any side to any side
An undirected concept map. Two ties are pinned to edges; two meet whichever port faces the other note.
const chart = Elicit({width: 560, height: 340,margins: { top: 24, right: 24, bottom: 24, left: 24 },axes: false,schema: {structure: "network",tables: {ideas: {role: "nodes",fields: {id: { type: "categorical", key: true, open: true },label: { type: "categorical", open: true },x: { type: "quantitative", domain: [0, 100] },y: { type: "quantitative", domain: [0, 100] },},},ties: {role: "links",directed: false,fields: {source: { type: "ref" },target: { type: "ref" },fromSide: { type: "categorical", domain: ["top", "right", "bottom", "left"] },toSide: { type: "categorical", domain: ["top", "right", "bottom", "left"] },},},},},data: {ideas: [{ id: "trust", label: "Trust in forecasts", x: 50, y: 50 },],ties: [{ source: "trust", target: "past", fromSide: "top", toSide: "right" },],},marks: [link({curve: "bezier",stroke: "#8A78C0",strokeWidth: 1.5,channels: {sourceSide: { field: "fromSide" },targetSide: { field: "toSide" },},edits: [edit.network.rewire(), remove({ when: when.alt })],}),sticker({fill: "#EDF7F8",stroke: "#6B5AA2",cornerRadius: 8,maxWidth: 120,edits: [editText(), create({ defaults: { label: "New idea" } })],channels: {x: { field: "x", edit: move({ channels: ["x", "y"], mode: "relative" }) },y: { field: "y" },text: { field: "label" },// Every edge, either way.ports: { value: true, edit: edit.network.connect() },},}),],});mount(chart.dom());
Recording sides
To record which edge each end uses, name the columns in sourceSide and targetSide channels on the link mark. Declare them in the links table as categorical over "top", "right", "bottom" and "left":
link({channels: {sourceSide: { field: "fromSide" },targetSide: { field: "toSide" },},})
The gestures write those columns, and write source and target to the links table's ref columns:
| gesture | the links table |
|---|---|
| port to port | a new row, with both sides |
| port to a node’s body | a new row; the target side is null |
| a link’s end to another node | that row re-pointed, with its new side |
| a link’s end to empty space | that row removed |
A null side is unpinned. It meets the facing port, or slides along a node that has no ports. With no side channel or side option, every end is unpinned.
edit.network.rewire() in the link mark's edits puts a handle on each end while you hover the connector. Drag one to another node to reconnect it, or into empty space to disconnect it. rewire({ channels: ["target"] }) handles one end only.
Constraints decide which ports show. Here each step shows an out port until it has one outgoing link.
Reconnect and disconnect
node() dots with ports on the left and right, and rewire on the link.
const chart = Elicit({width: 460, height: 280,margins: { top: 28, right: 24, bottom: 24, left: 24 },axes: false,schema: {structure: "network",tables: {nodes: {id: { type: "categorical", key: true, open: true },name: { type: "categorical", open: true },x: { type: "quantitative", domain: [0, 100] },y: { type: "quantitative", domain: [0, 100] },},links: {source: { type: "ref" },target: { type: "ref" },sourceSide: { type: "categorical", domain: ["left", "right"] },targetSide: { type: "categorical", domain: ["left", "right"] },},},},data: {nodes: [{ id: "a", name: "Ana", x: 16, y: 70 },],links: [{ source: "a", target: "c", sourceSide: "right", targetSide: "left" },{ source: "c", target: "b", sourceSide: "right", targetSide: "left" },],},marks: [link({curve: "bezier",stroke: "#94A5B3",strokeWidth: 1.5,channels: {sourceSide: { field: "sourceSide" },targetSide: { field: "targetSide" },},// Hover a connector for a handle at each end.edits: [edit.network.rewire()],}),node({size: 10,fill: "#50AEB7",channels: {x: { field: "x", edit: move({ channels: ["x", "y"] }) },y: { field: "y" },text: { field: "name" },ports: { value: ["left", "right"], edit: edit.network.connect() },},}),],});mount(chart.dom());
Only usable ports
An end step takes links in but sends none out, and a constraint allows each step one outgoing link.
const chart = Elicit({width: 520, height: 300,margins: { top: 24, right: 24, bottom: 24, left: 24 },axes: false,schema: {structure: "network",tables: {steps: {role: "nodes",fields: {id: { type: "categorical", key: true, open: true },label: { type: "categorical", open: true },kind: { type: "categorical", domain: ["step", "end"] },x: { type: "quantitative", domain: [0, 100] },y: { type: "quantitative", domain: [0, 100] },},},next: {role: "links",directed: true,fields: {source: { type: "ref" },target: { type: "ref" },fromSide: { type: "categorical", domain: ["top", "right", "bottom", "left"] },toSide: { type: "categorical", domain: ["top", "right", "bottom", "left"] },},},},},data: {steps: [{ id: "ask", label: "Ask", kind: "step", x: 12, y: 50 },],next: [{ source: "ask", target: "check", fromSide: "right", toSide: "left" }],},// Each step leads to one next step.constraints: [constraints.custom(({ data }) => {const sources = data.map((d) => d.source);return new Set(sources).size === sources.length;}, { table: "links" }),],marks: [link({curve: "orthogonal",stroke: "#6F8294",strokeWidth: 1.5,channels: {sourceSide: { field: "fromSide" },targetSide: { field: "toSide" },},edits: [edit.network.rewire()],}),sticker({cornerRadius: 6,maxWidth: 100,channels: {x: { field: "x", edit: move({ channels: ["x", "y"], mode: "relative" }) },y: { field: "y" },text: { field: "label" },fill: { field: "kind", scale: { range: ["#F9F4EC", "#F0F7EE"] } },// An end step takes links in but sends none out.ports: {fn: (d) => (d.kind === "end" ? { in: ["left"] } : { out: ["right"], in: ["left"] }),edit: edit.network.connect(),},},}),],});mount(chart.dom());
Pinned sides
As options, sourceSide and targetSide pin every link's ends to "top", "right", "bottom" or "left", so a diagram reads the same way however the boxes are arranged. Both default to null. A pinned end leaves its edge square-on, and routes around its own box if the other node is behind it.
The options set how links draw and write nothing to the data. Give one { fn: d => … } to derive a side from a column you already have. To record sides in the data, use channels; see Recording sides.
Pinned sides
curve: orthogonal with sourceSide and targetSide fixed, so flow reads left to right.
const chart = Elicit({width: 460, height: 320,margins: { top: 20, right: 20, bottom: 20, left: 20 },axes: false,schema: {structure: "network",tables: {stages: {role: "nodes",fields: {id: { type: "categorical", key: true, open: true },label: { type: "categorical", open: true },x: { type: "quantitative", domain: [0, 100] },y: { type: "quantitative", domain: [0, 100] },w: { type: "quantitative", domain: [40, 200] },h: { type: "quantitative", domain: [24, 120] },},},flows: {role: "links",directed: true,fields: { source: { type: "ref" }, target: { type: "ref" } },},},},data: {stages: [{ id: "a", label: "Collect", x: 22, y: 76, w: 92, h: 40 },],flows: [{ source: "a", target: "b" },{ source: "b", target: "c" },],},marks: [link({curve: "orthogonal",cornerRadius: 10,inset: 4,stroke: "#50AEB7",strokeWidth: 2,// Every flow leaves the right of its stage and enters the left of the next,// wherever the two boxes end up. Drop the two sides to let position choose.sourceSide: "right",targetSide: "left",}),sticker({fill: "#EDF7F8",stroke: "#37919B",cornerRadius: 6,channels: {x: { field: "x", edit: move({ channels: ["x", "y"], mode: "relative" }) },y: { field: "y" },width: { field: "w", scale: null },height: { field: "h", scale: null },text: { field: "label" },},}),],});mount(chart.dom());
getData() changes only x and y.What can be a node
Any mark that draws a shape at an x and a y can draw the nodes: point, rect, text, a composite, and the rest. Point it at the nodes table; channels, scales and edits work as usual. See Which table a mark draws.
Links meet the shape the mark draws, and a ports channel puts ports on that shape's edges:
| mark | a link meets |
|---|---|
point, node | the dot's circle, or a square symbol's edges |
ellipse | the ellipse, at rx and ry |
rect, sticker | the box's edges |
text | the box around the measured text, so an emoji or a word works |
face, any composite placing parts in its box | the box's circle |
any other composite | its first part that draws a shape |
A glyph puts ports on its main shape: the box's circle, or its first shape part. When a glyph draws a shape and a label, links meet the shape, so a node()'s links meet its dot.
Emoji as nodes
A plain text() mark draws the nodes, with ports on every side of each emoji.
const chart = Elicit({width: 540, height: 260,margins: { top: 24, right: 24, bottom: 24, left: 24 },axes: false,schema: {structure: "network",tables: {feelings: {role: "nodes",fields: {id: { type: "categorical", key: true, open: true },emoji: { type: "categorical", open: true },x: { type: "quantitative", domain: [0, 100] },y: { type: "quantitative", domain: [0, 100] },},},leadsTo: {role: "links",directed: true,fields: { source: { type: "ref" }, target: { type: "ref" } },},},},data: {feelings: [{ id: "tired", emoji: "😴", x: 12, y: 50 },],leadsTo: [{ source: "tired", target: "stressed" },],},marks: [link({ curve: "arc", curvature: 0.15, stroke: "#94a3b8", strokeWidth: 1.5, edits: [edit.network.rewire()] }),// A plain text mark is the node: its glyph's box is where links meet it.text({fontSize: 36,channels: {x: { field: "x", edit: move({ channels: ["x", "y"], mode: "relative" }) },y: { field: "y" },text: { field: "emoji" },ports: { value: true, edit: edit.network.connect() },},}),],});mount(chart.dom());
Faces as nodes
To draw the nodes with face, give it x and y columns on the nodes table and put the ports channel on it. Here a link leaves by the right or the bottom and arrives by the left or the top. The face keeps its own edits: move on the head, and slide on the mouth.
Whose mood rubs off on whom
face() over a network's nodes table, with ports out on the right and bottom and in on the left and top.
const chart = Elicit({width: 540, height: 320,margins: { top: 24, right: 24, bottom: 24, left: 24 },axes: false,schema: {structure: "network",tables: {people: {role: "nodes",fields: {id: { type: "categorical", key: true, open: true },x: { type: "quantitative", domain: [0, 100] },y: { type: "quantitative", domain: [0, 100] },valence: { type: "quantitative", domain: [-1, 1] },arousal: { type: "quantitative", domain: [-1, 1] },},},sways: {role: "links",// Whose mood rubs off on whom.directed: true,fields: { source: { type: "ref" }, target: { type: "ref" } },},},},data: {people: [{ id: "ana", x: 16, y: 72, valence: 0.7, arousal: 0.3 },],sways: [{ source: "ana", target: "ben" },{ source: "ben", target: "cy" },],},marks: [link({curve: "bezier",stroke: "#94a3b8",strokeWidth: 1.5,edits: [edit.network.rewire()],}),face({size: 34,channels: {x: { field: "x", edit: move({ channels: ["x", "y"], mode: "relative" }) },y: { field: "y" },mouthCurve: { field: "valence", edit: slide({ axis: "y", increase: "up" }) },eyeScale: { field: "arousal" },// A link leaves a face on the right or below, and arrives on the left or above.ports: {value: { out: ["right", "bottom"], in: ["left", "top"] },edit: edit.network.connect(),},},}),],});mount(chart.dom());
Building the topology
Three edits are specific to networks. The rest is ordinary editing.
edit.network.connect() creates a link. Put it on a ports channel, or in the node mark's edits to drag from a node's body. On the body, separate it from move with when: connect({ when: when.shift }) beside move({ when: when.noShift }). With selfLoops: true, release on the starting node to draw a loop. When the nodes draw no shape, a drop within threshold px of a node's centre connects.
edit.network.rewire() moves a link's ends; see Recording sides.
edit.network.reverse() on the link mark swaps the two ends on a click. Columns the link records its sides in swap with the ends, so each node keeps the port the link meets it at. Every other column stays. Give it a when if the mark carries another click edit. On directed: false it leaves the row unchanged and logs a warning.
create() mints a node where you click. The node table's key gives it an identity, so you can link it straight away.
remove() deletes a node and every link to it.
ui.network() puts these edits behind icons and a menu: a + beside a port that adds a linked node, link and detach icons beside it, and a right-click menu to add a node of a chosen category.
Weighted links
A link's own columns drive its width and colour, and rewire moves its source end.
const chart = Elicit({width: 440, height: 280,margins: { top: 24, right: 24, bottom: 24, left: 24 },axes: false,schema: {structure: "network",tables: {nodes: {id: { type: "categorical", key: true, open: true },name: { type: "categorical", open: true },x: { type: "quantitative", domain: [0, 100] },y: { type: "quantitative", domain: [0, 100] },},links: {source: { type: "ref" },target: { type: "ref" },strength: { type: "quantitative", domain: [0, 1], default: 0.5 },},},},data: {nodes: [{ id: "a", name: "Ana", x: 20, y: 70 },],links: [{ source: "a", target: "b", strength: 0.9 },],},marks: [link({inset: 3,channels: {strokeWidth: { field: "strength", scale: { range: [1, 7] } },stroke: { field: "strength", scale: { scheme: "ddramp" } },},// A handle at each link's source end, shown when you hover the link.edits: [edit.network.rewire({ channels: ["source"] })],}),node({size: 10,fill: "#20555A",channels: {x: { field: "x", edit: move({ channels: ["x", "y"] }) },y: { field: "y" },text: { field: "name" },},}),],});mount(chart.dom());
Link attributes
A link row is a row like any other. Give the links table columns, then encode them on link()'s style channels or edit them with set, cycle and editText.
channels.text draws a label at the midpoint, and takes editText() as a text mark does. Double-click anywhere along the connector to open the editor.
stroke and strokeWidth paint the connector and its arrowhead. fill paints the label (default: the theme's ink).
A label sits on its connector. labelBackground puts a plate behind it:
link({ labelBackground: true, channels: { text: { field: "note" } } })
true uses the theme's backdrop, which reads as a gap in the line. Pass a colour string for any other plate. labelOpacity (default 0.9) sets how much of the connector shows through. labelPadding and labelRadius size the plate.
Link attributes
A link's own columns drive its width, its colour and its label.
const chart = Elicit({width: 460, height: 290,margins: { top: 24, right: 24, bottom: 24, left: 24 },axes: false,schema: {structure: "network",tables: {people: {role: "nodes",fields: {id: { type: "categorical", key: true, open: true },name: { type: "categorical", open: true },x: { type: "quantitative", domain: [0, 100] },y: { type: "quantitative", domain: [0, 100] },},},ties: {role: "links",fields: {source: { type: "ref" },target: { type: "ref" },note: { type: "categorical", open: true },trust: { type: "quantitative", domain: [0, 1] },},},},},data: {people: [{ id: "a", name: "Ana", x: 18, y: 70 },],ties: [{ source: "a", target: "b", note: "worked together", trust: 0.9 },{ source: "b", target: "c", note: "met once", trust: 0.25 },],},marks: [link({inset: 3,fontSize: 11,labelBackground: true,channels: {strokeWidth: { field: "trust", scale: { range: [1, 6] } },stroke: { field: "trust", scale: { scheme: "ddramp" } },text: { field: "note" },},edits: [editText()],}),node({size: 10,fill: "#20555A",channels: {x: { field: "x", edit: move({ channels: ["x", "y"] }) },y: { field: "y" },text: { field: "name" },},}),],});mount(chart.dom());