Elicit.js

Adding, removing & locking rows

These edits add and remove rows. create() inverts a click into a new row, and remove() deletes the clicked row. lock on the spec makes given rows read-only: the record so far, points already measured, last quarter’s actuals. No gesture can change or delete a locked row, and rows an edit adds stay editable. Locked rows are still part of getData(); to leave given rows out of the answer, mark them as context.

API reference

create(options)

A plane gesture that mints a datum — inverts pointer pixels through positional channels, populating defaults for the rest.

create({ channels, defaults, gesture }) → Edit
Options
OptionType · defaultDescription
channels
string[]default['x','y','xOffset','yOffset']
Positional channels to place from the pointer. The mark's own channels decide which of them apply.
defaults
objectdefault{}
Seed values for non-positional fields on newly minted rows.
gesture
'click' | 'dblclick'default'click'
The plane gesture that triggers creation.

remove(options)

Deletes the targeted datum from the dataset.

remove({ pick, threshold, when, gesture }) → Edit
Options
OptionType · defaultDescription
when
(ctx) => boolean
Arbitration predicate (e.g. when.alt so Alt-click deletes while click recolours).
gesture
stringdefault'click'
The gesture that removes the row.

lock

Which rows of the dataset are read-only. A lock is a property of the data — not of a mark or an edit — so it lives on the dataset spec beside data and schema, and holds for every edit from every mark in every chart.

lock: "seed" | true | ((datum, index) => boolean)
Options
OptionTypeDescription
"seed" / true
string | boolean
The rows the chart was seeded with (its data) are fixed; anything an edit creates is free. setData re-seeds the chart, so it also re-takes the lock.
(datum, index) => boolean
function
Lock rows by what they are — d => d.kind === "actual", d => d.year <= 1990. Re-evaluated every render, so a row can be locked by a field an edit writes.
Returns / emits

Two things follow, and both are automatic. Data: a dataset invariant runs on every commit, last, so a lock outranks every other repair — a proposal that touched a locked row keeps its changes to the free rows and snaps the locked ones back (deleting one is rejected outright). Pointer: a locked row’s marks are not grabbable, show no editable cursor, and are skipped by proximity picking — so nearest / sweep / draw never even target one.

context

Marks seeded rows as reference: drawn and read-only, and left out of getData(), onChange, and the selection and control indices.

context: true | ((datum, index, table) => boolean)
Options
OptionTypeDescription
context: true
true
Every row in data is context; rows you add are your answer.
context: (d) => boolean
Function
The seeded rows the predicate accepts are context. It runs once, over the rows in data.

What can — and cannot — be created

Creation writes dataset rows, so it goes on a data mark with an invertible positional axis. Elements and guides write no rows. trend draws one line from a row's intercept and slope, so it has no position to invert.

MarkCreate?How
point · shape✓Invert pointer through x and y
tick · bar✓One axis inverts to a band slot, the other to value
rule✓Invert pointer on its value axis
rect✓Seed default extents (x1/x2 or y1/y2)
composite (glyph)✓Create on one component mark; row renders across all
line · path✓Series-aware anchor and draw edits
Elements: axisX · gridY · legendColor✗Edit the scale's domain, not rows
Guides: guides.*✗Write nothing
trend✗A parametric line drawn from one row; no position to invert

Create — click to add

create() inverts a click on empty space into a new row and appends it. A constraint such as count({ max }) caps the number of rows.

Declare a creator on one mark only. Each mark on the table that carries one appends a row on the same click.

Click to add a point

create({ defaults }) mints a datum at the pointer; count({ max: 8 }) caps the dataset.

const chart = Elicit({
width: 380, height: 260,
margins: { top: 14, right: 14, bottom: 26, left: 30 },
data: [{ x: 30, y: 30, group: "a" }],
schema: {
// Declared so create() mints it on a new row — an undeclared column would be
// missing from every point the click adds.
group: { type: "categorical" },
x: { type: "quantitative", domain: [0, 100] },
y: { type: "quantitative", domain: [0, 100] },
},
constraints: [ count({ max: 8 }) ], // dataset invariant: at most 8
marks: [
point({
fill: "#1B3F60",
size: 8,
channels: {
x: { field: "x", edit: move() },
y: { field: "y", edit: move() },
},
edits: [ create({ defaults: { group: "a" } }) ],
}),
],
});
mount(chart.dom());
Try: Click empty space to add a point · drag to move it.

Create, move & remove arbitration

remove() fires on a plain click. When several edits share a mark, split their gestures with when: for example, double-click to add, drag to move and Alt+click to remove.

Create, move, and remove on one mark

Double-click adds, Alt-click deletes, and drag moves.

const chart = Elicit({
width: 380, height: 260,
margins: { top: 14, right: 14, bottom: 26, left: 30 },
data: [{ x: 30, y: 40 }, { x: 65, y: 65 }],
schema: {
s: { type: "categorical" }, // the series key; declared so create() mints it
x: { type: "quantitative", domain: [0, 100] },
y: { type: "quantitative", domain: [0, 100] },
},
marks: [
point({
fill: "#1B3F60",
size: 8,
channels: {
x: { field: "x" },
y: { field: "y" },
},
edits: [
move({ channels: ["x", "y"] }),
create({ gesture: "dblclick" }),
remove({ when: when.alt }),
],
}),
],
});
mount(chart.dom());
Try: Double-click to add · Alt+click to delete · Drag to move.

