Elicit.js

Legend

A legend is a key for one encoding: a field on a non-positional channel such as fill, size or symbol. Declare it on the channel (fill: { field, legend: true }), or compose legendColor() / legendSize(). It reserves space on its side, so the plot shrinks and the legend never overlaps the marks. With an edit it becomes a picker: click a swatch to set a category, or drag a ramp handle to set a value.

API reference

ChannelSpec.legend · legendColor · legendSize · legendSymbol

A key for one encoding — a (channel, field) pair. Ask for it on the channel (fill: { field, legend: true }), chart-wide with legends: true, or by composing an element. The shape follows the channel: swatches, a colour ramp, graduated circles for size, a fan for theta, a weight stack for strokeWidth. It reserves space on its side, shrinking the plot so it never overlaps the marks. Import elements from elicitjs.

fill: { field, legend: true | { anchor, title, edit, … } | false }
legends: true   ·   legends: { fill: { anchor: 'top' }, size: false }
legendColor({ field, anchor, … }) → ChartElement   ·   legendSize · legendSymbol
Options
OptionType · defaultDescription
channel
'fill'|'stroke'|'size'|'symbol'|'opacity'|'theta'|'strokeWidth'|'strokeDash'default'fill'
Which channel’s scale to draw. legendSize and legendSymbol pin it; legendColor takes 'fill' (default) or 'stroke' and warns on any other. Implied when you declare the legend on the channel. Any other channel warns — an axis is the key for x/y.
anchor
'right'|'left'|'top'|'bottom'default'right'
Which side to sit on. Left/right stack vertically; top/bottom lay out in a row.
orientation
'vertical' | 'horizontal'defaultfrom anchor
Override the swatch/ramp direction independently of the side.
edit / edits
Edit | Edit[]
Opt-in interactivity: edit.legend.category() (click a swatch to set a category) or edit.legend.value() (drag the ramp handle to set a value).
field
stringdefaultthe channel's field
WHICH ENCODING this legend is for. Implied when declared on a channel. On a hand-composed element it is only needed when the channel carries more than one encoding — with two and no field, the first is used and a warning names both.
table
stringdefaultthe primary table
Which table that column is in, by name. Only meaningful on a multi-table schema.
row
number | (data, { selection }) => numberdefaultselection ?? (single row ? 0 : none)
Which dataset row the picker writes into. Left unset it tracks the chart's selection (edit.select / chart.select), falling back to the sole row of a one-row belief. A number pins a fixed row; a function computes one.
title
string | falsedefaultthe encoded field's name
A caption above the legend. Defaults to the field it is a key for; false removes it.
swatchSize / gap / labelWidth
numberdefault14 / 6 / auto
Discrete swatch geometry. labelWidth defaults to a per-label estimate.
rampLength / rampThickness / ticks
numberdefault140 / 12 / 4
Continuous ramp geometry and its approximate tick count.
tickFormat
string | fndefaultauto
Ramp tick labels: a d3-format string, or a formatter function.
stroke / fill / fontSize
string / numberdefaulttheme
Chrome, defaulting to the theme’s legend/axis tokens (resolved at build time).
handleColor / handleSize
string / numberdefaulttheme / 5
The ramp handle edit.legend.value() drags. A legend with no edit draws none.
id
string
Stable identity, as on every mark.

edit.legend.category · edit.legend.value

The legend pickers, written back through the normal edit pipeline. edit.legend.category() is a category picker (a direct-pick click on a swatch, whose value it sets). edit.legend.value() is a continuous value picker (a direct-pick drag on the ramp handle). Pass one to a legend’s edit option — the legend injects its channel.

edit.legend.category() → Edit   (click a swatch; writes node.category)
edit.legend.value() → Edit      (drag the handle; maps to [lo,hi])
Options
OptionTypeDescription
—
EditOptions
Neither picker names a column: a legend is a key for one encoding, so the column is the one that encoding uses. They take the universal edit options (name, stage, when, constrain).

The legends key

An encoding is a (channel, field) pair. A scale belongs to an encoding, and a legend belongs to a scale.

There are three ways to ask for one, most specific first:

  1. an element you compose — legendColor({ field: "group" })
  2. the channel — fill: { field: "group", legend: { anchor: "bottom" } }
  3. the spec — legends: true

