Elicit.js

Datasets & linked charts

Elicit.dataset(spec) makes a dataset: the rows, the schema, the constraints and the undo history. ds.chart(spec) draws it, and any number of charts can read and write one dataset: an edit in one chart redraws every other. Elicit(spec) is the one-call form, a dataset and one chart from one spec.

API reference

Elicit.dataset(spec)

A dataset that any number of charts read and write. It holds the rows, the schema, the dataset’s rules, the undo history and the selection. Make a chart over it with ds.chart(spec).

Elicit.dataset({ schema, data?, constraints?, lock?, context?, onChange? }) → Dataset
Options
OptionType · defaultDescription
schema
Schemadefaultrequired
Each field’s type and domain. See Data schema.
data
Datum[] | Record<table, Datum[]>default[]
The seed rows.
constraints
Constraint[]default[]
Rules that gate and repair every edit from every chart, and every set. See Constraints.
lock / context
LockSpec / ContextSpec
Read-only rows and reference rows, in every chart. See Locked rows and Context rows.
onChange
(data) => void
Called once per commit, from any chart, with the committed data.
Returns / emits

A Dataset: chart, getData, getSchema, setData, setDomain, set, undo / redo / canUndo / canRedo, transaction, hover / getHover, on("change" | "select" | "hover") and the selection methods. See Getting started for each.

ds.chart(spec)

A chart over the dataset. It takes every spec key the dataset doesn’t: marks, elements, size, axes, theme, guides, effects, layout, stages. Append chart.dom() to the page.

ds.chart({ marks, width?, height?, … }) → Chart
parent.append(chart.dom())
Returns / emits

A Chart: dom(), dataset, control, emit, on("stage" | "render" | "destroy"), the stage methods, the read methods and destroy(), plus the dataset’s methods.

ds.set(field, value, options?)

Write one cell from outside any chart. Runs the dataset’s constraints, then the lock. One undo step; fires change and redraws every chart. A value outside a closed categorical domain is refused with a warning; one outside a numeric or temporal domain is clamped to it.

ds.set("share", 40)                      // row 0 of the primary table
ds.set("share", "40", { index: 2 })      // stored as the number 40
ds.set("weight", 3, { index: 0, table: "links" })
Options
OptionType · defaultDescription
index
numberdefault0
The row, counted as getData() reports them: context rows left out.
table
stringdefaultthe primary table
The table the row is in.
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.

ds.hover(row) / ds.getHover()

The dataset’s hovered row, which every chart with a linked hover shows. It is interface state: no change event and no undo step. A row is named as the selection names it: a row of the primary table by its index, a row of another table as { table, index }.

ds.hover(2)                           // hover row 2 of the primary table
ds.hover({ table: "links", index: 0 }) // a row of another table
ds.hover(null)                        // clear it
ds.getHover()                         // 2, { table, index }, or null
ds.on("hover", (row) => { … })
Returns / emits

hover returns true when the hovered row changed.

Two charts, one dataset

Split the spec in two. Elicit.dataset takes schema, data, constraints, lock, context and onChange. ds.chart takes everything else: marks, size, axes, theme, guides. A chart is a plain object, so append chart.dom() to the page.

const ds = Elicit.dataset({ schema, data, constraints });
const split = ds.chart({ marks: [barX({ … })] });
const people = ds.chart({ marks: [waffleY({ … })] });
document.body.append(split.dom(), people.dom());

Give each chart the gesture it is best at. Here one bar of 100 patients takes edit.stack.edge(): drag the edge between two outcomes to trade one for the other. A waffle of the same counts takes edit.waffle.fill(): paint a block to say how many people land in it. An edit in either chart commits to the dataset, and both charts redraw from the new rows. maintainSum keeps the total at 100 in both, because it is the dataset's.

Out of 100 patients

The bar trades one outcome for its neighbour. The waffle sets one outcome's count, and maintainSum takes the difference from the others.