Arbitration between click actions

when.noAlt cycles color; when.alt removes the row.

const chart = Elicit({
width: 380, height: 260,
margins: { top: 14, right: 14, bottom: 26, left: 30 },
data: [
{ x: 25, y: 40, group: "alpha" },
],
schema: {
s: { type: "categorical" }, // the series key; declared so create() mints it
x: { type: "quantitative", domain: [0, 100] },
y: { type: "quantitative", domain: [0, 100] },
group: { type: "categorical", domain: ["alpha", "beta", "gamma"] },
},
marks: [
point({
size: 10,
channels: {
x: { field: "x", edit: move() },
y: { field: "y", edit: move() },
fill: { field: "group", edit: cycle({ when: when.noAlt }) },
},
edits: [ remove({ when: when.alt }) ],
}),
],
});
mount(chart.dom());
Try: Click a dot to recolour · Alt+click to delete.

Locked seed, free additions

lock: "seed" locks exactly the rows you passed as data. Rows a create mints afterwards are ordinary, editable data. One dataset can hold the observed points and your answer.

Scatter: observed points + yours

The five grey points are the seed — try to drag one and nothing happens. Click to add your own, and those you can move and delete.

const chart = Elicit({
width: 400, height: 300,
margins: { top: 16, right: 16, bottom: 32, left: 40 },
schema: {
x: { type: "quantitative", domain: [0, 10] },
y: { type: "quantitative", domain: [0, 10] },
// A schema `default` populates a field on a row an edit MINTS — so every
// point you add is tagged "yours" without create() saying a word about it.
source: { type: "categorical", domain: ["observed", "yours"],
default: "yours" },
},
data: [
{ x: 1.2, y: 2.4, source: "observed" },
],
// The observed rows are given, not elicited.
lock: "seed",
axes: { x: { grid: true }, y: { grid: true } },
marks: [
point({
size: 7,
channels: {
x: { field: "x" },
y: { field: "y" },
fill: { field: "source",
scale: { range: ["#94A5B3", "#50AEB7"] } },
},
edits: [
create({ when: when.noAlt }), // click empty space -> add
move({ channels: ["x", "y"] }), // drag one of yours in 2D
remove({ when: when.alt }), // alt-click -> gone
],
}),
],
});
mount(chart.dom());
Try: Click empty space to add a point · drag one of yours · Alt-click one of yours to remove it. The grey points never move.

Locking rows by what they are

Pass a predicate to lock rows by their fields. Here the quarters that already happened are actuals, and the rest are your forecast. Both live in one dataset and one bar mark.

Actuals + forecast

lock: (d) => d.kind === "actual". The two grey bars are reported; only the forecast bars take a drag.

const chart = Elicit({
width: 400, height: 260,
margins: { top: 16, right: 16, bottom: 30, left: 44 },
schema: {
q: { type: "categorical", domain: ["Q1", "Q2", "Q3", "Q4"] },
n: { type: "quantitative", domain: [0, 100] },
kind: { type: "categorical", domain: ["actual", "forecast"] },
},
data: [
{ q: "Q1", n: 42, kind: "actual" },
],
// The lock is a property of the row, so it reads straight off the data.
lock: (d) => d.kind === "actual",
marks: [
barY({
channels: {
x: { field: "q" },
y: { field: "n", edit: move({ guide: true }) },
fill: { field: "kind",
scale: { range: ["#94A5B3", "#50AEB7"] } },
},
}),
],
});
mount(chart.dom());
Try: Drag Q3 / Q4 (blue). Q1 and Q2 are reported — they don’t budge.

Context rows

A locked row is still part of your answer: getData() returns it. When the given rows are only for reference, mark them as context instead. context: true makes every row in data context. A predicate picks the rows it accepts (context: (d) => d.year <= 2020).

Context rows belong to the same table, so they share its columns and schema. Every mark over the table draws them, and they are read-only, like locked rows. getData() and onChange leave them out, so a one-table chart still reports a bare array of your answers. The selection API and control() count reported rows only, so getData()[i] and select(i) name the same row.

The predicate runs once, over the rows in data. A row you add or drag later stays part of your answer, even when the predicate would accept it. setData(rows) replaces your answers and keeps the context rows. Constraints see context rows too, because they are rows of the table. count leaves them out: its max is the number of answer rows.

When the reference rows carry other columns (an observed price beside your lo and hi), declare all of them in the schema. Each row fills its own columns, and a mark skips a row whose value is missing. Reference data unrelated to the answer is a guide.

The record as context, the forecast as your answer

2010–2020 is context: drawn and read-only. getData() returns only the four forecast years.

