Elicit.js

External controls

Drive a chart from a control outside it: an input, a <select>, a slider, a rotate icon or a plain function. set(field, value, { index }) writes a value straight into the dataset. chart.control(name).set(value) replays a gesture through a named edit and the chart's scales. Both run constraints and undo, and fire on("change").

API reference

set(field, value, options?)

A dataset method, carried by every chart: write one cell from outside any chart. It runs the dataset’s constraints, then the lock, as an edit does; an edit’s own constrain does not run. One undo step; fires change and redraws every chart on the dataset. See Datasets & linked charts.

chart.set("n", 62)                   // row 0 of the primary table
chart.set("n", "62", { index: 2 })   // stored as the number 62
ds.set("weight", 3, { index: 0, table: "links" })
Returns / emits

true when it commits. false when a constraint rejects it, the row is locked or the value is unchanged, and, with a warning, for an undeclared field, an index out of range or a value the field’s type can’t hold. Writes rows that exist; to add one, replay a create gesture with chart.emit, or call setData.

edit({ name }) · chart.control(name, target?)

Give any edit a name, then replay its gesture from outside the chart with chart.control(name). The control synthesizes the same events a pointer does on that chart and feeds them to the one dispatch — so constraints, the edit’s own constrain, undo, guides, and on("change") all run. A new input source, not a second interaction system.

move({ name: "move", channels: ["x", "y"] })   // name an edit
const h = chart.control("move", 0);   // handle bound to the edit + datum 0
h.set(value)          // one-shot write (value edit) …
h.set({ x, y })       // … or a data value per channel (positional edit)
h.begin(); h.set(v); h.end();   // live drag → ONE undo entry
h.fire()              // TRIGGER edit (cycle/remove/toggle) — fires its click gesture
h.emit(event)         // raw event to this edit alone, on this row's node
h.cancel()            // abandon an open stroke
chart.control("link", { index: 0, channel: "port", side: "right" })   // one handle of a row
h.accepts()           // what the channel's scale allows
Options
OptionType · defaultDescription
edit.name
stringdefaultnull
The handle this edit answers to. An edit with no name stays pointer- and keyboard-driven.
control(name, target?)
→ EditControldefaulttarget: 0
A handle bound to that edit and that row. target is a row index, or { index, channel, side } to name one of the row’s handles: a port’s side, a link’s "source" or "target" end. The edit’s when is not asked. Ask for the handle again after the data changes; it resolves the edit on each call.
h.set(value)
void
Write a data value. A scalar for a single-channel edit, or a map keyed by channel or field for a multi-channel one. Untouched axes hold their current position rather than teleporting. Drives a direct edit, or a nearest or sweep pick on the control's row. Other plane edits log a warning; use chart.emit for them.
h.begin() / h.end()
void
Bracket a live drag, so many set ticks collapse into one undo entry — exactly as a pointer drag does. end() releases where the stroke’s pointer last was, so a connect or a rewire lands there. begin({ x, y }) presses at that plot pixel in place of the row’s own.
h.cancel()
void
Abandon the stroke begin() opened, with no release. An edit that commits on release writes nothing; what earlier ticks wrote stays, as one undo entry.
h.fire()
void
Trigger an edit that takes no value — cycle, remove, toggle. It dispatches the edit’s own gesture on the row.
h.accepts()
→ object | null
What the channel’s scale allows: { field, type, kind, temporal, invertible, domain, values, range }. values is the accepted set on a discrete channel and null on a continuous one, where domain is the accepted range — so a picker or slider can offer only valid choices.
h.emit(event)
void
A raw event to this edit alone, on its node for the row. A plane edit takes it at the event's x and y. The low-level route when you already hold pixels.
Returns / emits

Every route runs the ordinary dispatch, so a constraint gates an external write exactly as it gates a drag. External control is not a bypass.

chart.transaction()

A dataset method, carried by every chart: hold one undo entry open across any number of writes. Everything committed until you call the returned function undoes as one step. change still fires per write.

const end = chart.transaction();
chart.set("w", 140, { index: 0 }); chart.set("x", 30, { index: 0 });
end();
Returns / emits

The closer. Undo, redo and setData close an open transaction; calling the closer twice does nothing.

chart.scene · hit · scale · edits · metrics

Read the drawn chart, for UI placed over it. Every value is a copy of the current frame; read again on on("render"). Pixels are plot pixels.

