Constraints
A constraint is a rule over the dataset, so it applies to every edit on every mark. It can reject a proposed change, or repair it by returning corrected rows. Every mark reads the same rows, so a repair shows up everywhere on the next render. Declare constraints in constraints: [...] on the Elicit spec, or on Elicit.dataset for several charts. clamp and snap bound a field, maintainSum fixes a total, count caps the number of rows, and unique forbids duplicate keys. ordering keeps a row's fields in order (lo ≤ mean ≤ hi), monotonic keeps a cumulative curve from dipping, spacing keeps neighbours apart, follow and symmetric couple a range to its centre, and contain governs membership in a hierarchy. constraints.custom defines your own.
API reference
Built-in constraints
Import constraints from elicitjs and pass on the Elicit spec’s constraints: [...]. That is the only place they go: a constraint gates every edit from every mark, so writing one inside a mark would say something the engine does not mean. All are pure data invariants — they run on every commit and never see pixels.
clamp({ min, max, field, table }) → Constraint
maintainSum({ total, field, strategy, table }) → Constraint
count({ max, strategy, table }) → Constraint
unique({ field, max, strategy, table }) → Constraint
snap({ field, step, origin, table }) → Constraint
ordering({ field, strategy, table }) → Constraint
monotonic({ field, along, direction, series, table }) → Constraint
spacing({ field, min, series, table }) → Constraint
follow({ field, lead, table }) → Constraint
symmetric({ field, about, table }) → Constraint
contain({ parent, child, maxDepth, maxChildren, table }) → Constraint| Option | Type · default | Description |
|---|---|---|
table | stringdefaultthe primary table | Every constraint takes it: the table the rule is about, by name or by role. contain defaults to the nesting role. A name that matches no table warns, and the rule runs on the primary table. |
clamp.min / max | numberdefaultfield domain | Bounds every cell the edit changed to [min, max]. An omitted bound falls back to the field’s declared domain. |
clamp.field | string | defaultthe edited column | The column(s) to bound. Every cell the proposal changed in them is clamped, including a neighbour another constraint pushed. |
maintainSum.total | number | Target total for the field. |
maintainSum.field | stringdefaultthe edited column | The column whose rows must sum to the total. |
maintainSum.strategy | 'cap' | default'cap' | cap bounds the touched datum (≤ sum); normalize scales all values to exact sum; redistribute holds the edited value and proportionally adjusts siblings. |
count.max | numberdefaultrequired | Maximum number of data rows. Context rows are not counted. The rule judges an edit that adds rows. Without `max` it does nothing and warns. |
count.strategy | 'replace' | default'replace' | Over the limit: drop the oldest rows (keep the newest max, and every context row) or refuse the interaction. |
unique.field | string | defaultthe edited column | Category key(s); an array makes a composite (per-cell) key. |
unique.max / strategy | number / stringdefault1 / 'reject' | How many may share a key, and whether to reject or replace the resident. |
snap.step / origin | numberdefault1 / 0 | Quantize the field to a grid (slider steps, waffle cells): the spacing, and the value the grid is anchored at. |
snap.field | string | defaultthe edited column | The column(s) to quantize. Every cell the proposal changed in them is snapped, so both rows of a stack edge stay on the grid. A temporal cell stays a Date, snapped in milliseconds. |
ordering.field | string[] | Columns of one row that must stay in this order, as one ordered list — ["lo", "mean", "hi"] means lo ≤ mean ≤ hi. At least two are needed. |
ordering.strategy | 'push' | default'push' | push repairs — the field you dragged wins and its neighbours give way, so the interval moves. reject refuses the edit instead, for an elicitation where the bounds are given and only the estimate inside them moves. |
monotonic.field / along | stringdefaultthe edited column / 'x' | The value that may never reverse, and the axis it runs along. Where ordering keeps fields of one row in order, this keeps rows in order along an axis. Rows are sorted by along, not by array position, so an appended anchor lands in the right place. |
monotonic.direction | 'up' | default'up' | up = non-decreasing (a CDF, a budget burning up); down = non-increasing (a survival curve). |
spacing.field / min | string / numberdefaultthe edited column / 1 | Adjacent values of field stay at least min apart, in data units — never pixels. Implies an order too (pushing apart preserves the sort), so a field with spacing needs no ordering. |
follow.field / lead | string[] / string | When lead moves, each column in field moves by the same amount. A column the edit set itself is left alone, so dragging an end moves only that end. Both are required. |
symmetric.field / about | [string, | When one column of the pair moves, the other is set to its mirror image about about. Declare after follow and before ordering and clamp. |
monotonic.series / spacing.series | string | defaultnull | Group rows by this field first, so each line of a multi-series chart is judged on its own. |
contain.parent / child | (d, | Predicates over the nodes table of a hierarchy: which rows may hold others, and which may sit inside a container. |
contain.maxDepth / maxChildren | numberdefault∞ | Limits on nesting depth (root = 0) and on direct children per container. Targets the nesting table automatically. Rows in data or setData() that break the rule are kept, and one warning names them. An edit is refused only for a break it adds. |
Each returns a Constraint — a reducer the engine runs on the proposed dataset after every edit.
constraints.custom(apply, meta?)
Author your own. One word for “author your own X” in every grammar namespace — cf. edit.custom and guides.custom. Every constraint is a descriptor — { type, field?, options, apply } — and field left out means the column the dispatching edit writes, so a one-column instrument names nothing twice. The rule gets a pure-data context and returns the shape that’s natural.
constraints.custom(({ data, oldData, activeIndex, active, field, fields, value, domain, table, tables }) => result, meta?) → Constraint| Option | Type · default | Description |
|---|---|---|
return number | The constrained value for the active datum’s field. | |
return object | Fields merged into the active datum. | |
return array | A full replacement dataset (cross-datum rules: sum, unique, count). | |
return false | Reject the whole interaction. | |
return true / undefined | Accept unchanged. | |
meta.field | string | defaultthe edited column | The column the rule is about, or an ordered list of them. Left out, the rule reads the column the edit writes. |
meta.table | stringdefaultthe primary table | The table the rule is about, by role or name. |
meta.type | stringdefault'custom' | The rule’s name, as its descriptor reports it. |
meta.options | objectdefault{} | The rule’s own settings, kept on the descriptor so a guide can read them. |
meta.guide | function | Optional drawer so an edit with guide:true can show this constraint’s bounds. |
A Constraint. The ctx.active is the datum the gesture touched; domain is that field’s declared data range.
maintainSum — a total held at 100
maintainSum holds the total for every edit on every mark. Drag one bar and the others give way to keep the total at 100. guide: true draws the live bound.
Bars that compensate
maintainSum bounds the touched bar so the total stays 100; a rule guide marks the even split.
const chart = Elicit({width: 380, height: 260,margins: { top: 14, right: 14, bottom: 26, left: 30 },guides: [ guides.rule({ y: 25, label: "even split (25)" }) ],data: ["A", "B", "C", "D"].map((c) => ({ x: c, y: 25 })),// Invariants on the DATA — they gate every edit, from every mark.constraints: [clamp({ field: "y", min: 0 }),maintainSum({ field: "y", total: 100 }),],schema: {x: { type: "categorical", domain: ["A", "B", "C", "D"] },y: { type: "quantitative", domain: [0, 100] },},marks: [bar({fill: "#50AEB7",channels: {x: { field: "x" },y: { field: "y", edit: move({ guide: true }) },},}),],});mount(chart.dom());
clamp — bound a field
clamp holds a field inside [min, max]. It bounds the field you name, or the column the edit writes when you name none. An omitted bound falls back to the field's declared domain. field also takes a list, so one clamp can bound every column of a glyph. Pair it with a nearest pick and guide: true to show the bounds while you drag.
clamp and snap repair every cell the proposal changed. A neighbour that ordering or monotonic pushed is bounded too. Both rows of a stack edge land on the grid together, so the stack keeps its total. snap keeps a temporal field a Date, and its step is in milliseconds.
Clamped drag with a guide
The y edit is clamped to 0–90; guide: true draws the bounds and the snap ring.
const chart = Elicit({width: 380, height: 260,margins: { top: 14, right: 14, bottom: 26, left: 30 },data: [{ x: "A", y: 20 }, { x: "B", y: 45 },{ x: "C", y: 30 }, { x: "D", y: 60 },],constraints: [ clamp({ field: "y", min: 0, max: 90 }) ],schema: {x: { type: "categorical", domain: ["A", "B", "C", "D"] },y: { type: "quantitative", domain: [0, 100] },},marks: [bar({fill: "#50AEB7",channels: {x: { field: "x" },y: { field: "y",edit: move({ pick: "nearest", threshold: 40, guide: true }) },},}),],});mount(chart.dom());
unique — one per key
unique forbids two rows with the same key. A single field guards one axis. An array is a composite key: at most one mark per cell, for both create and move.
One bar per category
create mints wherever you click, but unique({ field: "x" }) rejects a filled slot.
const chart = Elicit({width: 380, height: 260,margins: { top: 14, right: 14, bottom: 26, left: 30 },data: [{ x: "A", y: 40 }, { x: "C", y: 60 }],constraints: [ unique({ field: "x", max: 1 }) ],schema: {x: { type: "categorical", domain: ["A", "B", "C", "D"] },y: { type: "quantitative", domain: [0, 100] },},marks: [bar({fill: "#1B3F60",channels: {x: { field: "x" },y: { field: "y", edit: move() },},edits: [ create({ defaults: { y: 20 } }) ],}),],});mount(chart.dom());
Composite key — a band × band grid
unique({ field: ["x","y"] }) allows at most one mark per (x, y) cell.
const chart = Elicit({width: 400, height: 320,data: [{ x: "A", y: "Low", group: "alpha" },],constraints: [ unique({ field: ["x", "y"], max: 1 }) ],schema: {x: { type: "categorical", domain: ["A", "B", "C"] },y: { type: "ordinal", domain: ["Low", "Mid", "High"] },group: { type: "ordinal", domain: ["alpha", "beta", "gamma"] },},marks: [point({size: 12,channels: {x: { field: "x" },y: { field: "y" },fill: { field: "group" },},edits: [move({ channels: ["x", "y"] }),create({ defaults: { group: "alpha" } }),remove({ when: when.alt }),],}),],});mount(chart.dom());
ordering — fields of a row, in order
An error bar's caps and mean are separate marks over separate fields of one row. ordering keeps those fields in order across all three marks, whichever handle you drag. A low cap dragged above the high one would otherwise turn the interval inside-out.
An interval that can’t invert
ordering({ field: ["lo", "mean", "hi"] }) — one rule across three marks.
const chart = Elicit({width: 320, height: 260,margins: { top: 20, right: 20, bottom: 30, left: 44 },axes: { y: {}, x: false },schema: {q: { type: "categorical", domain: ["2027 GDP"] },lo: { type: "quantitative", domain: [0, 10] },mean: { type: "quantitative", domain: [0, 10] },hi: { type: "quantitative", domain: [0, 10] },},data: [{ q: "2027 GDP", lo: 2, mean: 4, hi: 6 }],// Declared ONCE, on the dataset — so it holds whichever of the three// handles you grab, and would hold for a create or a keyboard nudge too.constraints: [ ordering({ field: ["lo", "mean", "hi"] }) ],marks: [// An interval glyph is three marks over three FIELDS of one row.ruleY({ channels: { x: { field: "q" },y1: { field: "lo" }, y2: { field: "hi" } },stroke: "#94A5B3", strokeWidth: 2 }),tickY({ channels: { x: { field: "q" }, y: { field: "lo",edit: move() } }, stroke: "#6F8294" }),tickY({ channels: { x: { field: "q" }, y: { field: "hi",edit: move() } }, stroke: "#6F8294" }),point({ size: 7, fill: "#50AEB7",channels: { x: { field: "q" }, y: { field: "mean",edit: move() } } }),],});mount(chart.dom());
follow and symmetric — a range that moves with its mean
follow({ field: ['lo', 'hi'], lead: 'mean' }) moves the listed columns by the amount the lead column moved. Drag the mean and the whole interval travels with it. Drag an end and only that end moves, because follow reacts to the lead alone. The handles are plain move() edits.
symmetric({ field: ['lo', 'hi'], about: 'mean' }) mirrors the pair about the centre. Drag one end and the other moves to the same distance on the opposite side. It takes exactly two columns.
Declare them in the order [follow, symmetric, ordering, clamp]. Constraints run in the order you list them, so the coupling settles first, then ordering and clamp repair what it produced.
A range that follows its mean
follow({ field: ["lo", "hi"], lead: "mean" }) — the ends move with the mean, and on their own.
const chart = Elicit({width: 320, height: 260,margins: { top: 20, right: 20, bottom: 30, left: 44 },axes: { y: {}, x: false },schema: {q: { type: "categorical", domain: ["2027 GDP"] },lo: { type: "quantitative", domain: [0, 10] },mean: { type: "quantitative", domain: [0, 10] },hi: { type: "quantitative", domain: [0, 10] },},data: [{ q: "2027 GDP", lo: 2.5, mean: 4, hi: 7 }],constraints: [// Drag the mean: lo and hi move by the same amount.follow({ field: ["lo", "hi"], lead: "mean" }),ordering({ field: ["lo", "mean", "hi"] }),clamp({ field: ["lo", "mean", "hi"] }),],marks: [ruleY({ channels: { x: { field: "q" },y1: { field: "lo" }, y2: { field: "hi" } },stroke: "#94A5B3", strokeWidth: 2 }),tickY({ channels: { x: { field: "q" }, y: { field: "lo",edit: move() } }, stroke: "#6F8294" }),tickY({ channels: { x: { field: "q" }, y: { field: "hi",edit: move() } }, stroke: "#6F8294" }),point({ size: 7, fill: "#50AEB7",channels: { x: { field: "q" }, y: { field: "mean",edit: move() } } }),],});mount(chart.dom());
A symmetric interval
symmetric({ field: ["lo", "hi"], about: "mean" }) added — one end's drag mirrors the other.
const chart = Elicit({width: 320, height: 260,margins: { top: 20, right: 20, bottom: 30, left: 44 },axes: { y: {}, x: false },schema: {q: { type: "categorical", domain: ["2027 GDP"] },lo: { type: "quantitative", domain: [0, 10] },mean: { type: "quantitative", domain: [0, 10] },hi: { type: "quantitative", domain: [0, 10] },},data: [{ q: "2027 GDP", lo: 2, mean: 4, hi: 6 }],constraints: [follow({ field: ["lo", "hi"], lead: "mean" }),// Drag one end: the other mirrors it about the mean.symmetric({ field: ["lo", "hi"], about: "mean" }),ordering({ field: ["lo", "mean", "hi"] }),clamp({ field: ["lo", "mean", "hi"] }),],marks: [ruleY({ channels: { x: { field: "q" },y1: { field: "lo" }, y2: { field: "hi" } },stroke: "#94A5B3", strokeWidth: 2 }),tickY({ channels: { x: { field: "q" }, y: { field: "lo",edit: move() } }, stroke: "#6F8294" }),tickY({ channels: { x: { field: "q" }, y: { field: "hi",edit: move() } }, stroke: "#6F8294" }),point({ size: 7, fill: "#50AEB7",channels: { x: { field: "q" }, y: { field: "mean",edit: move() } } }),],});mount(chart.dom());
monotonic — a curve that can’t go back on itself
A CDF, a survival function or a dose-response curve can't dip, but a drawn stroke can. monotonic repairs the drag: it pushes the rows the moved point would have crossed.
A locked row stays where it is. The dragged value stops at the nearest locked row on either side, and the push carries between them.
A curve that can only rise
monotonic({ field: "p", along: "year" }) — the rule behind every CDF.
const chart = Elicit({width: 420, height: 260,margins: { top: 16, right: 16, bottom: 32, left: 42 },schema: {year: { type: "quantitative", domain: [2025, 2032] },p: { type: "quantitative", domain: [0, 1] },},data: [{ year: 2025, p: 0.05 }, { year: 2026, p: 0.12 },],// "By when will it have happened?" — a cumulative probability cannot dip,// because a dip is negative probability mass. The gesture has no idea; the// dataset invariant does.constraints: [monotonic({ field: "p", along: "year" }),clamp({ min: 0, max: 1, field: "p" }),],marks: [lineY({stroke: "#50AEB7", handleSize: 5,channels: { x: { field: "year" },y: { field: "p", edit: move() } },}),],});mount(chart.dom());
spacing — neighbours that keep their distance
spacing keeps values a minimum distance apart. Use it where the values must stay distinct: thresholds of a scale, breakpoints of a piecewise curve, tiers of a rubric. Two points at the same value are usually a slip. Once they overlap, the top one takes every grab and you can't separate them.
The chain always fits inside the field's domain. With three thresholds beyond the one you drag, the dragged one stops 3 × min short of the domain's edge, which leaves room for the three it pushes.
Thresholds that stay apart
spacing({ field: "at", min: 8 }) — no two boundaries closer than 8 points.
const chart = Elicit({width: 420, height: 170,margins: { top: 20, right: 24, bottom: 34, left: 24 },axes: { x: {}, y: false },schema: {grade: { type: "categorical", domain: ["D", "C", "B", "A"] },at: { type: "quantitative", domain: [0, 100] },},data: [{ grade: "D", at: 50 }, { grade: "C", at: 65 },{ grade: "B", at: 78 }, { grade: "A", at: 90 },],// Where do the grade boundaries go? Two coincident thresholds are almost// always a slip of the hand — they read as one, and the top one then takes// every later grab, so they can never be separated again.// `min` is in DATA units: a constraint never sees the scale, and a rule that// changed meaning when the chart was resized wouldn't be an invariant.constraints: [spacing({ field: "at", min: 8 }),clamp({ min: 0, max: 100, field: "at" }),],marks: [tickX({strokeWidth: 2, stroke: "#50AEB7",channels: { x: { field: "at", edit: move() } },}),text({dy: -14, fontSize: 11, fill: "#566A7C",channels: { x: { field: "at" }, text: { field: "grade" } },}),],});mount(chart.dom());
ordering as well.