const chart = Elicit({
width: 440, height: 260,
margins: { top: 16, right: 16, bottom: 32, left: 40 },
axes: { x: { tickFormat: "d" } },
schema: {
year: { type: "quantitative", domain: [2010, 2030] },
rate: { type: "quantitative", domain: [0, 10] },
},
// 2010–2020 is the record; 2022–2030 is your forecast.
data: [
{ year: 2010, rate: 4.2 }, { year: 2012, rate: 4.8 }, { year: 2014, rate: 5.1 },
],
// The record is context: drawn and read-only, but not part of your answer.
context: (d) => d.year <= 2020,
guides: [guides.region({ x: [2010, 2020], fill: "#94A5B3", opacity: 0.12 })],
marks: [
lineY({
stroke: "#50AEB7", strokeWidth: 2,
channels: {
x: { field: "year" },
y: { field: "rate", edit: move() },
},
}),
],
});
mount(chart.dom());
Try: Drag a forecast point (2022–2030). The record does not move.

You draw it

This recreates the New York Times’ “You Draw It” (2017): the record runs to 1990 and you draw the rest. Both halves use the same dataset, line mark and y field; only the lock differs. Proximity picks skip locked rows, so a drag beside the record starts drawing instead of grabbing it.

edit.line.draw({ stage: 0 }) draws the missing years in stage 0. chart.nextStage() moves to stage 1, which retires the edit and shows the answer key.

Draw the line for the missing years

edit.line.draw({ samples: years }) paints one point per year as the pointer crosses its column. Drag back over the record and it will not budge — the lock repairs those rows and keeps the rest of the stroke.

// U.S. motor-vehicle deaths per year (NHTSA/FARS, rounded).
// 1968-1990 is the record; the rest is what you believe happened.
const record = [
[1968, 52725], [1969, 53543], [1970, 52627], [1971, 52542],
[1972, 54589], [1973, 54052], [1974, 45196], [1975, 44525],
[1976, 45523], [1977, 47878], [1978, 50331], [1979, 51093],
[1980, 51091], [1981, 49301], [1982, 43945], [1983, 42589],
[1984, 44257], [1985, 43825], [1986, 46087], [1987, 46390],
[1988, 47087], [1989, 45582], [1990, 44599],
].map(([year, deaths]) => ({ year, deaths }));
const truth = [
[1991, 41508], [1992, 39250], [1993, 40150], [1994, 40716],
[1995, 41817], [1996, 42065], [1997, 42013], [1998, 41501],
[1999, 41717], [2000, 41945], [2001, 42196], [2002, 43005],
[2003, 42884], [2004, 42836], [2005, 43510], [2006, 42708],
[2007, 41259], [2008, 37423], [2009, 33883], [2010, 32999],
[2011, 32479], [2012, 33782], [2013, 32893], [2014, 32744],
[2015, 35485], [2016, 37806],
].map(([year, deaths]) => ({ year, deaths }));
const chart = Elicit({
width: 560, height: 340,
margins: { top: 20, right: 24, bottom: 32, left: 56 },
schema: {
year: { type: "quantitative", domain: [1968, 2016] },
deaths: { type: "quantitative", domain: [0, 60000] },
},
data: record,
lock: "seed", // the record is fact: undraggable, unsweepable, unpickable
axes: {
x: { tickFormat: "d", grid: true },
y: { tickFormat: "~s", grid: true },
},
// Stage 0 = you draw. Stage 1 = the answer is on screen, drawing is over.
stageLabels: ["draw", "revealed"],
guides: [
// The years you were given (the NYT's grey box).
guides.region({ x: [1968, 1990], fill: "#94A5B3", opacity: 0.12 }),
// The endpoint readouts.
guides.custom(({ data, scales }) => [1968, 1990]
.map((y) => data.find((d) => d.year === y))
.filter(Boolean)
.map((d) => ({
type: "text", text: d3.format(",")(d.deaths),
x: scales.x.encode(d.year), y: scales.y.encode(d.deaths) - 12,
fontSize: 12, textAnchor: "middle", fill: "#20555A",
}))),
// The answer key. Not data — it is not elicited, so it is a guide, and it
// stays out of getData(). It appears when the chart advances to stage 1.
guides.custom(({ scales, stage }) => stage < 1 ? [] : [{
type: "path", fill: "none", stroke: "#F2B94B", strokeWidth: 2.5,
points: truth.map((d) => [scales.x.encode(d.year),
scales.y.encode(d.deaths)]),
}]),
],
marks: [
lineY({
stroke: "#50AEB7", strokeWidth: 3, curve: "catmullRom", handleSize: 3,
channels: {
x: { field: "year" },
y: { field: "deaths" },
},
// One drag per year column. `stage: 0` retires the edit once you've asked
// to see the answer.
edits: [ edit.line.draw({ samples: d3.range(1991, 2017), stage: 0 }) ],
}),
],
});
mount(chart.dom());
const btn = mount(document.createElement("button"));
btn.textContent = "Show me how I did.";
btn.onclick = () => chart.nextStage();
Try: Drag left-to-right across the empty half to draw 1991–2016, then drag again to refine it. Then press Show me how I did.