chart.scene({ mark: "note", index: 2 })   // the row's drawn pieces
chart.hit({ x, y }, { reach: 12 })       // the piece at, or nearest, a point
chart.scale("x", { mark: "note" }).invert(px)
chart.edits({ mark: "note", index: 2 })   // the edits that row answers to
chart.metrics()                          // { width, height, margins, innerWidth, innerHeight, scale }
chart.toInner({ x: e.clientX, y: e.clientY })   // page -> plot pixels; toClient is the inverse
chart.on("render", () => { … })           // after every frame
Options
OptionType · defaultDescription
scene(query?)
→ SceneView[]defaultevery piece
One view per drawn piece, in paint order: { mark, table, index, type, channel, side, box, center, angle, datum, hovered, selected, grabbed, visible, editable, locked }. query is { mark, table, index, channel, side }. index counts the rows getData() reports. visible is false for a port or a link end the chart is hiding.
hit(point, { reach }?)
→ SceneView | nulldefaultreach: 0
The topmost visible piece at a point, or the nearest within reach pixels.
scale(channel, { mark, index }?)
→ object | nulldefaultthe chart's shared scale
{ channel, field, table, encode, invert, paired, domain, range, kind, temporal, invertible }. invert is null on a scale that can’t be read back. paired names the partner axis of a map’s lon or lat, and is null on any other scale. null when nothing binds a field on the channel.
edits(query?)
→ EditInfo[]defaultevery edit
{ name, type, gesture, pick, channels, fields, mark, table, cardinality, action?, available }. available is whether it can run now: its stage is current, the row is unlocked and, on a port, a link could land somewhere. action appears only on edits that share a type, such as the 'add', 'rename' and 'remove' of scale.categories.
metrics()
→ object
The chart’s size and margins in chart pixels. scale is CSS pixels per chart pixel: 1 unless responsive: "scale" stretches the drawing.
toInner(point) · toClient(point)
→ { x, y }
Convert between the page's viewport pixels and plot pixels.
on("render", cb) · on("destroy", cb)
→ unsubscribe
After each frame the chart draws; once, when the chart is destroyed.
chart.theme
Theme
A copy of the resolved theme.
chart.destroyed
booleandefaultfalse
true once destroy() has run. Read-only. UI built over a chart checks it before it attaches; ui.attach does.

set(options?)

The universal value edit: write a value into a channel's field — quantitative, colour, categorical, temporal. The commit-gesture counterpart to the positional edits, and what an external picker/slider drives via control(name).set(value). (editText is its text specialization.)

fill: { field: "group", edit: set({ name: "category" }) }
chart.control("category", i).set("B")

select() · chart.select · chart.selectWhere

Selection is transient pipeline state, not a selected data column. Put edit.select() on a mark to select it by click; drive the same state from outside with chart.select(index) (a specific item) or chart.selectWhere(field, value) (a category — the first matching row). The selected row gets the highlight outline, and a legend picker's row defaults to it. A select event fires; on("change")/getData never do.

edits: [ select() ]            // click a mark to select it
chart.select(2)                   // select a SPECIFIC item by index
chart.selectWhere("group", "North")  // select by CATEGORY (first match)
chart.getSelection()             // → the lowest index | null
chart.clearSelection()           // deselect all
chart.on("select", (i, all) => …)  // selection changed (no data moved)

chart.setDomain(field, domain, options)

Write a field’s domain from outside the chart — a zoom button, a reset control. The same commit a wheel zoom, a pan or an axis drag makes: one undo step, a change notification, and every scale reflows. Pass null to restore the declared domain. See Zoom & pan.

chart.setDomain("x", [20, 40])            // → true
chart.setDomain("x", null)                // restore the declared domain
chart.setDomain("weight", [0, 5], { table: "links" })

chart.emit(event)

Low-level: inject a renderer-shaped gesture event directly. x/y are inner (margin-subtracted) pixels. Prefer control, which computes pixels from a data value for you. An event from chart.emit names no row, so it is a plane gesture: a click that a create() answers, a drag that a sweep answers. To aim at one row, use control(name, index).

chart.emit({ type: "click", x: 120, y: 80 })                    // a plane gesture
chart.control(name, index).set(value)                           // a value, on one row
chart.control(name, index).emit({ type: "drag", x, y })         // a pointer event, on one row