// Four outcomes for 100 patients. The counts always sum to 100.
const outcomes = ["Recovers", "Improves", "No change", "Worse"];
const ds = Elicit.dataset({
schema: {
cohort: { type: "categorical", domain: ["100 patients"] },
outcome: { type: "categorical", domain: outcomes },
count: { type: "quantitative", domain: [0, 100] },
},
data: [
{ cohort: "100 patients", outcome: "Recovers", count: 40 },
],
constraints: [
clamp({ min: 0, field: "count" }),
maintainSum({ total: 100, field: "count", strategy: "redistribute" }),
],
});
// One bar of 100. Drag the edge between two outcomes to trade one for the other.
const split = ds.chart({
width: 440, height: 140,
margins: { top: 8, right: 16, bottom: 26, left: 16 },
axes: { y: false },
elements: [legendColor({ anchor: "top" })],
marks: [
barX({
channels: {
y: { field: "cohort" },
x: { field: "count" },
stack: { field: "outcome" },
fill: { field: "outcome" },
},
edits: [edit.stack.edge()],
}),
],
});
// The same counts as people. Paint a block to set how many land in it.
const people = ds.chart({
width: 440, height: 170,
margins: { top: 8, right: 16, bottom: 26, left: 16 },
marks: [
waffleY({
multiple: 10,
channels: {
x: { field: "outcome" },
count: { field: "count", edit: edit.waffle.fill() },
fill: { field: "outcome" },
},
}),
],
});
mount(split.dom());
mount(people.dom());
Try: Drag an edge in the bar, or paint a block of people. The other chart follows.

Three charts, two columns

A chart writes the columns its channels encode. Two line charts each draw one column over year, and move({ pick: "sweep" }) writes it as you sweep. A third chart plots unemployment against inflation, one dot per year, joined by path in year order. move({ channels: ["x", "y"] }) writes both columns of the dot you drag.

Draw the two lines over time and the third chart shows the trade-off they imply. Drag a year in the trade-off and both lines bend at that year.

Unemployment, inflation and the trade-off

The lines write one column each. The scatter writes both.

// Seven years, two columns to forecast.
const ds = Elicit.dataset({
schema: {
year: { type: "quantitative", domain: [2026, 2032] },
unemployment: { type: "quantitative", domain: [3, 8] },
inflation: { type: "quantitative", domain: [1, 6] },
},
data: [
{ year: 2026, unemployment: 7.4, inflation: 1.6 },
],
});
// Sweep across a chart to draw one column over time.
const overTime = (field, title, stroke) => ds.chart({
width: 440, height: 150,
margins: { top: 10, right: 16, bottom: 26, left: 44 },
axes: { x: { tickFormat: "d" }, y: { title } },
marks: [
lineY({
stroke, strokeWidth: 2.5,
channels: { x: { field: "year" }, y: { field, edit: move({ pick: "sweep" }) } },
}),
],
});
// Both columns at once, one dot per year, joined in year order.
// Drag a dot to set that year's unemployment and inflation together.
const tradeOff = ds.chart({
width: 440, height: 260,
margins: { top: 14, right: 16, bottom: 34, left: 44 },
axes: { x: { title: "Unemployment (%)" }, y: { title: "Inflation (%)" } },
marks: [
path({
stroke: "#94A5B3", strokeWidth: 1.5,
channels: { x: { field: "unemployment" }, y: { field: "inflation" } },
}),
point({
fill: "#1B3F60", size: 6,
channels: { x: { field: "unemployment" }, y: { field: "inflation" } },
edits: [move({ channels: ["x", "y"] })],
}),
text({
dx: 10, dy: -8, fontSize: 11, textAnchor: "start", fill: "#46596B", format: "d",
channels: { x: { field: "unemployment" }, y: { field: "inflation" }, text: { field: "year" } },
}),
],
});
mount(overTime("unemployment", "Unemployment (%)", "#50AEB7").dom());
mount(overTime("inflation", "Inflation (%)", "#EE7B5E").dom());
mount(tradeOff.dom());
Try: Sweep across a line chart, or drag a year in the scatter.

One chart per table

A dataset with structure: "network" holds two tables, nodes and links. Each mark draws one table, and table: names it. Here a network chart draws both tables, and a bar chart draws nodes alone. Add a factor in the graph and it gets a bar. Its circle grows with the weight you give it.

create() gives a new node the key "Item 1", "Item 2" and so on, and each new node starts at the default you declare for importance.

A causal map with weights

The graph writes nodes and links. The bars write each node's importance.

