Elicit.js

Layout

A layout settles marks in pixel space after an edit commits, then writes the settled positions and sizes back into the dataset. Declare layouts in layout: [...] on the Elicit spec. The root layouts work on any table: layout.dodge keeps marks from overlapping, layout.row, layout.column, layout.grid and layout.pack arrange them into slots or clusters, layout.tidy lines a hand layout up, and layout.custom runs your own pass. A layout that needs a structure is scoped to it: layout.network.layered, tree and force place a network's nodes by its links, and layout.hierarchy.contain sizes containers in a hierarchy.

API reference

Built-in layouts

Import layout from elicitjs and pass in the Elicit spec’s layout: [...]. Each rule settles mark boxes in pixel space and writes the resulting positions and sizes back to the dataset.

layout.hierarchy.contain({ padding?, fit?, where? }) → Layout
layout.dodge({ gap?, in?, where?, table? }) → Layout
layout.row({ gap?, in?, where?, table? }) → Layout
layout.column({ gap?, in?, where?, table? }) → Layout
layout.grid({ columns?, gap?, in?, where?, table? }) → Layout
layout.pack({ gap?, keep?, in?, where?, table? }) → Layout
layout.tidy({ snap?, gap?, in?, where?, table? }) → Layout
Options
OptionType · defaultDescription
contain.where
(d, i, rows) => booleandefaultall containers
Picks the containers this padding and fit apply to. Which rows may take a drop is constraints.contain or a mark’s contains.
contain.padding
number | { top, right, bottom, left }default10
Room in px between a container’s edge and its contents.
contain.fit
boolean | 'x' | 'y'defaultfalse
Resize containers to enclose their contents on both axes (true), width only ("x"), height only ("y"), or keep their given size (false).
dodge.gap
numberdefault8
Minimum space in px kept between overlapping siblings.
where
(d, i, rows) => booleandefaultall rows
Picks the rows the layout acts on: the rows that dodge, take a slot, line up, or (on hierarchy.contain) the containers its padding and fit apply to. The others stay where they are.
in
'all' | 'roots' | 'containers' | ((parent, i, rows) => boolean)default'all' (dodge, tidy) / 'containers' (row, column, grid, pack)
Scopes a rule to a level of a hierarchy: top-level roots, every container, both, or containers matching a predicate. On a flat table, "containers" and a predicate match nothing and log a warning.
gap
numberdefault8
Space in px between slots in row, column, grid or pack. A negative or non-number gap on any layout logs a warning and takes the default.
grid.columns
numberdefaultas many as fit
Cells per row in layout.grid.
pack.keep
'distance' | 'angle'default'distance'
How a dropped mark moves in to touch the cluster: "distance" picks the nearest free spot; "angle" preserves its direction from the cluster’s centre.
tidy.snap
numberdefault16
Centres this close, in px, share a row or a column.
tidy.gap
numberdefault8
Space in px a mark needs clear of its neighbours to keep its cell.
table
stringdefaultnodes (hierarchy) or primary table
Table name or role the layout settles in a multi-table schema. Scoped layouts (layout.network.*, layout.hierarchy.*) always settle their structure’s nodes table.

Network layouts

Place nodes from their links. Each works on the nodes table of a dataset with a links table: a network, or a hierarchy that declares one.

layout.network.layered({ direction?, gap?, rankGap?, place?, where? }) → Layout
layout.network.tree({ direction?, gap?, rankGap?, place?, where? }) → Layout
layout.network.force({ length?, gap?, place?, where? }) → Layout
Options
OptionType · defaultDescription
direction
'down' | 'right' | 'up' | 'left' (tree also 'radial')default'down'
Which way links run (layered) or the tree grows from its root (tree).
gap
numberdefault24 (layered), 12 (tree), 8 (force)
Space in px between neighbouring boxes.
rankGap
numberdefault48 (layered), 40 (tree)
Space in px between ranks or depths.
force.length
numberdefault100
Distance in px between the centres of two linked nodes.
place
'new' | 'all'default'new'
"new" positions only rows with no position; "all" redraws every row when the nodes or links change. Locked rows stay put.
where
(d, i, rows) => booleandefaultevery node
Picks the nodes the layout places. The others stay where they are.

layout.custom(fn, options?)

Author your own pixel-space layout pass. It runs after the built-in layouts, and the rows it moves are written back to the dataset.