Write a value with set

set(field, value, { index, table }) is a dataset method, and every chart carries it. It writes one cell, runs the dataset's constraints and then the lock, and is one undo step. index counts the rows getData() reports and defaults to 0. The value is stored as the field's type, so an input's "62" becomes the number 62. No edit runs, so set works on any declared field, with or without a chart. See Datasets & linked charts.

Type a value per bar

Each input calls chart.set("n", value, { index: i }). The clamp on the dataset caps a typed 95 at 80.

// `chart.set(field, value, { index })` writes one cell of the dataset. No edit
// is involved, so the bars need none for the inputs to work; `move()` is here so
// you can drag them too.
const quarters = ["Q1", "Q2", "Q3", "Q4"];
const chart = Elicit({
width: 380, height: 220,
margins: { top: 14, right: 16, bottom: 28, left: 36 },
schema: {
q: { type: "categorical", domain: quarters },
n: { type: "quantitative", domain: [0, 100] },
},
data: quarters.map((q) => ({ q, n: 40 })),
// The dataset's rules hold for a typed value as they do for a drag.
constraints: [clamp({ min: 0, max: 80, field: "n" })],
marks: [
barY({
fill: "#50AEB7",
channels: { x: { field: "q" }, y: { field: "n", edit: move() } },
}),
],
});
mount(chart.dom());
const row = document.createElement("div");
row.style.cssText = "display:flex;gap:10px;flex-wrap:wrap;margin-top:10px;font:13px system-ui";
const inputs = quarters.map((q, i) => {
const label = document.createElement("label");
label.style.cssText = "display:flex;flex-direction:column;gap:2px;color:#6F8294";
label.textContent = q;
const input = document.createElement("input");
input.type = "number"; input.min = 0; input.max = 100; input.style.width = "64px";
// The input's value is a string; `set` stores it as the field's type.
input.onchange = () => chart.set("n", input.value, { index: i });
label.append(input);
row.append(label);
return input;
});
const sync = () => chart.getData().forEach((d, i) => { inputs[i].value = Math.round(d.n); });
chart.on("change", sync);
sync();
mount(row);
Try: Type a value under a quarter, or drag a bar.

Name an edit, then drive it

Use control when the write should act like the gesture: an edit's own constrain, a click edit such as cycle, a rotate about the mark. Give the edit a name, then call chart.control(name).

An edit maps a gesture to data by inverting a channel's scale. control(name).set(value) runs that scale forward to find the gesture, then runs the ordinary edit. A value edit (set, editText) takes the value directly, with no pixel step. A value outside a closed categorical domain is refused with a warning; one outside a numeric or temporal domain is clamped to it. control(name).accepts() reports what the channel's scale allows, so a picker or slider can offer only valid values.

A control names its edit, so the edit's when is not asked: when chooses between pointer gestures. Constraints, the lock and the edit's stage still apply. For an edit only a control should fire, give it gesture: "commit", a gesture no pointer makes, and drive it with .fire().

A picker and a drag pad

The <select> writes the point's category through a set() edit. Its options come from control("category", 0).accepts().values. The pad drives a two-axis move live, and begin() … set({x,y}) … end() is one undo entry. The clamp on y limits the pad exactly as it limits a mouse drag.

Scatter: category picker + drag pad

A <select> sets the category; a draggable pad moves the point through the clamped drag edit.