Each wins over the ones below it. legends: { fill: false } suppresses a channel; legend: false on a channel suppresses that one encoding.

legends defaults to off, unlike axes. A legend reserves layout space, so adding one shrinks the plot.

Two marks stating the same encoding share one scale and get one legend. Two marks with different fields on one channel are two encodings. They get two scales and two keys, in different colour families.

legends: true

One key per encoding, with no legend composed by hand.

const chart = Elicit({
width: 420, height: 240,
margins: { top: 16, right: 16, bottom: 30, left: 34 },
data: [
{ cat: "A", n: 40, team: "north" },
],
schema: {
cat: { type: "categorical", domain: ["A", "B", "C"] },
n: { type: "quantitative", domain: [0, 100] },
team: { type: "categorical", domain: ["north", "south"] },
},
// One legend per ENCODING — per (channel, field) pair, not per channel.
// For per-channel config: `legends: { fill: { anchor: "top" }, size: false }`.
legends: true,
marks: [
bar({
channels: {
x: { field: "cat" },
y: { field: "n", edit: move() },
fill: { field: "team" },
},
}),
],
});
mount(chart.dom());

A discrete colour legend

legendColor() draws a swatch per category. Here it names no field: fill carries one encoding, so the legend binds to that scale. anchor sets which side it's on. The plot shrinks to leave room, so the legend never overlaps the marks.

The title defaults to the encoded field's name.

Swatches, reserving space

A categorical fill; the legend claims the right band and the bars shrink to fit.

const chart = Elicit({
width: 380, height: 220,
margins: { top: 20, right: 16, bottom: 30, left: 40 },
data: [
{ group: "North", value: 12 },
],
schema: {
group: { type: "categorical", domain: ["North", "South", "East", "West"] },
value: { type: "quantitative", domain: [0, 16] },
},
marks: [
barY({
channels: {
x: { field: "group" },
y: { field: "value" },
fill: { field: "group" },
},
}),
// A legend for the fill channel, reserving space on the right so the bars
// shrink to make room instead of drawing under it.
legendColor({ anchor: "right" }),
],
});
mount(chart.dom());

A continuous colour ramp

A quantitative fill resolves to a sequential colour scale, so the same legendColor() draws a gradient ramp with ticks.

Gradient ramp

fill: { field: 'temp' } over a quantitative field becomes a ramp.

const chart = Elicit({
width: 380, height: 240,
margins: { top: 20, right: 16, bottom: 32, left: 44 },
data: [
{ hour: 0, temp: 4 }, { hour: 3, temp: 6 }, { hour: 6, temp: 9 },
],
schema: {
hour: { type: "quantitative", domain: [0, 24] },
temp: { type: "quantitative", domain: [0, 30] },
},
marks: [
point({
size: 8,
channels: {
x: { field: "hour" },
y: { field: "temp" },
// A quantitative fill resolves to a SEQUENTIAL colour scale, so the
// legend draws a gradient ramp with ticks instead of swatches.
fill: { field: "temp" },
},
}),
legendColor({ anchor: "right" }),
],
});
mount(chart.dom());

A size legend

legendSize() over a quantitative field draws graduated circles at tick values.

The radii come from the channel's scale. size defaults to a linear radius, so the same pointer movement makes the same change in value anywhere on the scale. Pass scale: "sqrt" on the channel for linear area instead, the convention for a static read. The legend follows either way.

size: { field: "people", scale: "sqrt" }

A categorical size field gets swatches instead — one dot per category, at that category's radius.

Graduated circles

Nested circles at tick values, sized by the same scale as the dots.

const rows = [
{ city: "A", x: 20, y: 70, people: 120 },
{ city: "B", x: 55, y: 40, people: 640 },
{ city: "C", x: 85, y: 76, people: 980 },
{ city: "D", x: 38, y: 22, people: 320 },
];
const chart = Elicit({
width: 400, height: 230,
margins: { top: 20, right: 20, bottom: 24, left: 24 },
axes: false,
data: rows,
schema: {
city: { type: "categorical", domain: ["A", "B", "C", "D"] },
x: { type: "quantitative", domain: [0, 100] },
y: { type: "quantitative", domain: [0, 100] },
people: { type: "quantitative", domain: [0, 1000] },
},
marks: [
point({ fill: "#50AEB7", fillOpacity: 0.5, channels: {
x: { field: "x" }, y: { field: "y" }, size: { field: "people" },
} }),
legendSize({ anchor: "right" }),
],
});
mount(chart.dom());