layout.custom(({ rows, tables, tree, nodeBox, extentOf, scales, width, height, locked, anchors, yields, preview, shift, shiftTree, write }) => rows | void, { table?, where? }) → Layout
Options
OptionType · defaultDescription
fn
(ctx) => rows | voiddefaultrequired
Reads a row’s drawn box with ctx.nodeBox(i) and moves it with ctx.shift(i, dx, dy), in plot pixels, or sets its fields with ctx.write(i, patch). It can also return a new array of rows.
ctx.nodeBox(i) / ctx.extentOf(i)
→ { cx, cy, hw, hh } | null
The row’s drawn box, and the space its whole glyph takes, in plot pixels.
ctx.shift(i, dx, dy) / ctx.shiftTree(i, dx, dy)
→ void
Move a row by a pixel delta, alone or with every row nested inside it. A locked row stays where it is.
ctx.write(i, patch)
→ void
Merge fields into a row.
ctx.rows / ctx.tables / ctx.tree
Datum[] / Record<name, Datum[]> / tree
The rows as the earlier layouts left them, every table, and the hierarchy’s containment.
ctx.locked / ctx.anchors / ctx.yields / ctx.preview
fn | null / Set / Set | null / boolean
Which rows are locked, which hold their place, which the gesture moved, and whether a drag is still going.
table
stringdefaultprimary table
Table name or role to settle.
where
(d, i, rows) => booleandefaultall rows
The rows fn may change. Changes to any other row are dropped.

Layout vs constraints

Constraints and layouts enforce two different kinds of rule:

  • A constraint is a pure-data invariant (maintainSum, clamp, ordering, constraints.contain). It runs on committed data rows and applies across every chart sharing a dataset.
  • A layout works in pixel space. It measures each mark's drawn box (its width, height, radius or label extent), resolves overlaps, slots or container fits, and inverts the settled pixels back into the data columns within the same undo step.

While you drag, overlapping or slotted neighbours preview live and ease with motion; the settled positions commit once on release.

The rows a layout settles then run through the dataset's constraints for that table, so a clamp still holds. If a constraint rejects them, the layout's rows are kept and a warning says so.

dodge — keep marks apart

layout.dodge({ gap, where, in }) pushes overlapping marks apart by gap pixels (default 8). It works on any table of positioned marks — cards, scatter points, or network nodes — with no hierarchy required.

layout: [
layout.dodge({ gap: 10 }),
]

When you drop a mark on top of another, the dropped mark holds its place and the others move the shortest distance that clears it. Locked rows stay put.

Non-overlapping cards on a flat table

layout.dodge({ gap: 10 }) keeps draggable cards apart without a hierarchy.

const chart = Elicit({
width: 520, height: 300,
margins: { top: 16, right: 16, bottom: 16, left: 16 },
axes: false,
schema: {
id: { type: "categorical", key: true },
label: { type: "categorical", open: true },
x: { type: "quantitative", domain: [0, 100] },
y: { type: "quantitative", domain: [0, 100] },
w: { type: "quantitative", domain: [0, 400] },
h: { type: "quantitative", domain: [0, 200] },
},
data: [
{ id: "a", label: "Clarity", x: 25, y: 65, w: 96, h: 32 },
],
layout: [
layout.dodge({ gap: 10 }),
],
marks: [
rect({
cornerRadius: 6,
fill: "#EDF7F8",
stroke: "#46596B",
edits: [move({ channels: ["x", "y"], mode: "relative" })],
channels: {
x: { field: "x" },
y: { field: "y" },
width: { field: "w", scale: null },
height: { field: "h", scale: null },
},
}),
text({
fontSize: 11,
fill: "#20555A",
channels: {
x: { field: "x" },
y: { field: "y" },
text: { field: "label" },
},
}),
],
});
mount(chart.dom());
Try: Drag any card onto another and release: the others make room.

row, column, grid and pack

Arrangement rules place marks into slots or tight clusters:

  • layout.row({ gap, in, where }) arranges left to right, wrapping at the available width.
  • layout.column({ gap, in, where }) arranges top to bottom, wrapping at the available height.
  • layout.grid({ columns, gap, in, where }) places equal cells in reading order.
  • layout.pack({ gap, keep, in, where }) draws marks together until they touch, edge to edge for boxes and circumference to circumference for circles.