// External controllers drive the SAME edits a pointer does. A `<select>` sets the
// category (a value edit); a draggable pad moves the point (a positional edit).
// Both go through the normal pipeline — the clamp on y holds for the pad exactly
// as it would for a mouse drag.
const chart = Elicit({
width: 380, height: 260,
margins: { top: 14, right: 14, bottom: 26, left: 30 },
schema: {
x: { type: "quantitative", domain: [0, 100] },
y: { type: "quantitative", domain: [0, 100] },
group: { type: "categorical", domain: ["A", "B", "C"] },
},
data: [
{ x: 30, y: 40, group: "A" },
],
// A dataset invariant: y can never exceed 80. It gates EVERY edit, including the
// pad drag below — external control is not a bypass.
constraints: [ clamp({ field: "y", max: 80 }) ],
marks: [
point({
size: 7,
channels: {
x: { field: "x" },
y: { field: "y" },
fill: {
field: "group",
scale: { range: ["#50AEB7", "#315228", "#F2B94B"] },
// A universal value edit: an external picker writes the chosen category.
edit: set({ name: "category" }),
},
},
// One drag over BOTH axes, named so a controller can address it.
edits: [ move({ name: "move", channels: ["x", "y"] }) ],
}),
],
});
mount(chart.dom());
// We drive datum 0 (the first point). `.accepts()` reads what the channel allows.
const i = 0;
const cats = chart.control("category", i).accepts().values; // ["A","B","C"]
const [dx, dy] = [chart.getSchema().x.domain, chart.getSchema().y.domain];
const controls = document.createElement("div");
controls.style.cssText = "display:flex;gap:16px;align-items:center;margin-top:10px;font:12px system-ui";
// 1) Category picker — options come straight from the scale's accepted values.
const pick = document.createElement("select");
for (const c of cats) {
const o = document.createElement("option");
o.value = c; o.textContent = c; pick.append(o);
}
pick.value = chart.getData()[i].group;
pick.onchange = () => chart.control("category", i).set(pick.value);
const pickWrap = document.createElement("label");
pickWrap.style.cssText = "display:flex;gap:6px;align-items:center";
pickWrap.append("category", pick);
// 2) A draggable pad — the "UI element around the point". Dragging maps the pad's
// local position to data space and drives the `move` edit live: begin() … set() …
// end() collapse into ONE undo entry, just like a real drag.
const pad = document.createElement("div");
pad.style.cssText = "position:relative;width:120px;height:120px;border:1px solid #BCC9D4;border-radius:8px;background:#F6F9FB;touch-action:none;cursor:grab";
const knob = document.createElement("div");
knob.style.cssText = "position:absolute;width:14px;height:14px;margin:-7px 0 0 -7px;border-radius:50%;background:#14283A";
pad.append(knob);
const placeKnob = () => {
const d = chart.getData()[i];
knob.style.left = ((d.x - dx[0]) / (dx[1] - dx[0]) * 100) + "%";
knob.style.top = (100 - (d.y - dy[0]) / (dy[1] - dy[0]) * 100) + "%";
};
let handle = null;
const toData = (ev) => {
const r = pad.getBoundingClientRect();
const fx = Math.max(0, Math.min(1, (ev.clientX - r.left) / r.width));
const fy = Math.max(0, Math.min(1, (ev.clientY - r.top) / r.height));
return { x: dx[0] + fx * (dx[1] - dx[0]), y: dy[0] + (1 - fy) * (dy[1] - dy[0]) };
};
pad.addEventListener("pointerdown", (ev) => {
pad.setPointerCapture(ev.pointerId);
handle = chart.control("move", i);
handle.begin();
handle.set(toData(ev));
});
pad.addEventListener("pointermove", (ev) => { if (handle) handle.set(toData(ev)); });
const stop = () => { if (handle) { handle.end(); handle = null; } };
pad.addEventListener("pointerup", stop);
pad.addEventListener("pointercancel", stop);
const padWrap = document.createElement("div");
padWrap.style.cssText = "display:flex;flex-direction:column;gap:4px;align-items:center;color:#6F8294";
padWrap.append(pad, document.createTextNode("drag to move point 0"));
controls.append(pickWrap, padWrap);
mount(controls);
// Keep the picker + knob in sync with committed data (edits, undo, redo all fire).
chart.on("change", () => { pick.value = chart.getData()[i].group; placeKnob(); });
placeKnob();
Try: pick a category; drag the pad; the point obeys the y ≤ 80 clamp either way.

Pick vs. cycle: driving other gestures

.set(value) drives the value edits (set, editText) and the positional ones (move, rotate, resize). The click edits (cycle, remove, toggle) take no value. .fire() drives them by running the edit's own gesture on the row. One circle can offer a picker (set, to jump to a value) and a cycle (fire, to step to the next) on the same field, beside the native click.

A circle: pick or cycle its category

A dropdown sets the category (set); a button and the circle itself step it (cycle).