// A network: factors (nodes) and what causes what (links).
const ds = Elicit.dataset({
schema: {
structure: "network",
tables: {
nodes: {
id: { type: "categorical", key: true, open: true },
x: { type: "quantitative", domain: [0, 100] },
y: { type: "quantitative", domain: [0, 100] },
importance: { type: "quantitative", domain: [0, 10], default: 5 },
},
links: {
role: "links",
directed: true,
fields: {
source: { type: "ref" },
target: { type: "ref" },
},
},
},
},
data: {
nodes: [
{ id: "Interest rates", x: 16, y: 80, importance: 8 },
],
links: [
{ source: "Interest rates", target: "House prices" },
{ source: "Migration", target: "House prices" },
],
},
});
// Draw the structure. Drag a factor to move it, shift-drag from one to another
// to say it causes it, click empty space to add a factor.
const graph = ds.chart({
width: 440, height: 260,
margins: { top: 24, right: 44, bottom: 24, left: 44 },
axes: false,
marks: [
link({ inset: 3, stroke: "#94A5B3", strokeWidth: 1.5 }),
node({
fill: "#50AEB7",
channels: {
x: { field: "x", edit: move({ channels: ["x", "y"], when: when.noShift }) },
y: { field: "y" },
size: { field: "importance", scale: { range: [4, 16] } },
text: { field: "id" },
},
edits: [
edit.network.connect({ when: when.shift }),
create(),
],
}),
],
});
// Weigh each factor. The bars draw the nodes table.
const weights = ds.chart({
width: 440, height: 200,
margins: { top: 10, right: 16, bottom: 30, left: 100 },
axes: { x: { title: "Importance" } },
marks: [
barX({
table: "nodes",
fill: "#50AEB7",
channels: { y: { field: "id" }, x: { field: "importance", edit: move() } },
}),
],
});
mount(graph.dom());
mount(weights.dom());
Try: Shift-drag from one factor to another to add a cause. Click empty space to add a factor. Drag a bar to weigh it.

A chart that only shows

A chart with no edits is a view. It redraws on every commit like any other chart. Here you set a rent estimate for each Vancouver local area on the bars, and the map shows where your estimates rise and fall. Both charts colour by change on a diverging scale, so a bar and its area share a colour.

Rent change by local area

The bars take move(). The map takes no edits.

// One row per Vancouver local area: its outline and your estimate.
const guess = {
"Kitsilano": -3, "West Point Grey": -4, "Dunbar-Southlands": -3, "Kerrisdale": -5,
"Arbutus Ridge": -4, "Shaughnessy": -6, "Fairview": 1, "West End": 2,
"Downtown": 3, "South Cambie": 0, "Oakridge": -1, "Marpole": 2,
"Riley Park": 3, "Mount Pleasant": 5, "Strathcona": 7, "Grandview-Woodland": 6,
"Kensington-Cedar Cottage": 4, "Sunset": 3, "Victoria-Fraserview": 4,
"Hastings-Sunrise": 6, "Renfrew-Collingwood": 5, "Killarney": 4,
};
const areas = vancouver.features
.map((f) => ({ area: f.properties.name, geometry: f.geometry, change: guess[f.properties.name] ?? 0 }))
.sort((a, b) => a.area.localeCompare(b.area));
const ds = Elicit.dataset({
schema: {
area: { type: "categorical" },
geometry: { type: "categorical" },
change: { type: "quantitative", domain: [-10, 10] },
},
data: areas,
});
const colour = { field: "change", scale: { type: "diverging", scheme: "dddiverging" } };
// Set each estimate here.
const bars = ds.chart({
width: 440, height: 400,
margins: { top: 10, right: 16, bottom: 30, left: 160 },
axes: { x: { title: "Rent change next year (%)" } },
marks: [
barX({
channels: {
y: { field: "area", scale: { reverse: true } },
x: { field: "change", edit: move() },
fill: colour,
},
}),
],
});
// The map has no edits. It recolours on every change.
const map = ds.chart({
width: 440, height: 300,
margins: { top: 8, right: 8, bottom: 8, left: 8 },
projection: { type: "mercator", domain: vancouver, inset: 6 },
marks: [
geoPolygon({
stroke: "#fff", strokeWidth: 1,
channels: { geometry: { field: "geometry", scale: null }, fill: colour },
}),
],
});
mount(map.dom());
mount(bars.dom());
Try: Drag a bar left or right. Its area on the map recolours.

Write from outside a chart

ds.set(field, value, { index, table }) writes one cell from your own code: a slider, an input or a script. index counts the rows getData() reports, from 0, and defaults to 0. table defaults to the primary table. The value is stored as the field's type, so a slider's "40" becomes the number 40.

A value outside the field's declared domain follows one rule, for ds.set and for the set() and editText() edits. A closed categorical or ordinal domain refuses it and logs a warning. A numeric or temporal domain clamps it to its ends, as a drag does. An open domain takes it.