On a flat table or a network, every row in the table takes part.

Packed circles on a flat table

layout.pack({ gap: 4 }) gathers dragged circles into a tight cluster.

const chart = Elicit({
width: 520, height: 300,
margins: { top: 16, right: 16, bottom: 16, left: 16 },
axes: false,
schema: {
id: { type: "categorical", key: true },
label: { type: "categorical", open: true },
x: { type: "quantitative", domain: [0, 100] },
y: { type: "quantitative", domain: [0, 100] },
r: { type: "quantitative", domain: [0, 100] },
},
data: [
{ id: "a", label: "Solar", x: 50, y: 50, r: 28 },
],
layout: [
layout.pack({ gap: 4 }),
],
marks: [
point({
fill: "#C3B9DF",
stroke: "#46596B",
edits: [move({ channels: ["x", "y"], mode: "relative" })],
channels: {
x: { field: "x" },
y: { field: "y" },
size: { field: "r", scale: null },
},
}),
text({
fontSize: 10,
fill: "#20555A",
channels: {
x: { field: "x" },
y: { field: "y" },
text: { field: "label" },
},
}),
],
});
mount(chart.dom());
Try: Drag any circle to the edge of the cluster and let go: it slides in to touch.

Composing levels in a hierarchy

In a hierarchy, in scopes each rule to a level of the tree. dodge, tidy, row, column, grid and pack all take the same values:

  • "containers" — inside every container (the default for row, column, grid and pack).
  • "roots" — top-level nodes only.
  • "all" — both roots and containers (the default for dodge and tidy).
  • (parent, i, rows) => boolean — only inside containers matching the predicate.

A flat table's rows are all roots. There, "containers" and a predicate match nothing, so the layout does nothing and logs a warning.

Combine rules in layout: [...] to arrange roots one way and container contents another. layout.hierarchy.contain({ padding, fit }) accepts fit: true, "x", "y", or false so containers can grow on a single axis while holding a fixed width.

Lanes in a row, cards in a column

layout.row({ in: "roots" }) aligns lanes side by side; layout.column stacks cards and fit: "y" grows each lane vertically.

const chart = Elicit({
width: 560, height: 340,
margins: { top: 16, right: 16, bottom: 16, left: 16 },
axes: false,
schema: {
structure: "hierarchy",
tables: {
nodes: {
id: { type: "categorical", key: true, open: true },
label: { type: "categorical", open: true },
kind: { type: "categorical", domain: ["lane", "card"] },
x: { type: "quantitative", domain: [0, 100] },
y: { type: "quantitative", domain: [0, 100] },
w: { type: "quantitative", domain: [0, 600], default: 150 },
h: { type: "quantitative", domain: [0, 600], default: 72 },
},
nesting: {
child: { type: "ref" },
parent: { type: "ref" },
},
},
},
data: {
nodes: [
{ id: "todo", label: "To do", kind: "lane", x: 18, y: 80, w: 150, h: 72 },
],
nesting: [
{ child: "c1", parent: "todo" },
],
},
constraints: [
constraints.contain({ parent: (d) => d.kind === "lane", child: (d) => d.kind === "card" }),
],
layout: [
layout.hierarchy.contain({ padding: { top: 30, right: 10, bottom: 10, left: 10 }, fit: "y" }),
layout.row({ in: "roots", gap: 16 }),
layout.column({ in: (d) => d.kind === "lane", gap: 6 }),
],
marks: [
rect({
cornerRadius: 6,
stroke: "#46596B",
edits: [
move({ channels: ["x", "y"], mode: "relative" }),
edit.hierarchy.nest(),
],
channels: {
x: { field: "x" },
y: { field: "y" },
width: { field: "w", scale: null },
height: { field: "h", scale: null },
fill: { fn: (d) => (d.kind === "lane" ? "#EDF7F8" : "#FFFFFF") },
},
}),
text({
fontSize: 11,
fill: "#20555A",
channels: {
x: { field: "x" },
y: { field: "y" },
text: { field: "label" },
dy: { fn: (d) => (d.kind === "lane" ? -d.h / 2 + 15 : 0) },
},
}),
],
});
mount(chart.dom());
Try: Drag a lane left or right to reorder the columns. Drag a card within a lane or into another lane: the lane grows or shrinks vertically while keeping its width.

Network layouts