// Two ways to change one circle's category, both external. `cycle` is a CLICK edit
// that steps to the next value; a picker SETS a value directly. They coexist on the
// same `fill` field: click the circle (or the ⟳ button) to step, or choose from the
// dropdown to jump straight there.
const chart = Elicit({
width: 300, height: 190,
margins: { top: 10, right: 10, bottom: 10, left: 10 },
schema: {
kind: { type: "categorical", domain: ["low", "med", "high"] },
},
data: [ { kind: "low" } ],
marks: [
point({
size: 26,
channels: {
x: { value: 150 },
y: { value: 90 },
fill: {
field: "kind",
scale: { range: ["#B7DEE1", "#50AEB7", "#50AEB7"] },
// Direct choice — a picker writes exactly this value.
edit: set({ name: "pick" }),
},
},
// Step-at-a-time — a click (on the circle or a button) advances the category.
edits: [ cycle({ name: "step", channel: "fill" }) ],
}),
],
});
mount(chart.dom());
const controls = document.createElement("div");
controls.style.cssText = "display:flex;gap:12px;align-items:center;margin-top:10px;font:13px system-ui";
// A picker → `set`: options are exactly the values the fill scale accepts.
const pick = document.createElement("select");
for (const v of chart.control("pick", 0).accepts().values) {
const o = document.createElement("option");
o.value = v; o.textContent = v; pick.append(o);
}
pick.value = chart.getData()[0].kind;
pick.onchange = () => chart.control("pick", 0).set(pick.value);
const pickWrap = document.createElement("label");
pickWrap.style.cssText = "display:flex;gap:6px;align-items:center";
pickWrap.append("pick", pick);
// A button → `fire`: dispatches the cycle edit's own click gesture, no value.
const step = document.createElement("button");
step.textContent = "⟳ cycle";
step.onclick = () => chart.control("step", 0).fire();
controls.append(pickWrap, step);
mount(controls);
// Keep the picker in step whichever way the category changed.
chart.on("change", () => { pick.value = chart.getData()[0].kind; });
Try: choose from the dropdown, press ⟳, or click the circle — all change the same field.

Sliders for facial features

Each facial parameter is a channel. Bind it to a field with a named set() edit and a slider can write it. The face stays draggable: grab the mouth to pull a smile. Each slider's range comes from control(field).accepts().domain.

Face: parameter sliders

Three sliders drive three facial params; dragging the face keeps them in sync.

// A face is a row of fields; each facial PARAM is a channel on one of the face's
// parts. Bind each to a field with a named `set()` edit and an external slider can
// write it. `.accepts()` reads the range off the very scale the part encodes
// through — including a group's per-datum FRAME scale — so the slider offers
// exactly the values the channel accepts. Add a `slide`/`rotate` alongside and the
// same field is drivable by pointer too: one dataset, two input sources.
const PARAMS = [
{ param: "mouthCurve", field: "smile", label: "smile" },
{ param: "eyeScale", field: "eyes", label: "eyes" },
{ param: "browTilt", field: "brow", label: "brow" },
];
const channels = {};
for (const { param, field } of PARAMS) {
channels[param] = { field, edit: set({ name: field }) };
}
const chart = Elicit({
width: 300, height: 300,
margins: { top: 12, right: 12, bottom: 12, left: 12 },
schema: {
smile: { type: "quantitative", domain: [0, 1] },
eyes: { type: "quantitative", domain: [0, 1] },
brow: { type: "quantitative", domain: [0, 1] },
},
data: [ { smile: 0.7, eyes: 0.5, brow: 0.4 } ],
marks: [ face({ channels }) ],
});
mount(chart.dom());
const controls = document.createElement("div");
controls.style.cssText = "display:flex;flex-direction:column;gap:8px;margin-top:10px;font:12px system-ui;max-width:300px";
const sliders = {};
for (const { field, label } of PARAMS) {
// `.accepts()` tells the UI the field's range (from the scale/schema), so the
// slider offers only values the channel accepts.
const [lo, hi] = chart.control(field).accepts().domain;
const row = document.createElement("label");
row.style.cssText = "display:grid;grid-template-columns:48px 1fr;gap:8px;align-items:center";
const s = document.createElement("input");
s.type = "range"; s.min = lo; s.max = hi; s.step = (hi - lo) / 100;
s.value = chart.getData()[0][field];
s.oninput = () => chart.control(field).set(+s.value);
sliders[field] = s;
row.append(label, s);
controls.append(row);
}
mount(controls);
// Dragging the face directly also fires `change` — keep the sliders in step.
chart.on("change", () => {
const d = chart.getData()[0];
for (const { field } of PARAMS) sliders[field].value = d[field];
});
Try: move a slider, or drag the face directly — both write the same fields.