The write runs the dataset's constraints and then the lock, as an edit does. An edit's own constrain doesn't run, because no edit is involved. Each set is one undo step, fires change once and redraws every chart.

set returns true when it commits. It returns false when a constraint rejects the value, the row is locked or the value is unchanged. An undeclared field, an index out of range or a value the field's type can't hold also logs a warning.

set writes rows that exist. To add rows from code, call setData, or replay a create gesture with chart.emit. To drive a named edit through a chart's scales, use chart.control.

A slider beside two charts

The slider calls ds.set("share", value, { index: 0 }). maintainSum repairs the other shares.

// Three shares that always sum to 100. The rule is the dataset's, so it holds
// for a drag in either chart and for the slider below.
const ds = Elicit.dataset({
schema: {
party: { type: "categorical", domain: ["D", "R", "O"] },
share: { type: "quantitative", domain: [0, 100] },
},
data: [
{ party: "D", share: 45 },
],
constraints: [clamp({ min: 0, field: "share" }), maintainSum({ total: 100, strategy: "redistribute" })],
});
const pie = ds.chart({
width: 340, height: 200,
margins: { top: 10, right: 10, bottom: 10, left: 10 },
axes: false,
marks: [
arc({
outerRadius: 85,
channels: { theta: { field: "share" }, fill: { field: "party" } },
}),
],
});
const bars = ds.chart({
width: 340, height: 180,
margins: { top: 10, right: 16, bottom: 28, left: 36 },
marks: [
barY({
channels: {
x: { field: "party" },
y: { field: "share", edit: move() },
fill: { field: "party" },
},
}),
],
});
mount(pie.dom());
mount(bars.dom());
// A plain HTML slider. `ds.set` writes row 0's share; the constraints repair
// the other rows, and both charts redraw.
const label = document.createElement("label");
label.style.cssText = "display:flex;gap:10px;align-items:center;margin-top:10px;font:13px system-ui";
label.textContent = "D's share";
const slider = document.createElement("input");
slider.type = "range"; slider.min = 0; slider.max = 100; slider.step = 1;
slider.oninput = () => ds.set("share", slider.value, { index: 0 });
label.append(slider);
mount(label);
// A drag in either chart moves the slider too.
const sync = () => { slider.value = ds.getData()[0].share; };
ds.on("change", sync);
sync();
Try: Move the slider, or drag a bar. The pie, the bars and the slider stay in step.

One history, one selection

The undo history belongs to the dataset. A gesture in any chart is one step, and ds.undo() steps back the last gesture, whichever chart it happened in. setData clears the history.

Here you click the map to add a place you expect to flood, and its bar appears below. Drag the bar to set the depth, and the place's circle grows on the map. Undo steps back the depth, then the place, in both charts. count({ max: 8 }) caps the number of places.

The selection belongs to the dataset too. Select a row in one chart and every chart outlines it. ds.on("select") fires once per change. See Selection.

Where it floods, and how deep

The map writes lon and lat. The bars write depth. ds.undo() steps back either.

// Up to eight places you expect to flood, and how deep.
const ds = Elicit.dataset({
schema: {
id: { type: "categorical", key: true },
lon: { type: "quantitative", domain: [-123.27, -123.02] },
lat: { type: "quantitative", domain: [49.20, 49.32] },
depth: { type: "quantitative", domain: [0, 3], default: 0.5 },
},
data: [
{ id: "Item 1", lon: -123.115, lat: 49.272, depth: 1.2 },
{ id: "Item 2", lon: -123.130, lat: 49.206, depth: 0.8 },
],
constraints: [count({ max: 8 })],
});
// Click the map to add a place. Drag a pin to move it.
// Its circle grows with the depth you set below.
const map = ds.chart({
width: 440, height: 300,
margins: { top: 8, right: 8, bottom: 8, left: 8 },
projection: { type: "mercator", domain: vancouver, inset: 6 },
marks: [
geoBasemap({ geojson: vancouver, fill: "#EDF7F8", stroke: "#6F8294", strokeWidth: 0.7 }),
geoPoint({
fill: "#2B5D87", fillOpacity: 0.7, stroke: "#fff", strokeWidth: 1.5,
channels: {
lon: { field: "lon" },
lat: { field: "lat" },
size: { field: "depth", scale: { range: [3, 18] } },
},
edits: [edit.geo.move(), edit.geo.create()],
}),
geoText({
dy: -14, fontSize: 11, fill: "#20555A",
channels: { lon: { field: "lon" }, lat: { field: "lat" }, text: { field: "id" } },
}),
],
});
// Each place gets a bar as soon as it exists. Drag it to set the depth.
const depths = ds.chart({
width: 440, height: 200,
margins: { top: 10, right: 16, bottom: 30, left: 56 },
axes: { x: { title: "Flood depth (m)" } },
marks: [
barX({
fill: "#2B5D87",
channels: { y: { field: "id" }, x: { field: "depth", edit: move() } },
}),
],
});
mount(map.dom());
mount(depths.dom());
// One history for both charts.
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 = () => ds.undo();
redo.onclick = () => ds.redo();
const sync = () => {
undo.disabled = !ds.canUndo();
redo.disabled = !ds.canRedo();
};
ds.on("change", sync);
sync();
controls.append(undo, redo);
mount(controls);
Try: Click the map to add a place, drag its bar, then press Undo twice.