layout.network.* places nodes from their links. Each one works on the nodes table of a dataset with links: a network, or a hierarchy that declares a links table. On a dataset with no links it logs a warning and does nothing. where picks the nodes it places.

  • layout.network.layered({ direction, gap, rankGap, place, where }) draws ranks for flowcharts and causal maps. Every link runs one way (direction: "down", "right", "up" or "left"), and nodes in a rank are ordered to cut crossings. A cycle has to break somewhere, so one link in each loop points back.
  • layout.network.tree({ direction, gap, rankGap, place, where }) draws a tidy tree for trees and mind maps. Each parent sits centred on its children, and siblings keep the order you drew them in. direction: "radial" puts the root in the middle with a ring per depth. The root is a node no link points to. Links that make a cycle or give a node two parents are left out of the layout, with a warning.
  • layout.network.force({ length, gap, place, where }) spreads a concept map. Linked nodes sit about length px apart (default 100), and other pairs sit farther apart the more links lie between them. The same rows always give the same drawing.

gap is the space in px between neighbouring boxes, and rankGap the space between ranks or depths.

A causal map laid out in ranks

layout.network.layered places factors that have no position. The loop through Supply points one link back up.

const chart = Elicit({
width: 560, height: 440,
margins: { top: 16, right: 16, bottom: 16, left: 16 },
axes: false,
schema: {
structure: "network",
tables: {
factors: {
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] },
},
},
causes: {
role: "links",
directed: true,
fields: {
source: { type: "ref" },
target: { type: "ref" },
},
},
},
},
// No row has a position: the layout places them all.
data: {
factors: [
{ id: "rates", label: "Interest rates" },
],
causes: [
{ source: "rates", target: "mort" },
],
},
layout: [
layout.network.layered({ direction: "down", gap: 20, rankGap: 28 }),
],
marks: [
link({ arrow: true, stroke: "#7D8B99" }),
sticker({
fill: "#ECF3F9",
stroke: "#1D4467",
cornerRadius: 8,
channels: {
x: { field: "x", edit: move({ channels: ["x", "y"], mode: "relative" }) },
y: { field: "y" },
text: { field: "label" },
ports: { value: true, edit: edit.network.connect() },
},
}),
],
});
mount(chart.dom());
Try: Drag a factor anywhere: it stays where you drop it. Drag from a port to another factor to add a cause.

A mind map as a tidy tree

layout.network.tree({ direction: "right", place: "all" }) redraws the tree on every change.

const chart = Elicit({
width: 560, height: 440,
margins: { top: 16, right: 16, bottom: 16, left: 16 },
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] },
},
},
branches: {
role: "links",
directed: true,
fields: {
source: { type: "ref" },
target: { type: "ref" },
},
},
},
},
data: {
ideas: [
{ id: "trip", label: "Trip", x: 50, y: 50 },
],
branches: [
{ source: "trip", target: "travel" },
],
},
layout: [
layout.network.tree({ direction: "right", gap: 8, place: "all" }),
],
marks: [
link({ stroke: "#7D8B99" }),
sticker({
fill: "#F3F0FA",
stroke: "#4B3D78",
cornerRadius: 8,
channels: {
x: { field: "x", edit: move({ channels: ["x", "y"], mode: "relative" }) },
y: { field: "y" },
text: { field: "label" },
},
}),
],
});
mount(chart.dom());
Try: Drag Hiking below Museums and let go: the two swap places.

Placing new rows or every row

place decides which rows a network layout moves.

  • place: "new" (the default) positions only rows with no x or y yet: the seed, a setData call, or rows added from outside. Every row that has a position stays where it is, so a participant's arrangement is never redrawn.
  • place: "all" redraws every row whenever the nodes or links change. It starts from the current drawing, so a dragged node changes its rank order, sibling order or starting point instead of springing back.

Locked rows stay put under both.

New concepts placed around the ones already drawn

layout.network.force({ length: 80 }) places the seven concepts with no position. The three placed ones stay where they are.