Theta and stroke width

Every legendable channel gets a key shaped like the channel. theta draws a fan of spokes at their encoded bearings. strokeWidth draws a stack of segments at their encoded thicknesses. Both read the scale, so the key matches the marks.

Legendable channels are fill, stroke, size, symbol, opacity, theta, strokeWidth and strokeDash. A legend on any other channel draws nothing and logs a warning. For x and y, the axis is the key. angle, a mark's rotation in place, has no key.

A fan and a weight stack

One needle mark encoding two channels; a key for each.

const rows = [
{ site: "A", x: 60, bearing: 20, flow: 2 },
{ site: "B", x: 140, bearing: 75, flow: 6 },
{ site: "C", x: 220, bearing: 130, flow: 9 },
];
const chart = Elicit({
width: 460, height: 210,
margins: { top: 18, right: 18, bottom: 22, left: 22 },
axes: false,
data: rows,
schema: {
site: { type: "categorical", domain: ["A", "B", "C"] },
x: { type: "quantitative", domain: [0, 280] },
bearing: { type: "quantitative", domain: [0, 180] },
flow: { type: "quantitative", domain: [0, 10] },
},
marks: [
// A needle per site: its ANGLE reads bearing, its WIDTH reads flow.
needle({ channels: {
x: { field: "x" },
y: { value: 90 },
theta: { field: "bearing", legend: { anchor: "right" } },
strokeWidth: { field: "flow", legend: { anchor: "right" } },
} }),
],
});
mount(chart.dom());

Any side

anchor: 'bottom' (or 'top') lays the swatches out in a row and reserves space on that side. The row sits past the axis, clear of the x-axis labels.

Legend on the bottom

A horizontal row below the x-axis; the plot height shrinks to fit.

const chart = Elicit({
width: 380, height: 240,
margins: { top: 20, right: 20, bottom: 30, left: 40 },
data: [
{ x: 1, y: 3, group: "A" }, { x: 2, y: 5, group: "B" },
],
schema: {
x: { type: "quantitative", domain: [0, 7] },
y: { type: "quantitative", domain: [0, 8] },
group: { type: "categorical", domain: ["A", "B", "C"] },
},
marks: [
point({ size: 7, channels: { x: { field: "x" }, y: { field: "y" }, fill: { field: "group" } } }),
// anchor:'bottom' lays the swatches out in a row and reserves space below
// the x-axis (offset past the author's bottom margin, so it clears the axis).
legendColor({ anchor: "bottom" }),
],
});
mount(chart.dom());

A category picker

Pass edit: edit.legend.category() to make each swatch a click target that sets the channel's field on the target row. The target is the legend's row, else the selection, else the sole row of a one-row table.

Click a swatch to pick

A one-row dataset whose category is set by clicking the legend.

const chart = Elicit({
width: 320, height: 170,
margins: { top: 20, right: 16, bottom: 20, left: 20 },
data: [ { group: "A" } ],
schema: { group: { type: "categorical", domain: ["A", "B", "C", "D"] } },
axes: false,
marks: [
// A one-row belief. The dot's colour reflects the current category.
point({ size: 26, channels: { x: { value: 90 }, y: { value: 65 }, fill: { field: "group" } } }),
// edit.legend.category() makes each swatch a click target that sets `group`.
legendColor({ anchor: "right", edit: edit.legend.category() }),
],
});
mount(chart.dom());
Try: Click a swatch to set the group; the dot recolours.

Two encodings on one channel

A scale belongs to one encoding, so two marks colouring by different fields get two scales. Each has its own domain, colour family and key, and each legend takes its column from the channel.

When you compose a legend by hand for a channel that carries two encodings, say which with field:

legendColor({ field: "goal" })

With one encoding on the channel, leave field out. With two and no field, the legend takes the first and logs a warning naming both.

