Guides
A guide draws the rule and the state: what a gesture is allowed to do, and what is left to do. It views the interface's state rather than rows, so it has no channels, no table and no edit, and writes no data. Declare one in guides: [...] (guides.rule, guides.region, guides.image, guides.remaining, guides.proximity, guides.tip, guides.crosshair, guides.custom), or let an edit draw its own with guide:. Survey widgets use their own widget guides. Guides rebuild every render and let every gesture pass through. For hovered, selected and grabbed feedback, see Effects.
API reference
guides.rule · guides.region · guides.remaining · guides.proximity
Import guides from elicitjs and pass in the chart’s guides: [...]. All position in data space through the same scale.encode() a mark uses, so they compose across scale types. Non-interactive.
guides.rule({ x?, y?, label, stroke, strokeDash, strokeWidth, opacity }) → Guide
guides.region({ x?, y?, fill, opacity }) → Guide
guides.remaining({ field?, total?, unit?, format?, anchor?, label?, fill?, fontSize? }) → Guide
guides.proximity({ mark, stroke, strokeDash, strokeWidth, opacity }) → Guide
guides.custom((ctx) => FeatureNode[]) → Guide
// any option may be a function of the guide context:
guides.rule({ y: ({ data }) => d3.mean(data, (d) => d.y), label: "mean" })| Option | Type · default | Description |
|---|---|---|
rule.x / y | any | | The value to draw the reference line at — a number, a category, or a function of the data. Give both for a crosshair. |
rule.label | string | | Text drawn beside the line. |
rule.stroke / strokeDash / strokeWidth / opacity | styledefaulttheme.guide.rule | Line paint. Restyle every reference line at once through the theme instead. |
region.x / y | [a, | The two values to shade between on that axis. Give both for a rectangle. |
region.fill / opacity | styledefault#64748b / 0.1 | Band fill and opacity. |
remaining.field / total | string | defaultthe maintainSum's column · from constraints | The column being allocated, and the target sum. Omit `field` and it reads the `maintainSum`'s column, else the one column the chart's edits write. Omit `total` and it reads the target from the `maintainSum`, so the total you enforce and the total you show stay one number. With neither, it draws nothing and warns. |
remaining.unit / anchor / label | string · string · (remaining, default— · 'top-right' · — | `unit` gives the countable phrasing ("3 tokens left"); `anchor` picks the corner; `label` replaces the wording entirely. |
remaining.format | string | defaultauto | How the number reads: a d3-format string, or a formatter function. |
remaining.fill / fontSize | string / numberdefaulttheme / 11 | Readout colour and size. |
proximity.mark | string | The `id` of the mark whose proximity catchment to draw — a dashed ring at the pointer, the radius within which it reaches for a mark. Prefer `guide: { catchment: true }` on the edit itself, which needs no id. |
proximity.stroke / strokeDash / strokeWidth / opacity | styledefaulttheme catchment | Ring paint. |
id | string | Stable identity, on every guide. |
Each returns a Guide ({ isGuide: true, build(ctx) }), rebuilt every render so it tracks live data.
guides.image(options)
A picture behind the gridlines and marks, as a CSS background image on the plot. With no x or y it fills the plot; with them it is placed in data space and moves with the scales.
guides.image({ src, x?, y?, fit?, position?, repeat?, opacity? }) → Guide| Option | Type · default | Description |
|---|---|---|
src | string | | The picture's URL. Required. |
x / y | [a, defaultthe plot | The picture's extent on that axis, in data units. Unset, it spans the plot on that axis. |
fit | 'fill' | default'fill' | How the picture is sized in its box, as CSS object-fit. 'fill' stretches it, so data positions line up with it. |
position | stringdefault'center' | Where the picture sits in its box, as CSS background-position: keywords and percentages. |
repeat | 'no-repeat' | default'no-repeat' | Whether the picture repeats, as CSS background-repeat. |
opacity | number | default1 | Fades the picture. |
guides.custom(build)
Draw your own non-interactive nodes from the live render context. One word for “author your own” in every namespace — cf. edit.custom and constraints.custom.
guides.custom((ctx) => FeatureNode[]) → Guide| Option | Type · default | Description |
|---|---|---|
ctx | object | The guide context: { scales, state, features, featureNodes, ui, effects, width, height, stage }. |
return | FeatureNode[] | Nodes to draw. Every one is tagged pointerEvents: 'none', so a custom guide can never capture a gesture. |
node.background | booleandefaultfalse | Draw this node behind the marks — a track, a cell grid — rather than in front. |
guides.tip(options) · tip (on a mark)
A tooltip of the row under the pointer. It reads state and writes nothing, so it never blocks a gesture, and it shows on read-only rows too. Every mark takes tip as a shorthand for a tip bound to that mark.
barY({ tip: true, … }) // every column of the row
barY({ tip: ["city", "rain"], … }) // these columns
barY({ tip: (d) => `${d.city}: ${d.rain}`, … })
guides.tip({ mark, text, anchor, dx, dy, fill, stroke, fontSize, format, touch }) → Guide| Option | Type · default | Description |
|---|---|---|
mark | stringdefaultany mark | The id of the mark whose rows the tip reads. Left out, it reads whichever mark is under the pointer. |
text | string[] | defaultevery column | The columns to show as column: value lines, or a function of the row returning a string or an array of lines. |
anchor | 'pointer' | default'pointer' | Where the box sits: beside the pointer, or beside the mark's centre. It flips to stay inside the plot. |
dx / dy | numberdefault12 / 12 | The box's offset from its anchor, in px. |
fill / stroke | stringdefaulttheme background / axis stroke | The box's paint. |
fontSize | numberdefaulttheme font size + 1 | The text size, in px. |
format | string | | How a number prints: a d3 format specifier or a function. Without it, numbers round to three decimals. |
touch | booleandefaulttrue | Show for a finger, while it is on the chart. false shows the tip only for a mouse. |
guides.crosshair(options)
Rules through the pointer across the plot, each labelled with the value under the pointer on its axis. Hidden while the pointer is outside the plot.
guides.crosshair({ x, y, readout, stroke, strokeDash, fontSize, format, touch }) → Guide| Option | Type · default | Description |
|---|---|---|
x / y | booleandefaulttrue / true | Which rules to draw: the vertical one that reads x, the horizontal one that reads y. |
readout | booleandefaulttrue | Label each rule with its axis value, at the plot's bottom and left edges. |
stroke / strokeDash | stringdefaulttheme.guide.rule / '3 3' | The rules' colour and dash pattern. |
fontSize | numberdefaulttheme font size | The readout size, in px. |
format | string | | How a readout prints: a d3 format specifier or a function. Without it, numbers round to two decimals. |
touch | booleandefaulttrue | Follow a finger while it is on the chart. false shows the rules only for a mouse. |
Instrument affordances
The chrome a survey instrument draws itself with. Guides like any other — pass them in guides: [...]. The built-in widgets are assembled from these, so a hand-built instrument gets the same look.
guides.prompt(text, { y }) → Guide // the question, in the top margin
guides.options({ labelOffset, size, control }) → Guide // a choice's options
guides.cellGrid({ padding }) → Guide // a matrix's cells + headers
guides.sliderTrack({ format }) → Guide // a track with end labels
guides.quadrants({ x, y }) → Guide // centred axes + four end labels| Option | Type · default | Description |
|---|---|---|
prompt.y | number | Lift the prompt clear of whatever sits below it — column headers, an axis title. |
options.size / labelOffset | numberdefaultdesign / 30 | Ring radius in px, and the label's distance below it. |
options.control | "radio" | default"radio" | One answer or several: which control a box design draws in each option. |
cellGrid.padding | numberdefault3 | Each cell's inset from its band, in px. |
sliderTrack.format | fndefaultString | How the two end labels read. |
quadrants.x / y | string | The variable name to label each axis's high and low ends with. |
guide (on an edit)
What an edit draws about its own rules. guide: true turns on bounds and catchment at their defaults; an object turns parts on individually and restyles them.
guide: true | false | { bounds, catchment, track, color }| Option | Type · default | Description |
|---|---|---|
bounds | boolean | defaulton with guide: true | Constraint boundaries on the edit's own value channel — a clamp's band and limit lines, a snap's stops, a maintainSum cap. |
catchment | boolean | defaulton with guide: true | The reach of a proximity pick: the radius within which a free pointer resolves to a mark. Only drawn by picks that select one. |
track | boolean | defaultoff | Where a handle can travel. Opt-in: extra ink on a chart that already shows the handle, and worth it when the range isn't obvious. |
color | stringdefaulttheme.constraint.color | One colour for every part; a part's own colour still wins. |
<part>.dash · width · opacity | any | Per-part stroke appearance. |
Which kind you want
The two differ in who declares them.
guide: on an edit draws the edit's own rules. It already knows the channel, the scale and the constraints that gate it, so you repeat none of them. Use it to show what a gesture is allowed to do.
guides: [...] on the spec draws an annotation you choose — a target line, a shaded range — from whatever you compute out of the guide context.
Use the edit-owned form when the edit already knows what you want to draw. A guides.rule at a clamp limit restates that limit in a second place, and the two can drift apart.
An edit draws its own rules
guide: true on an edit turns on bounds and catchment. You repeat nothing from the constraints: the edit draws every dataset constraint whose field matches its own channel.
Bounds follow the edit's axis. The same clamp draws horizontal lines on a y-edit and vertical ones on an x-edit.
clamp and snap, drawn by the edit
guide: true draws the allowed band and a labelled line at each clamp limit.
const chart = Elicit({width: 400, height: 260,margins: { top: 16, right: 16, bottom: 28, left: 36 },data: [{ cat: "A", n: 55 }, { cat: "B", n: 30 }, { cat: "C", n: 70 }],schema: {cat: { type: "categorical", domain: ["A", "B", "C"] },n: { type: "quantitative", domain: [0, 100] },},// The rules this chart enforces. An edit governed by them can draw them.constraints: [ clamp({ field: "n", min: 20, max: 85 }) ],marks: [bar({channels: {x: { field: "cat" },// `guide: true` turns on bounds and catchment. This edit has no proximity// pick, so only the bounds draw: the allowed band and a labelled line at// each `clamp` limit. You repeat none of it — the edit draws every// constraint whose `field` matches its channel.y: { field: "n", edit: move({ guide: true }) },},}),],});mount(chart.dom());
What is left to do
guides.remaining() states how much of a budget is still unallocated — the count a reader needs while placing tokens or splitting a total.
guides.remaining({ field: "y", total: 20, unit: "token" }) // "3 tokens left"guides.remaining({ field: "y", total: 100 }) // "45 of 100 left"
Pass no total and it reads the target from the chart's own maintainSum constraint, so the number you enforce and the number you show cannot drift apart. With neither a total nor a maintainSum on the field, the guide draws nothing. Set unit for the countable phrasing, anchor ("top-right" by default) for the corner, and label for a function of (remaining, used, total) when you want your own wording.
A budget with its remainder
guides.remaining reads the target from the maintainSum constraint, so the budget is written once.
const chart = Elicit({width: 380, height: 260,margins: { top: 26, right: 14, bottom: 26, left: 34 },// No `total` here: the guide reads the target off the maintainSum// constraint, so the budget is written once.guides: [ guides.remaining({ field: "y", unit: "point" }) ],data: [{ x: "A", y: 25 }, { x: "B", y: 20 },{ x: "C", y: 15 }, { x: "D", y: 10 },],schema: {x: { type: "categorical", domain: ["A", "B", "C", "D"] },y: { type: "quantitative", domain: [0, 100] },},constraints: [ clamp({ min: 0 }), maintainSum({ total: 100, strategy: "cap" }) ],marks: [barY({fill: "#8A78C0",channels: {x: { field: "x" },y: { field: "y", edit: move() },},}),],});mount(chart.dom());
Restyling the parts
guide: true is shorthand for the parts at their defaults. Pass an object to switch and restyle each one.
A snap's stops draw as short ticks at the plot edge, where the axis also sits. Raise bounds.tickLength and bounds.tickOpacity to pull them clear of it.
bounds draws the constraint boundaries: a clamp's band and limits, a snap's stops, a maintainSum cap. On with guide: true.
catchment draws the radius in which a proximity pick resolves a free pointer to a mark. On with guide: true. Edits that select a target (pick: "nearest", pick: "sweep", brushSpan, brushRect, edit.geo.brush) also turn catchment on when you omit guide. Set guide: false to turn it off.
track draws where a handle can travel, for handles whose range isn't a scale. Off by default — turn it on where that range isn't obvious from the mark, as on a face's parameters.
Each part takes color, dash, width and opacity; bounds adds bandOpacity, fontSize, tickLength and tickOpacity. A color on the guide itself sets all three parts at once. Theme tokens live at theme.guide.bounds|catchment|track (and theme.constraint.color).
One colour, or one per part
Left sets one colour for every part. Right styles each separately and lengthens the snap ticks.
// `guide: true` is shorthand for the parts at their defaults. Pass an object to// switch and restyle each one.const rows = [{ x: 30, y: 45 }, { x: 60, y: 62 }, { x: 84, y: 28 }];const schema = {x: { type: "quantitative", domain: [0, 100] },y: { type: "quantitative", domain: [0, 100] },};const bounds = [clamp({ field: "y", min: 15, max: 80 }),snap({ field: "y", step: 10 }),];// One colour for every part.mount(Elicit({width: 300, height: 220,margins: { top: 14, right: 14, bottom: 26, left: 30 },data: rows, schema, constraints: bounds,marks: [point({size: 9,channels: {x: { field: "x" },y: { field: "y", edit: move({pick: "nearest", threshold: 40,guide: { bounds: true, catchment: true, color: "#1B3F60" },}) },},}),],}).dom());// Or one per part. `snap` stops draw as short ticks at the plot edge, where the// axis sits — `tickLength` and `tickOpacity` pull them clear of it.mount(Elicit({width: 300, height: 220,margins: { top: 14, right: 14, bottom: 26, left: 30 },data: rows, schema, constraints: bounds,marks: [point({size: 9,channels: {x: { field: "x" },y: { field: "y", edit: move({pick: "nearest", threshold: 40,guide: {bounds: {color: "#EE7B5E", dash: "6 2", width: 1.5, opacity: 1,tickLength: 18, tickOpacity: 0.9,},catchment: { color: "#94A5B3", dash: "1 4", width: 1, opacity: 0.35 },},}) },},}),],}).dom());
Rule and region
Annotations you declare in guides: [...]. guides.region shades a band, guides.rule draws a reference line. Both position in data space through the same scales a mark uses, so they stay put as points move.
A target band with a rule
guides.region shades 40–60; guides.rule marks the midpoint.
const chart = Elicit({width: 380, height: 260,margins: { top: 14, right: 14, bottom: 26, left: 30 },guides: [guides.region({ y: [40, 60], fill: "#1B3F60", opacity: 0.14 }),guides.rule({ y: 50, label: "target 50" }),],data: [{ x: 20, y: 30 }, { x: 50, y: 55 }, { x: 80, y: 70 }],schema: {x: { type: "quantitative", domain: [0, 100] },y: { type: "quantitative", domain: [0, 100] },},marks: [point({fill: "#1B3F60",size: 9,channels: {x: { field: "x" },y: { field: "y" },},edits: [ move({ channels: ["x", "y"] }) ],}),],});mount(chart.dom());
An image behind the marks
guides.image({ src }) draws a picture behind the gridlines and marks, as a CSS background image on the plot. With no x or y it fills the plot, which makes it the chart's background. Give x: [lo, hi] and y: [lo, hi] in data units to place it, and it moves with the scales. Unset, either axis spans the plot.
fit defaults to 'fill': the picture stretches to its box, so a position on it reads in the same units as the axes. fit, position and repeat take the same values as on the image channel, so fit: 'none', repeat: 'repeat' tiles a texture. opacity fades it.
For a picture per row, or one inside a mark, see Image.
A floor plan to place things on
guides.image spans 40 × 30 m, so each point's x and y are metres on the plan.
const chart = Elicit({width: 440, height: 340,margins: { top: 14, right: 14, bottom: 30, left: 34 },data: [{ item: "router", x: 6, y: 24 },{ item: "speaker", x: 30, y: 6 },],schema: {item: { type: "categorical" },x: { type: "quantitative", domain: [0, 40] },y: { type: "quantitative", domain: [0, 30] },},guides: [// The plan spans 40 × 30 m, so a point on it reads in metres.guides.image({ src: "/images/floorplan.svg", x: [0, 40], y: [0, 30] }),],marks: [point({fill: "#2B5D87", size: 8,channels: { x: { field: "x" }, y: { field: "y" } },edits: [ move({ channels: ["x", "y"] }) ],}),text({dy: -16,channels: { x: { field: "x" }, y: { field: "y" }, text: { field: "item" } },}),],});mount(chart.dom());
Guides derived from the data
Every option takes a literal or a function of the guide context ({ data, scales, features, ui, width, height, stage }), so an annotation can read the rows it annotates. Here one rule is a fixed target and the other is the running mean. Guides rebuild each render, so it recomputes on every commit. The band tracks the spread the same way.
A mean line that chases the bars
guides.rule({ y: ({ data }) => d3.mean(data, (d) => d.y) }) reads the dataset.
const chart = Elicit({width: 380, height: 260,margins: { top: 14, right: 14, bottom: 26, left: 30 },guides: [// A literal: a fixed reference.guides.rule({ y: 50, label: "target 50" }),// A function of the context: recomputed from the live rows every render.guides.region({y: ({ data }) => [d3.min(data, (d) => d.y), d3.max(data, (d) => d.y)],fill: "#50AEB7", opacity: 0.10,}),guides.rule({y: ({ data }) => d3.mean(data, (d) => d.y),label: "mean",stroke: "#EE7B5E",}),],schema: {x: { type: "categorical", domain: ["A", "B", "C", "D"] },y: { type: "quantitative", domain: [0, 100] },},data: [{ x: "A", y: 30 }, { x: "B", y: 55 },{ x: "C", y: 45 }, { x: "D", y: 70 },],constraints: [ clamp({ field: "y", min: 0, max: 100 }) ],marks: [bar({fill: "#50AEB7",channels: {x: { field: "x" },y: { field: "y", edit: move() },},}),],});mount(chart.dom());
Tooltips
tip: true on any mark shows a tooltip of the row under the pointer: one column: value line per column. Pass a list of columns (tip: ["city", "rain"]) to show only those, or a function of the row (tip: (d) => string) for your own wording.
A tip is a guide, so gestures pass through it: drag a bar while its tooltip is showing. It also shows on read-only rows, such as a locked seed or a context row.
guides.tip() is the long form. text takes the columns or a function of the row returning a string or an array of lines. anchor: "mark" pins the box beside the mark's centre instead of the pointer, and mark limits it to the mark with that id. format sets how numbers print, as a d3 format specifier or a function.
On a touch screen the tip shows while your finger is on the chart. Set touch: false to show it only for a mouse. See Mobile & touch.
A tip on every bar
tip: true on a barY with move(). The tooltip follows the pointer; the drag works underneath.
const chart = Elicit({width: 400, height: 260,margins: { top: 16, right: 16, bottom: 32, left: 40 },schema: {city: { type: "categorical", domain: ["Lima", "Oslo", "Pune", "Rome"] },rain: { type: "quantitative", domain: [0, 100] },},data: [{ city: "Lima", rain: 12 }, { city: "Oslo", rain: 64 },{ city: "Pune", rain: 78 }, { city: "Rome", rain: 41 },],marks: [barY({fill: "#50AEB7",// A tooltip of the row's columns. The bars stay draggable under it.tip: true,channels: {x: { field: "city" },y: { field: "rain", edit: move() },},}),],});mount(chart.dom());
A tip in your own words
guides.tip({ anchor: "mark", text: (d) => [...] }) writes two lines beside the point.
const chart = Elicit({width: 400, height: 260,margins: { top: 16, right: 16, bottom: 32, left: 40 },schema: {party: { type: "categorical", domain: ["Green", "Labour", "Liberal", "Other"] },share: { type: "quantitative", domain: [0, 60] },},data: [{ party: "Green", share: 12 }, { party: "Labour", share: 38 },{ party: "Liberal", share: 31 }, { party: "Other", share: 9 },],guides: [guides.tip({anchor: "mark",text: (d) => [`${d.party}`, `${d.share.toFixed(0)}% of the vote`],}),],marks: [point({fill: "#7DB86B", size: 8,channels: {x: { field: "party" },y: { field: "share", edit: move() },},}),],});mount(chart.dom());
Crosshair
guides.crosshair() draws dashed rules through the pointer across the plot. Each rule reads the value under the pointer off its axis, at the plot's bottom and left edges. Set x: false or y: false to draw one rule, and readout: false to drop the labels. format sets how the readouts print.
The rules hide when the pointer leaves the plot. Gestures pass through them. On a touch screen they follow your finger while it is down; touch: false turns that off.
A crosshair with readouts
guides.crosshair({ format: ".1f" }) reads hours and score under the pointer.
const chart = Elicit({width: 420, height: 260,margins: { top: 16, right: 16, bottom: 32, left: 40 },schema: {hours: { type: "quantitative", domain: [0, 12] },score: { type: "quantitative", domain: [0, 100] },},data: [{ hours: 2, score: 35 }, { hours: 4, score: 52 },{ hours: 7, score: 68 }, { hours: 10, score: 81 },],guides: [guides.crosshair({ format: ".1f" })],marks: [point({fill: "#EE7B5E", size: 6,channels: { x: { field: "hours" }, y: { field: "score" } },edits: [move({ channels: ["x", "y"] })],}),],});mount(chart.dom());
Widget guides
The widgets draw their chrome with guides you can also use in your own spec:
guides.prompt(text, { y })draws the question in the top margin.guides.options({ labelOffset, size, control })draws a choice's options, one per category, in the theme's design: rings and labels by default.sizeis a ring's radius in px.guides.cellGrid({ padding })draws a matrix's cells and headers, each cell inset bypaddingpx.guides.sliderTrack({ format })draws a slider's track with end labels.guides.quadrants({ x, y })draws centred axes with a label at each end.
Each reads its colours from theme.widget.