const chart = Elicit({
width: 560, height: 360,
margins: { top: 16, right: 16, bottom: 16, left: 16 },
axes: false,
schema: {
structure: "network",
tables: {
concepts: {
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] },
},
},
relations: {
role: "links",
directed: false,
fields: {
source: { type: "ref" },
target: { type: "ref" },
},
},
},
},
// Three concepts are placed; the rest arrive with no position.
data: {
concepts: [
{ id: "co2", label: "CO₂", x: 50, y: 55 },
],
relations: [
{ source: "energy", target: "coal" },
],
},
layout: [
layout.network.force({ length: 80 }),
],
marks: [
link({ stroke: "#7D8B99" }),
sticker({
fill: "#EDF7F1",
stroke: "#22573A",
cornerRadius: 8,
channels: {
x: { field: "x", edit: move({ channels: ["x", "y"], mode: "relative" }) },
y: { field: "y" },
text: { field: "label" },
},
}),
],
});
mount(chart.dom());
Try: Drag any concept: nothing else moves.

where — lay out some of the rows

Every layout takes where: (d, i, rows) => boolean. It picks the rows the layout acts on, and the others stay where they are. On layout.dodge the picked rows dodge, on row, column, grid and pack they take the slots, on tidy they line up, and a network layout places them, leaving links to other nodes out of the layout. On layout.hierarchy.contain it picks the containers its padding and fit apply to, and layout.custom can change only the rows it picks. Give two layouts different where rules to lay out two parts of one diagram differently.

In a hierarchy, a container with fit: true grows round the nodes the layout places inside it. A node the layout moves carries its own contents.

Two groups, each with its own layout

layout.network.layered({ where: (d) => d.kind === "step" }) places the sign-up steps, and layout.network.tree({ where: (d) => d.kind === "topic" }) the help topics. The groups and the note are left where they are.

const chart = Elicit({
width: 640, height: 420,
margins: { top: 16, right: 16, bottom: 16, left: 16 },
axes: false,
schema: {
structure: "hierarchy",
tables: {
nodes: {
id: { type: "categorical", key: true, open: true },
label: { type: "categorical", open: true },
kind: { type: "categorical", domain: ["group", "step", "topic", "note"] },
x: { type: "quantitative", domain: [0, 100] },
y: { type: "quantitative", domain: [0, 100] },
w: { type: "quantitative", domain: [0, 600] },
h: { type: "quantitative", domain: [0, 400] },
},
nesting: {
child: { type: "ref" },
parent: { type: "ref" },
},
links: {
directed: true,
fields: {
source: { type: "ref" },
target: { type: "ref" },
},
},
},
},
// The groups and the note are placed. The steps and topics have no position:
// each group's layout places them.
data: {
nodes: [
{ id: "signup", label: "Sign-up flow", kind: "group", x: 22, y: 55, w: 120, h: 120 },
],
nesting: [
{ child: "visit", parent: "signup" },
],
links: [
{ source: "visit", target: "join" },
],
},
constraints: [
constraints.contain({ parent: (d) => d.kind === "group" }),
],
layout: [
layout.hierarchy.contain({ padding: { top: 30, right: 14, bottom: 14, left: 14 }, fit: true }),
// Each layout places only the nodes `where` picks.
layout.network.layered({ where: (d) => d.kind === "step", gap: 14, rankGap: 20, place: "all" }),
layout.network.tree({ where: (d) => d.kind === "topic", direction: "right", gap: 8, rankGap: 26, place: "all" }),
// Keep the groups and the note apart as the groups grow.
layout.dodge({ in: "roots", gap: 16 }),
],
marks: [
rect({
cornerRadius: 6,
stroke: "#46596B",
edits: [move({ channels: ["x", "y"], mode: "relative" })],
channels: {
x: { field: "x" },
y: { field: "y" },
width: { field: "w", scale: null },
height: { field: "h", scale: null },
fill: { fn: (d) => (d.kind === "group" ? "#F3F6F9" : d.kind === "note" ? "#FFF6DD" : "#DCEAF6") },
},
}),
link({ stroke: "#46596B", arrow: true }),
text({
fontSize: 11,
fill: "#20414F",
channels: {
x: { field: "x" },
y: { field: "y" },
text: { field: "label" },
dy: { fn: (d) => (d.kind === "group" ? -d.h / 2 + 15 : 0) },
},
}),
],
});
mount(chart.dom());
Try: Drag a group: its nodes go with it. Drag a step away and let go: it goes back to its rank.

Layouts and link shapes together

A layout places the nodes, and the link mark's curve draws the links between them. sourceSide and targetSide pin where a link leaves and arrives, so its ends follow the layout's direction. Links are drawn one at a time, so several can end at one point on a node, and long links in a layered drawing pass close to the nodes they skip.