In a multi-table schema, table names which table the column is in. A hand-composed legend needs it when the encoding doesn't settle the table.

Two encodings on fill

Two scales, two keys, two colour families — and each picker writes its own column.

const chart = Elicit({
width: 340, height: 190,
margins: { top: 20, right: 16, bottom: 20, left: 20 },
data: [ { now: "low", goal: "mid" } ],
schema: {
now: { type: "categorical", domain: ["low", "mid", "high"] },
goal: { type: "categorical", domain: ["low", "mid", "high"] },
},
axes: false,
marks: [
// Two marks put DIFFERENT columns on `fill`. That is two encodings, so it
// is two scales — different colour families, not one shared vocabulary.
point({ size: 22, channels: { x: { value: 60 }, y: { value: 70 }, fill: { field: "now" } } }),
point({ size: 22, channels: { x: { value: 60 }, y: { value: 115 }, fill: { field: "goal" } } }),
text({ text: "now", channels: { x: { value: 95 }, y: { value: 74 } }, textAnchor: "start", fontSize: 12 }),
text({ text: "goal", channels: { x: { value: 95 }, y: { value: 119 } }, textAnchor: "start", fontSize: 12 }),
// `field` NAMES the encoding each legend is a key for — the one case where
// it is required, because the channel alone no longer identifies a scale.
// It doubles as the title, so neither legend restates anything.
legendColor({ anchor: "right", field: "now", edit: edit.legend.category() }),
legendColor({ anchor: "right", field: "goal", edit: edit.legend.category() }),
],
});
mount(chart.dom());
Try: Click a swatch in either legend — each writes only its own column.

Declaring it on the channel

Putting legend: on the channel is the shortest way to a correct legend. The channel already names the field and the mark names the table, so the legend restates neither.

fill: { field: "group", legend: true }
fill: { field: "group", legend: { anchor: "bottom", title: "region" } }
fill: { field: "group", legend: false } // suppress under legends: true

A picker goes in the same place. The gesture happens on the legend, and the bars stay inert.

fill: { field: "group", legend: { edit: edit.legend.category() } }

Compose a legendColor() for the same encoding and it takes over that legend, edit included. The channel says what is edited, and the element sets how it looks:

barY({ channels: { fill: { field: "group", legend: { edit: edit.legend.category() } } } }),
legendColor({ anchor: "bottom" }),

The picker on the fill channel

No legendColor() in the spec — the legend is built from the encoding.

const chart = Elicit({
width: 380, height: 220,
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" },
// The edit sits on the channel whose column it writes. There is no
// legendColor() in this spec — the legend is minted for `fill`.
fill: { field: "group", edit: edit.legend.category() },
},
edits: [ edit.select() ]
}),
],
});
mount(chart.dom());
Try: Click a bar to select it, then click a swatch to change its group.

Editing a selected item

Selection is interface state, kept outside the data. Give the bars an edit.select() and a click selects one, drawing a highlight outline. The legend's row defaults to the selection, so the swatch you click edits the bar you picked. See Selection.

The swatch holding the target row's current value is ringed, so the legend shows what a click would replace. With nothing selected, a picker has no target and shows no ring. A one-row table is always the target.

Pin row to a number, an array of indices, or a (data, { selection }) => index function to override.

Pick a bar, then edit its group

The bars carry edit.select(); the legend edits the selected bar. Nothing about selection touches the data.

const chart = Elicit({
width: 380, height: 220,
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: [
// Click a bar to SELECT it. Selection is pipeline state (no `selected` column);
// the selected bar gets the highlight outline automatically.
barY({
channels: {
x: { field: "name" },
y: { field: "value" },
fill: { field: "group" },
},
edits: [ edit.select() ]
}),
// The legend picker edits whichever row is currently selected — its `row`
// defaults to the chart's selection, so there is no findIndex to keep in sync.
legendColor({
anchor: "right",
edit: edit.legend.category(),
}),
],
});
mount(chart.dom());
Try: Click a bar to select it, then click a legend swatch to change its group.

Editing several rows at once

edit.select({ multi: true }) makes shift-click (or meta-click) add a row to the selection. Shift-click a selected row to take it back out. A plain click still replaces the selection.