Linked hover

Charts on one dataset link their hover. Point at a row in one chart and every chart outlines that row and fades the others. A chart with no edits takes part: every mark that draws rows answers the pointer. Hover writes no data and adds no undo step.

effects.hovered.link sets a chart's part in it. true sends the chart's hover and shows the others'. "receive" shows the others' hover and sends none. false keeps hover inside the chart. The default is true when two or more charts draw the dataset.

ds.chart({ marks, effects: { hovered: { link: "receive" } } });

effects.hovered.others styles the faded rows, and others: false keeps the outline alone. See Highlight and mute.

ds.getHover() returns the hovered row, and ds.on("hover", cb) fires when it changes. A row of the primary table is its index; a row of another table is { table, index }, as in the selection. ds.hover(row) sets it from outside a chart, such as a table row beside the charts, in either form, and ds.hover(null) clears it.

For a highlight that stays, use the selection: edit.select() on the mark and effects: { selected: { others: { opacity: 0.25 } } }.

Rent and income, by district

Two charts, no effects in either spec. The bars are draggable and the dots are view-only.

// Six districts: what people pay, and what they earn.
const ds = Elicit.dataset({
schema: {
district: { type: "categorical", domain: ["Ash", "Birch", "Cedar", "Dale", "Elm", "Fern"] },
rent: { type: "quantitative", domain: [0, 3000] },
income: { type: "quantitative", domain: [0, 100] },
},
data: [
{ district: "Ash", rent: 2400, income: 82 },
],
});
// Neither chart sets `effects`. Two charts on one dataset link their hover.
const bars = ds.chart({
width: 440, height: 200,
margins: { top: 10, right: 16, bottom: 30, left: 44 },
axes: { y: { title: "Rent" } },
marks: [
bar({
fill: "#50AEB7",
channels: { x: { field: "district" }, y: { field: "rent", edit: move() } },
}),
],
});
// A view-only chart takes part too.
const scatter = ds.chart({
width: 440, height: 220,
margins: { top: 10, right: 16, bottom: 36, left: 44 },
axes: { x: { title: "Income" }, y: { title: "Rent" } },
marks: [
point({
fill: "#F4A259", size: 7,
channels: { x: { field: "income" }, y: { field: "rent" } },
}),
],
});
mount(bars.dom());
mount(scatter.dom());
Try: Hover a bar or a dot. The same district lights up in the other chart.

What each chart keeps

Every chart on a dataset shares:

  • the rows, the schema and its domains, so a zoom in one chart zooms every chart;
  • constraints, lock and context;
  • the undo history, the selection and the linked hover;
  • onChange and on("change"), which fire once per commit, whichever chart made it.

Each chart keeps its own marks, size, theme, guides, effects and stages. chart.on("stage") and the stage methods are the chart's own. chart.control and chart.emit replay a gesture on that chart, through its edits and its scales.

A chart also carries the dataset's methods, so chart.getData() and chart.undo() work on a chart from Elicit(spec). chart.dataset is the dataset itself. A dataset key passed to ds.chart(), such as constraints, is ignored with a warning that points to Elicit.dataset.

chart.destroy() detaches one chart. The other charts keep working, and the dataset keeps its rows and history.

A chart draws the dataset's rows as they are. A view shows the same rows another way. To show a summary of them, such as a histogram of another chart's points, compute it in a change listener and give it a dataset of its own.

A hierarchy fits its node boxes in one chart's pixels: the chart where the last gesture happened, or, for seed data, setData and set, the first chart that draws the boxes. Other charts draw the stored boxes as they are.