A rotate icon for a box

rotate reads a pointer angle about the mark. control("spin", 0).set(deg) encodes the angle to the pointer position that would produce it, then runs the ordinary rotate edit. The box also rotates by direct drag.

Rotate a box by an icon

Buttons step the angle in data space; the control encodes each angle for the rotate edit.

// A rotate icon spins a box. `rotate` is a positional edit — it reads a pointer
// angle about the mark. Driving it by a DATA value (degrees) is just the inverse:
// `.set(deg)` forward-encodes the angle to the pointer that would produce it, then
// runs the ordinary edit. The box is also directly rotatable by dragging it.
const chart = Elicit({
width: 300, height: 260,
margins: { top: 20, right: 20, bottom: 20, left: 20 },
schema: {
x: { type: "quantitative", domain: [0, 100] },
y: { type: "quantitative", domain: [0, 100] },
theta: { type: "quantitative", domain: [0, 360] },
},
data: [ { x: 50, y: 50, theta: 20 } ],
marks: [
rect({
channels: {
x: { field: "x" },
y: { field: "y" },
width: { value: 90 },
height: { value: 60 },
fill: { value: "#50AEB7" },
stroke: { value: "#37919B" },
angle: {
field: "theta",
scale: { range: [0, 360] },
edit: rotate({ name: "spin", pick: "direct", pivot: "mark", fold: false }),
},
},
}),
],
});
mount(chart.dom());
const controls = document.createElement("div");
controls.style.cssText = "display:flex;gap:8px;align-items:center;margin-top:10px;font:13px system-ui";
const spinBy = (delta) => {
const now = chart.getData()[0].theta;
const next = ((now + delta) % 360 + 360) % 360;
chart.control("spin", 0).set(next); // forward-encodes deg -> pointer -> rotate
};
const ccw = document.createElement("button");
ccw.textContent = "⟲ −15°";
ccw.onclick = () => spinBy(-15);
const cw = document.createElement("button");
cw.textContent = "⟳ +15°";
cw.onclick = () => spinBy(15);
const readout = document.createElement("span");
readout.style.cssText = "color:#6F8294";
const sync = () => { readout.textContent = Math.round(chart.getData()[0].theta) + "°"; };
chart.on("change", sync);
sync();
controls.append(ccw, cw, readout);
mount(controls);
Try: press the rotate buttons, or drag the box itself.

Sweep and nearest edits

control(name, index) names its row, so it also drives an edit whose pick only chooses a row: nearest and sweep. .set(v) writes row index, whichever row the pointer would have found. An edit that needs a pointer path or position, such as edit.line.draw(), a brush or create(), logs a warning on .set() and writes nothing. Drive those with control(name).emit({ type, x, y }) in plot pixels, which runs that edit alone at the point.

// create({ name: "add", gesture: "commit" }) on a mark: add a row at a plot pixel.
chart.control("add").emit({ type: "commit", x: 120, y: 80 });

Type or sweep a forecast

One move({ pick: "sweep", name: "forecast" }) edit takes both the sweep and the inputs.

// A you-draw-it line takes its answer two ways: sweep across the chart, or type
// a value per year. `move({ pick: "sweep" })` finds the row under the pointer;
// `control("forecast", i)` names the row itself, so `.set(v)` writes year i.
const years = [2024, 2025, 2026, 2027, 2028];
const chart = Elicit({
width: 420, height: 240,
margins: { top: 14, right: 14, bottom: 26, left: 30 },
axes: { x: { tickFormat: "d" } },
schema: {
year: { type: "quantitative", domain: [2024, 2028] },
v: { type: "quantitative", domain: [0, 100] },
},
data: years.map((year) => ({ year, v: 50 })),
marks: [
lineY({
stroke: "#50AEB7", strokeWidth: 2.5,
channels: {
x: { field: "year" },
y: { field: "v", edit: move({ pick: "sweep", name: "forecast" }) },
},
}),
],
});
mount(chart.dom());
const row = document.createElement("div");
row.style.cssText = "display:flex;gap:10px;flex-wrap:wrap;margin-top:10px;font:13px system-ui";
const inputs = years.map((year, i) => {
const label = document.createElement("label");
label.style.cssText = "display:flex;flex-direction:column;gap:2px;color:#6F8294";
label.textContent = year;
const input = document.createElement("input");
input.type = "number"; input.min = 0; input.max = 100; input.style.width = "64px";
input.onchange = () => chart.control("forecast", i).set(Number(input.value));
label.append(input);
row.append(label);
return input;
});
const sync = () => chart.getData().forEach((d, i) => { inputs[i].value = Math.round(d.v); });
chart.on("change", sync);
sync();
mount(row);
Try: Sweep across the chart, or type a value under a year.