A picker writes every selected row, so one swatch click recategorises the whole selection. Where the selected rows hold different values, each of those swatches is ringed.

From your page code, chart.select([0, 2]) replaces the selection with those rows, chart.toggleSelection(i) adds or removes one, chart.selectAll() takes every row, and chart.selectWhere('group', 'North', { all: true }) takes a whole category. chart.getSelectionAll() reads them back.

Shift-click to select several bars

One swatch click sets the group on every selected bar.

const chart = Elicit({
width: 380, height: 220,
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" },
// Shift-click adds a bar to the selection; a plain click replaces it.
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 swatch.

Editing the category list

The pickers above write a row. edit.scale.categories() writes the domain: the list of categories on the scale the legend draws. It needs no target row.

Double-click a swatch to rename its category, click the × to drop it, and double-click the + to add one. Renaming relabels every row carrying the old value, and removing deletes those rows, so the schema and the data stay in step.

The same edit works on an axis — axisX({ edit: edit.scale.categories() }) — because both draw a scale.

Where one scale spans several columns, the edit writes all of them, including columns in different tables. A rename applies to every column that shares the category.

Rename, remove, add

The domain and the rows move together.

const chart = Elicit({
width: 380, height: 220,
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"] },
value: { type: "quantitative", domain: [0, 16] },
},
marks: [
barY({ channels: {
x: { field: "name" },
y: { field: "value" },
fill: { field: "group" },
} }),
// The domain belongs to the scale, so the edit sits on the element drawing it.
legendColor({ anchor: "right", edit: edit.scale.categories() }),
],
});
mount(chart.dom());
Try: Double-click a swatch to rename · click a × to remove · double-click the + to add.

One vocabulary across two tables

`team` is declared on both nodes and links; renaming writes both schemas and both row sets.

const chart = Elicit({
width: 420, height: 260,
margins: { top: 20, right: 20, bottom: 20, left: 20 },
axes: false,
schema: {
structure: "network",
tables: {
people: {
role: "nodes",
fields: {
id: { type: "categorical", key: true, open: true },
x: { type: "quantitative", domain: [0, 100] },
y: { type: "quantitative", domain: [0, 100] },
// The SAME vocabulary is declared on both tables.
team: { type: "categorical", domain: ["#EE7B5E", "#50AEB7"] },
},
},
ties: {
role: "links",
fields: {
source: { type: "ref" },
target: { type: "ref" },
team: { type: "categorical", domain: ["#EE7B5E", "#50AEB7"] },
},
},
},
},
data: {
people: [
{ id: "a", x: 20, y: 75, team: "#EE7B5E" },
],
ties: [
{ source: "a", target: "c", team: "#EE7B5E" },
{ source: "b", target: "c", team: "#50AEB7" },
],
},
marks: [
// One `stroke` scale spans BOTH tables' `team` columns.
link({ strokeWidth: 3, channels: { stroke: { field: "team" } } }),
point({ size: 12, fill: "#fff", channels: {
x: { field: "x" }, y: { field: "y" }, stroke: { field: "team" },
} }),
legendColor({ channel: "stroke", anchor: "right", edit: edit.scale.categories() }),
],
});
mount(chart.dom());
Try: Double-click a swatch and rename it — the nodes and the ties move together.

A continuous value picker

Pass edit: edit.legend.value() to add a handle you drag along the ramp. The handle's position maps to a value in the field's domain, and the edit writes that value.

Drag the ramp to pick a value

Dragging the handle sets temp; the dot's colour tracks it.

const chart = Elicit({
width: 320, height: 210,
margins: { top: 20, right: 16, bottom: 20, left: 20 },
data: [ { temp: 15 } ],
schema: { temp: { type: "quantitative", domain: [0, 30] } },
axes: false,
marks: [
// The dot's colour tracks the elicited value as you drag the ramp handle.
point({ size: 30, channels: { x: { value: 80 }, y: { value: 95 }, fill: { field: "temp" } } }),
// edit.legend.value() adds a handle you drag along the ramp to set `temp`.
// A colour scale isn't invertible, so the edit maps the drag to [0,30] by hand.
legendColor({ anchor: "right", edit: edit.legend.value() }),
],
});
mount(chart.dom());
Try: Drag the handle up and down the ramp to set the value.