Elicit.js

History & undo

chart.undo() and chart.redo() step back and forward one gesture at a time. Each step restores the data rows, the schema's domains and the chart size.

API reference

chart.undo() · chart.redo()

On the dataset, and on every chart through it. Snapshots, not inverse operations: an edit’s apply() is already a pure data → data function, so the state before it is the undo, and no edit ever has to describe how to reverse itself.

chart.undo() → boolean
chart.redo() → boolean
chart.canUndo() → boolean
chart.canRedo() → boolean
Options
OptionTypeDescription
the unit is a gesture
A drag commits on every pointermove, so a per-commit history would replay the drag backwards one pixel at a time. A transaction opens when a gesture starts and closes when it ends; the first commit inside it records the pre-gesture state, once. A click, a keyboard nudge, and a typed commit are each their own step.
undo() / redo()
→ boolean
Whether it actually moved — false at the end of the stack. Both fire the ordinary change notification: downstream, an undo is just the data changing.
canUndo() / canRedo()
→ boolean
For a button’s disabled state. Re-check them on change, which fires for edits, undo and redo alike.
what a snapshot holds
data + schema + size
Not just the rows. edit.axis.* writes the schema’s domain and can grow the chart, so undoing a category-add has to put the domain, the rows and the size back.
a new edit drops the redo branch
The usual linear-history rule: undo twice, then edit, and the future you undid past is gone.
setData() clears it
A reseed is a new starting point, not an edit — there is nothing sensible to undo back to, and undoing past it would resurrect rows the caller replaced, under a lock that no longer covers them.
depth
100 gestures
The oldest step falls off the end.
Returns / emits

History belongs to the dataset. Charts over one dataset share one history, so an undo steps back the last gesture in any of them; a chart’s size is put back on the chart that changed it. Charts over separate datasets each keep their own. See Datasets & linked charts.

Keyboard editing

Keyboard nudge is always on — there is no option to disable it. Any node carrying a direct edit is focusable (tabindex=0, role="button"), and an arrow key on it dispatches a nudge that goes through the edit’s own apply() — so the keyboard cannot drift from the pointer, because it isn’t a second path. The only chrome option is the browser focus ring below.

Options
OptionType · defaultDescription
Arrow keys
Move the focused mark along whichever axis carries an edit. On a continuous scale a press is 1% of the domain; on a band or point scale it steps one category, so a ranking moves by rank rather than by pixels.
Shift + arrow
A coarse step — 10% of the domain. (No effect on a categorical axis, where one category is already the smallest move.)
focusOutline
booleandefaultfalse
The browser’s native focus ring (the blue box after a click or Tab). Off by default — Tab and arrow keys still work; only the outline is suppressed. Set true when you want the ring visible (e.g. teaching keyboard editing). SVG/D3 renderer only.
Constraints & locks apply
Same edit, same dataset invariants: a nudge is clamped, snapped and repaired exactly as the drag is, and a locked row is not focusable at all.
One nudge, one undo step
A press has no dragstart/dragend to bracket, so it commits as its own transaction — arrow-arrow-arrow then undo three times.
Returns / emits

A nudge steps the value once, as a short drag would. It works for a direct move(), slide() or resize(), for a rotate() about its own mark, and for the brush edits.

Gesture-level undo & redo

A history transaction opens when a gesture starts and closes when it ends. A whole drag is one undo step.

undo() and redo() return a boolean: whether the history moved. Both fire the change event, so your listeners update.

Undo a whole drag

chart.undo() / chart.redo() step the answer. canUndo() / canRedo() drive the button states.

const chart = Elicit({
width: 420, height: 240,
margins: { top: 16, right: 16, bottom: 30, left: 40 },
schema: {
q: { type: "categorical", domain: ["Q1", "Q2", "Q3", "Q4"] },
n: { type: "quantitative", domain: [0, 100] },
},
data: [
{ q: "Q1", n: 40 }, { q: "Q2", n: 55 },
{ q: "Q3", n: 35 }, { q: "Q4", n: 60 },
],
marks: [
barY({ channels: { x: { field: "q" },
y: { field: "n", edit: move() } } }),
],
});
mount(chart.dom());
// Nothing here is special to undo: it is the ordinary chart API.
const controls = document.createElement("div");
controls.style.cssText = "display:flex;gap:8px;margin-top:10px;font:12px system-ui";
const undo = document.createElement("button");
const redo = document.createElement("button");
undo.textContent = "↶ Undo";
redo.textContent = "↷ Redo";
undo.onclick = () => chart.undo();
redo.onclick = () => chart.redo();
// canUndo / canRedo drive the buttons. `change` fires for edits AND for undo and
// redo themselves — downstream, all three are just "the data changed".
const sync = () => {
undo.disabled = !chart.canUndo();
redo.disabled = !chart.canRedo();
};
chart.on("change", sync);
sync();
controls.append(undo, redo);
mount(controls);
Try: Drag a bar, then press Undo — the bar resets in one step.

Transaction boundaries & snapshots

A snapshot holds the data rows, the schema's domains (added categories, rescaled axes) and the chart size.

  • Gesture unit: a drag, a sweep, a category addition and a keyboard nudge are each one transaction.
  • Redo branch: a new edit after an undo drops the redo branch.
  • Reseed: chart.setData() sets a new baseline and clears the undo stack.