One handle of a row

A row can draw several handles: a node's four ports, a link's two ends, an area's two edges. control(name, { index, channel, side }) names one. channel is the channel the handle moves ("port", a link's "source" or "target"), and side is where it sits.

An edit that commits on release, such as edit.network.connect(), takes a whole stroke. begin() presses on the handle, emit({ type: "drag", x, y }) moves the pointer, and end() releases where the pointer last was. cancel() abandons the stroke, so a connect or a rewire writes nothing. begin({ x, y }) presses at a plot pixel of your choosing: use it when the stroke follows a pointer that went down somewhere else, such as on an icon beside the mark, so a relative move travels as far as the pointer does.

// A new link from the right port of node 0, dropped on a plot pixel.
const link = chart.control("link", { index: 0, channel: "port", side: "right" });
link.begin();
link.emit({ type: "drag", x: 300, y: 140 });
link.end(); // or link.cancel()
// Re-point the target end of link 2.
chart.control("rewire", { index: 2, channel: "target" });

A view from chart.scene() carries index, channel and side, so it can be passed as the target.

Several writes, one undo step

chart.transaction() holds one undo entry open and returns the function that closes it. Everything committed in between, by set, a control or a gesture, undoes as one step. change still fires for each write.

// Widen a box by 20 and keep its left edge still: two cells, one step.
const end = chart.transaction();
chart.set("w", row.w + 20, { index: i });
chart.set("x", row.x + 10, { index: i });
end();

Undo, redo and setData close an open transaction.

Reading the chart

UI placed over a chart needs to know where things are. These methods report the drawn chart. Every value is a copy, and pixels are plot pixels: the space inside the margins that an edit's pointer uses.

  • chart.scene(query?) lists the drawn pieces: each one's mark, table, row index, box, center, datum and state (hovered, selected, grabbed, visible). A row usually draws several pieces. query narrows them by mark, table, index, channel or side.
  • chart.hit(point, { reach }) returns the piece at a point, or with reach the nearest within that many pixels.
  • chart.scale(channel, { mark }) returns encode, invert, domain, range and the field behind the channel.
  • chart.edits(query?) lists the declared edits with their name, type and whether each can run now.
  • chart.metrics() gives the chart's size and margins. chart.toInner(point) and chart.toClient(point) convert between the page (a pointer event's clientX, clientY) and plot pixels.
  • chart.on("render", cb) runs after every frame, including each step of an eased motion. Read scene() again there.

A mark's id names it in a query. A glyph's id covers all its parts: "note" matches a sticker's box "note/body", its label and its ports.

The UI layer is built on these methods and on control.

A label that follows a point

An HTML label per dot, re-placed on every render from chart.scene() and chart.toClient().

// A label of your own that follows a point: read the chart, never reach into it.
const chart = Elicit({
width: 420, height: 280,
margins: { top: 20, right: 20, bottom: 40, left: 44 },
schema: {
x: { type: "quantitative", domain: [0, 100] },
y: { type: "quantitative", domain: [0, 100] },
},
data: [{ x: 30, y: 60 }, { x: 68, y: 35 }],
marks: [
point({
id: "dot",
size: 9,
channels: { x: { field: "x", edit: move({ channels: ["x", "y"] }) }, y: { field: "y" } },
}),
],
});
mount(chart.dom());
const labels = chart.getData().map(() => {
const el = document.createElement("div");
el.style.cssText = "position:absolute;left:0;top:0;pointer-events:none;font:12px system-ui;"
+ "padding:2px 6px;border-radius:4px;background:#14283A;color:#fff;white-space:nowrap";
return el;
});
// After every frame: where is each dot now, and what does its row hold?
chart.on("render", () => {
const host = chart.dom().getBoundingClientRect();
for (const view of chart.scene({ mark: "dot" })) {
const el = labels[view.index];
if (!el.isConnected) chart.dom().appendChild(el);
// Plot pixels -> the page -> the chart's own container.
const at = chart.toClient({ x: view.center.x, y: view.box.y });
el.style.transform = `translate(${at.x - host.left}px, ${at.y - host.top}px) translate(-50%, -130%)`;
el.textContent = `${Math.round(view.datum.x)}, ${Math.round(view.datum.y)}`;
}
});
Try: Drag a dot. Its label follows and reads the row.