Diagram
Layout
Direction
Links
Link ends
Place

Layout and link explorer

Pick a diagram, a layout and a link shape. The code is the spec for the chart you see.

const chart = Elicit({
width: 620, height: 320,
margins: { top: 12, right: 12, bottom: 12, left: 12 },
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, 100] },
y: { type: "quantitative", domain: [0, 100] },
},
},
flows: {
role: "links",
directed: true,
fields: {
source: { type: "ref" },
target: { type: "ref" },
},
},
},
},
// No node has a position: the layout places them all.
data: {
steps: [
{ id: "apply", label: "Apply" },
],
flows: [
{ source: "apply", target: "id" },
],
},
layout: [
layout.network.layered({ direction: "right", gap: 14, rankGap: 24, place: "all" }),
],
marks: [
link({ curve: "orthogonal", sourceSide: "right", targetSide: "left", arrow: true, stroke: "#5A6B7D" }),
sticker({
fill: "#ECF3F9",
stroke: "#1D4467",
cornerRadius: 7,
maxWidth: 100,
channels: {
x: { field: "x", edit: move({ channels: ["x", "y"], mode: "relative" }) },
y: { field: "y" },
text: { field: "label" },
},
}),
],
});
mount(chart.dom());
Try: Drag a node and let go. With place: "all" the layout puts it back in order, and a node dropped past a sibling swaps with it. With "new" it stays where you drop it.

tidy — line a hand layout up

layout.tidy({ snap, gap, where, in }) keeps the arrangement you drew and straightens it. Centres within snap px of each other (default 16) share a row or a column. Rows and columns that are nearly evenly spaced become exactly even. A mark that overlaps another and stands alone in its column moves to the nearest free cell in its row, keeping gap px clear (default 8). It works on any table, and in scopes it in a hierarchy as it does dodge.

A rough sketch lined up

layout.tidy({ snap: 20 }) snaps the cards into rows and columns and moves Docs off Ship.

const chart = Elicit({
width: 520, height: 300,
margins: { top: 16, right: 16, bottom: 16, left: 16 },
axes: false,
schema: {
id: { type: "categorical", key: true },
label: { type: "categorical", open: true },
x: { type: "quantitative", domain: [0, 100] },
y: { type: "quantitative", domain: [0, 100] },
},
// A rough sketch: nearly in rows and columns, one card on top of another.
data: [
{ id: "a", label: "Idea", x: 14, y: 78 },
],
layout: [
layout.tidy({ snap: 20, gap: 10 }),
],
marks: [
sticker({
fill: "#FBF3E6",
stroke: "#6B4A1D",
cornerRadius: 6,
channels: {
x: { field: "x", edit: move({ channels: ["x", "y"], mode: "relative" }) },
y: { field: "y" },
text: { field: "label" },
},
}),
],
});
mount(chart.dom());
Try: Drag a card near a row and let go: it lines up with it.

Custom pixel-space layouts

layout.custom(fn, { table?, where? }) runs your own pass after the built-in layouts. fn reads each row's drawn box and moves the row in plot pixels. The move is written back to the row's columns through the channels' scales.

layout: [
// Keep every box inside the plot's left and right edges.
layout.custom(({ rows, nodeBox, shift, width }) => {
rows.forEach((row, i) => {
const box = nodeBox(i);
if (!box) return;
const cx = Math.max(box.hw, Math.min(width - box.hw, box.cx));
shift(i, cx - box.cx, 0);
});
}),
]

fn receives one context:

  • rows and tables are the rows as the earlier layouts left them.
  • nodeBox(i) is row i's drawn box, { cx, cy, hw, hh } in plot pixels, or null when no mark draws the row. extentOf(i) is the space the whole glyph takes, label included.
  • shift(i, dx, dy) moves a row by a pixel delta. shiftTree(i, dx, dy) also moves every row nested inside it.
  • write(i, patch) merges fields into a row.
  • width, height and scales describe the plot.
  • tree is the hierarchy's containment: parentOf(i), childrenOf(i).
  • locked(row, i) tells a locked row. anchors is the set of rows that hold their place, and yields the set the gesture moved. preview is true while a drag is still going.

shift and write leave a locked row as it is, and a row outside where. fn can also return a new array of rows.