Elicit.js

Selection

The selection is the set of rows currently picked. It is interface state, kept beside the data. edit.select() makes a mark selectable, and selected rows get the selected effect. An edit that needs a row to write to, such as a legend picker or an external control, reads the selection. The answer data is unchanged.

API reference

edit.select

Makes a mark selectable. Selection is transient pipeline state — the chart remembers which rows are picked, and nothing is written to the data. That is the point: it replaces a selected column, so no change fires, nothing lands in undo, and the elicited dataset stays clean.

edit.select({ multi, exclusive, toggle }) → Edit
Options
OptionType · defaultDescription
multi
booleandefaultfalse
Shift- or meta-click adds to the selection; a plain click still replaces it. Shift-clicking a selected row removes it. Turning this on changes nothing about the gesture you already had.
exclusive
booleandefaulttrue
Selecting a row clears the rest. false makes every click additive, with no modifier — the flat override for a chart that is always multi.
toggle
booleandefaulttrue
Clicking an already-selected row deselects it. false never deselects.

Selection API

Selection has its own small API beside getData/setData, and its own select event. Every route — a mark click, an external control, the keyboard — goes through the same commit, so the selection is always in one shape. A row of the primary table is its index; a row of another table is { table, index }. Indices count the rows getData() reports.

chart.getSelection() → row | null         ·   chart.getSelectionAll() → row[]
chart.select(row | row[] | null) → boolean      // row: index | { table, index }
chart.toggleSelection(row) → boolean      ·   chart.selectAll() → boolean
chart.selectWhere(field, value, { all }) → boolean
chart.clearSelection() → boolean          ·   chart.on('select', (first, all) => …)
Options
OptionType · defaultDescription
getSelection / getSelectionAll
() => row | null / row[]
The primary selection, and every selected row. getSelectionAll lists the primary table's indices in ascending order, then each other table's rows as { table, index }. getSelection is the first of them: the lowest selected index. It stays scalar when several are selected. A row that was removed drops out.
select
(row | row[] | null)
Replace the selection. An array selects exactly those rows in one commit; null, an empty array, or an out-of-range index clears. A table the dataset lacks warns.
toggleSelection / selectAll
(row) / ()
Add or remove one row, leaving the rest alone (the external counterpart of a shift-click); or take every row.
selectWhere
(field | predicate, value?, { all }?)defaultall: false
Select by category or predicate. The default takes the first match; { all: true } takes every one, which is what selecting a category usually means.
clearSelection
()
Empty the selection. The same commit path, so the select event fires.
on('select')
(first, all) => void
Fires whenever the selection moves, with getSelection() and getSelectionAll(). No data changed, so this is not a change event.

State, not data

The selection stays out of getData(), fires no change and takes no undo step. It survives edits and resets when you call setData or advance a stage. It reports through its own select event.

edit.select() is a direct-pick click. A plain click selects one row and clears the rest. Clicking the selected row again deselects it.

Click a bar to select it

The selected bar takes the highlight outline. getData() gains no column.

const chart = Elicit({
width: 380, height: 210,
margins: { top: 20, right: 16, bottom: 30, left: 40 },
data: [
{ name: "A", value: 12 },
],
schema: {
name: { type: "categorical", domain: ["A", "B", "C", "D"] },
value: { type: "quantitative", domain: [0, 16] },
},
marks: [
barY({
fill: "#50AEB7",
channels: { x: { field: "name" }, y: { field: "value", edit: move() } },
edits: [ edit.select() ]
}),
],
});
mount(chart.dom());
Try: Click a bar to select it, click it again to deselect · drag the top to change its value.

Seeing the selection

Selected rows get the selected effect. By default it traces each mark's edge in a deep shade of its own colour. It applies to every mark over that row, so selecting a bar also outlines its label. Restyle it on the spec:

effects: { selected: { outline: { color: "#2563eb", width: 3 } } }

selected outranks hovered, and grabbed outranks selected, so a row you are dragging shows as grabbed.

Selecting several rows

edit.select({ multi: true }) makes shift-click (or meta-click) add to the selection. Shift-clicking a selected row removes it. A plain click still replaces the whole selection. Use exclusive: false to make every click add, with no modifier.

An edit that writes the selected row writes all selected rows, so one swatch click recategorises the whole selection.

Shift-click to build a selection

One swatch click sets the group on every selected bar.

const chart = Elicit({
width: 380, height: 210,
margins: { top: 20, right: 16, bottom: 30, left: 40 },
data: [
{ name: "A", value: 12, group: "North" },
],
schema: {
name: { type: "categorical", domain: ["A", "B", "C", "D"] },
group: { type: "categorical", domain: ["North", "South", "East", "West"] },
value: { type: "quantitative", domain: [0, 16] },
},
marks: [
barY({
channels: {
x: { field: "name" },
y: { field: "value" },
fill: { field: "group", edit: edit.legend.category() },
},
edits: [ edit.select({ multi: true }) ]
}),
],
});
mount(chart.dom());
Try: Click a bar, shift-click another, then click a legend swatch.

Driving it from outside

An external call and a click set the selection the same way. The selection belongs to the dataset, so charts that share a dataset show one selection. See External controls for the related API.

chart.select(2); // replace with row 2
chart.select([0, 2]); // replace with rows 0 and 2
chart.toggleSelection(3); // add or remove row 3
chart.selectAll();
chart.selectWhere("group", "North", { all: true }); // a whole category
chart.clearSelection();
chart.getSelection(); // 0 (the lowest, or null)
chart.getSelectionAll(); // [0, 2]
chart.on("select", (first, all) => …);

A row of the primary table is its index. On a network or hierarchy, a row of another table is { table, index }: chart.select({ table: "links", index: 1 }), and getSelectionAll() returns [0, 2, { table: "links", index: 1 }].

selectWhere takes the first match by default. Pass { all: true } for every row of a category, which is usually what a control that names a value wants.

Edits that read the selection

A gesture that carries no row of its own reads the selection. A legend picker is the main case: a swatch names a value, so its row defaults to the selection.

The swatch that matches the selection is ringed. With nothing selected and more than one row, a picker has no target and no ring. In a one-row table, the picker writes that row. If nothing in the chart can select, a warning says the picker has no target.