Driving the selection

The selection is interface state, kept beside the data, so it has its own API and its own select event. chart.select(index) picks a specific row. chart.selectWhere(field, value) picks by category: the first matching row, or every match with { all: true }. Both set the selection as a click with edit.select() does. The selected row gets the highlight outline, and a legend picker edits it. on("select") reports the change. getData and on("change") stay silent, because no answer data changed.

Pass an array to chart.select for several rows at once. See Selection for multi-select, the selected effect and the edits that read the selection.

Select an item, or a category

A dropdown selects a specific bar; a button selects the first bar in a group; the legend edits whichever is selected.

// Selection is pipeline state, so it has its own external API beside the data —
// no `selected` column. `el.select(index)` picks a SPECIFIC ITEM; `el.selectWhere`
// picks by CATEGORY (the first row matching a field/value). Clicking a bar selects
// it too (edit.select), and `on("select")` reports whichever way it moved. The
// legend then edits the SELECTED bar's group.
const chart = Elicit({
width: 360, height: 210,
margins: { top: 16, right: 16, bottom: 28, left: 36 },
data: [
{ name: "A", value: 12, group: "North" },
],
schema: {
name: { type: "categorical", domain: ["A", "B", "C", "D"] },
group: { type: "categorical", domain: ["North", "South"] },
value: { type: "quantitative", domain: [0, 16] },
},
marks: [
barY({
channels: {
x: { field: "name" },
y: { field: "value" },
fill: { field: "group" },
},
edits: [ select() ], // click a bar to select it
}),
legendColor({ anchor: "right", title: "group", edit: edit.legend.category() }),
],
});
mount(chart.dom());
const controls = document.createElement("div");
controls.style.cssText = "display:flex;gap:12px;align-items:center;flex-wrap:wrap;margin-top:10px;font:13px system-ui";
// Select a SPECIFIC ITEM by its name (→ el.selectWhere on the `name` field).
const item = document.createElement("select");
for (const d of chart.getData()) {
const o = document.createElement("option");
o.value = d.name; o.textContent = d.name; item.append(o);
}
item.onchange = () => chart.selectWhere("name", item.value);
const itemWrap = document.createElement("label");
itemWrap.style.cssText = "display:flex;gap:6px;align-items:center";
itemWrap.append("item", item);
// Select a CATEGORY — the first bar in a group (→ el.selectWhere on `group`).
const cat = document.createElement("select");
for (const g of ["North", "South"]) {
const o = document.createElement("option");
o.value = g; o.textContent = g; cat.append(o);
}
const catBtn = document.createElement("button");
catBtn.textContent = "select group";
catBtn.onclick = () => chart.selectWhere("group", cat.value);
const catWrap = document.createElement("label");
catWrap.style.cssText = "display:flex;gap:6px;align-items:center";
catWrap.append("group", cat, catBtn);
// Clear.
const clear = document.createElement("button");
clear.textContent = "clear";
clear.onclick = () => chart.clearSelection();
const readout = document.createElement("span");
readout.style.cssText = "margin-left:auto;color:#6F8294";
controls.append(itemWrap, catWrap, clear, readout);
mount(controls);
// on("select") hears selection changes — never `change`, since no data moved.
chart.on("select", (i) => {
const d = i == null ? null : chart.getData()[i];
readout.textContent = d ? `selected: ${d.name} (${d.group})` : "nothing selected";
if (d) item.value = d.name;
});
readout.textContent = "nothing selected";
Try: pick an item or a group — or click a bar — then use the legend to change its group.

Driving a domain

chart.setDomain(field, domain) writes a field's domain from outside the chart, for a zoom button or a "reset view" control. It commits like edit.view.zoom() and edit.view.pan(): one undo step, a change notification, and every scale updates. Pass null to restore the domain declared in the schema.

chart.setDomain("x", [20, 40]); // zoom to 20–40
chart.setDomain("x", null); // back to the declared domain

See Zoom & pan for buttons that zoom in, zoom out and reset.