Bar
bar draws one rectangle per row: a categorical band on one axis, and a value on the other, drawn as length from a baseline or as a span (x1/x2, y1/y2). bar takes its orientation from the band axis; barY forces vertical, barX horizontal. For rectangles that span both axes, use rect with brushRect().
API reference
bar(options) · barY(options) · barX(options)
Import plot from elicitjs. bar infers orientation from which axis is a band; barY forces vertical, barX horizontal. All three share these options.
bar({ channels, orientation, edits, id }) → Mark
barY(options) → Mark // orientation: "vertical"
barX(options) → Mark // orientation: "horizontal"| Option | Type · default | Description |
|---|---|---|
channels | objectdefault{} | Channel map — one band axis (category) and one linear axis (value or a span). See Channels. |
image | string | | The shape's background image: a URL painted inside each shape in place of the fill. Takes fit, position and repeat as channel params, as in CSS. See Image. |
orientation | 'vertical' | defaultauto | Override the inferred direction (bar only; barY/barX pin it). |
handles / handleSize / handleColor | boolean | defaulttrue with a stack edit / 5 / theme | Boundary dots on a stacked bar. Unset, they draw once a edit.stack.* edit is wired. true always draws them, as ink on a bar with no edit. 'hit' keeps them grabbable but invisible. false is neither drawn nor grabbable. |
gradient | { stops, | A linear-gradient fill over each bar’s own box, in place of a flat fill. stops is a list of [offset, colour] with offsets 0–1; direction is 'vertical' (default, bottom to top) or 'horizontal' (left to right). |
cornerRadius | numberdefaulttheme (0) | Corner radius in px. Defaults to the theme’s marks.bar.cornerRadius. |
edits | Edit[] | Mark-level edits; per-channel edits live in channels[ch].edit. A stacked bar also takes edit.stack.cut(), edit.stack.edge() and edit.stack.merge(). |
fill, | styledefaultfill: 'steelblue' | Style shorthands / channels (see the style surface on any mark). |
| Channel | Type | Description |
|---|---|---|
x | band | | Category (band) or value (linear), depending on orientation. |
y | band | | The other axis. The value axis carries edit: move() to make bars draggable. |
y1 / y2 | linear | Explicit vertical span (barY) — draw between two values instead of from the baseline. With one end given, the other is the baseline. |
x1 / x2 | linear | Explicit horizontal span (barX) — a Gantt-style range per category. |
xOffset / yOffset | band | A second category nested inside each x (or y) category: one sub-band per value, for grouped bars. |
stack | categorical | | Stack the bars that share a band. The field names each segment and sets their order (its schema domain, else first appearance). edit.stack.cut() writes a new segment’s category into it. Declare a schema domain covering the stacked total. |
fill, | const | | Standard style surface; a field tints through the ordinal palette. |
A mark emitting one rect per datum, styled through the standard surface.
Band × value
The band axis places the bars. The linear axis sets their length from a baseline.
gradient: { stops, direction } fills each bar with a linear gradient over its own box. Each stop is [offset, colour], from 0 to 1. direction: 'vertical' (the default) runs from the bottom of the bar to its top; 'horizontal' runs left to right. area and rect take the same option.
A vertical bar chart
x band of categories, y linear value.
const chart = Elicit({width: 380, height: 240,margins: { top: 16, right: 16, bottom: 28, left: 34 },data: [{ cat: "A", n: 30 }, { cat: "B", n: 55 },{ cat: "C", n: 22 }, { cat: "D", n: 44 },],schema: {cat: { type: "categorical", domain: ["A", "B", "C", "D"] },n: { type: "quantitative", domain: [0, 60] },},marks: [bar({fill: "#50AEB7",channels: {x: { field: "cat" },y: { field: "n" },},}),],});mount(chart.dom());
Colour by a field
fill: { field: "kind" } tints each bar through the ordinal palette.
const chart = Elicit({width: 340, height: 240,margins: { top: 14, right: 14, bottom: 26, left: 30 },data: [{ cat: "A", n: 34, kind: "low" }, { cat: "B", n: 58, kind: "high" },{ cat: "C", n: 22, kind: "low" }, { cat: "D", n: 47, kind: "high" },],schema: {cat: { type: "categorical", domain: ["A", "B", "C", "D"] },n: { type: "quantitative", domain: [0, 60] },kind: { type: "categorical", domain: ["low", "high"] },},marks: [bar({channels: {x: { field: "cat" },y: { field: "n" },fill: { field: "kind" },},}),],});mount(chart.dom());
A gradient fill
gradient: { stops: [[0, pale], [1, teal]] } — every bar shades from bottom to top.
const chart = Elicit({width: 380, height: 240,margins: { top: 16, right: 16, bottom: 28, left: 34 },data: [{ cat: "A", n: 30 }, { cat: "B", n: 55 },{ cat: "C", n: 22 }, { cat: "D", n: 44 },],schema: {cat: { type: "categorical", domain: ["A", "B", "C", "D"] },n: { type: "quantitative", domain: [0, 60] },},marks: [bar({// Offset 0 is the bottom of each bar, 1 the top.gradient: { stops: [[0, "#EDF7F8"], [1, "#50AEB7"]] },channels: {x: { field: "cat" },y: { field: "n", edit: move() },},}),],});mount(chart.dom());
Horizontal bars (barX)
Put the band on y and the value on x, and the bars run horizontally. barX forces this orientation; bar infers it from the band axis.
barX — value on x
y is the category band, x the linear value drawn rightward from the baseline.
const chart = Elicit({width: 380, height: 240,margins: { top: 14, right: 16, bottom: 26, left: 60 },data: [{ region: "North", sales: 42 }, { region: "South", sales: 68 },{ region: "East", sales: 30 }, { region: "West", sales: 54 },],schema: {region: { type: "categorical", domain: ["North", "South", "East", "West"] },sales: { type: "quantitative", domain: [0, 80] },},marks: [barX({fill: "#1B3F60",channels: {y: { field: "region" },x: { field: "sales" },},}),],});mount(chart.dom());
Stacked bars
The stack channel stacks the bars that share a band. Each segment sits on the cumulative total of the ones before it. Rows with the same x (or y, for barX) stack together. Declare a schema domain that covers the stacked total.
The stack field names each segment, and must be categorical or ordinal. Segments follow the order of its schema domain, so the same category sits at the same height in every band. With no domain declared, they follow the order the values first appear in the data. Colour is separate: bind fill to the same field to colour each segment by its category.
stack is the counterpart of xOffset: xOffset places the rows of a band side by side, and stack places them end to end. Without either, rows that share a band draw over each other. Elicit.js logs a warning, unless the bars are translucent (a constant opacity or fillOpacity below 1), which layers them on purpose. A span (x1/x2 or y1/y2) places each bar itself, so stack has no effect on it.
A stacked bar chart
stack and fill both on { field: "asset" }. The pct domain covers the total, [0, 100].
const chart = Elicit({width: 380, height: 240,margins: { top: 16, right: 16, bottom: 28, left: 40 },data: [{ year: "2023", asset: "stocks", pct: 50 },],schema: {year: { type: "categorical", domain: ["2023", "2024"] },asset: { type: "categorical", domain: ["stocks", "bonds", "cash"] },pct: { type: "quantitative", domain: [0, 100] },},legends: true,marks: [bar({channels: {x: { field: "year" },y: { field: "pct" },stack: { field: "asset" },fill: { field: "asset" },},}),],});mount(chart.dom());
Grouped bars (xOffset, yOffset)
xOffset splits each x category into one sub-band per value of a second field, so the bars of one category sit side by side. yOffset does the same inside each y category, for grouped barX. The offset needs a categorical x (or y) to divide. It has no axis, so colour the groups with fill on the same field and turn on its legend.
Sub-bands touch by default, and the gap between categories is the x band's padding. scale: { padding } on the offset channel opens a gap inside each group. yOffset puts its first value at the bottom of the slot, as a band y axis does.
A bar draws only for a row that names both a category and a group. Combine xOffset with stack and each sub-band stacks on its own. create() and toggle() read the group from where you click, and move({ channels: ["x", "xOffset"] }) drags a bar into another category and group.
Grouped bars
xOffset: { field: "year" } splits each region into three sub-bands. y carries edit: move().
const chart = Elicit({width: 400, height: 250,margins: { top: 16, right: 16, bottom: 28, left: 36 },data: [{ region: "North", year: "2022", sales: 40 }, { region: "North", year: "2023", sales: 55 },],schema: {region: { type: "categorical", domain: ["North", "South", "West"] },year: { type: "categorical", domain: ["2022", "2023", "2024"] },sales: { type: "quantitative", domain: [0, 80] },},marks: [barY({channels: {x: { field: "region" },xOffset: { field: "year" },y: { field: "sales", edit: move() },fill: { field: "year", legend: { anchor: "top", title: false } },},}),],});mount(chart.dom());
Fill in one group
context marks 2023 as given; you drag the 2024 bar beside it in each region.
// 2023 is given; you estimate 2024 beside it in every region.const regions = ["North", "South", "West", "East"];const given = { North: 55, South: 34, West: 47, East: 40 };const chart = Elicit({width: 400, height: 250,margins: { top: 16, right: 16, bottom: 28, left: 36 },data: regions.flatMap((region) => [{ region, year: "2023", sales: given[region] },{ region, year: "2024", sales: given[region] },]),context: (d) => d.year === "2023",schema: {region: { type: "categorical", domain: regions },year: { type: "categorical", domain: ["2023", "2024"] },sales: { type: "quantitative", domain: [0, 80] },},constraints: [clamp({ min: 0 })],marks: [barY({channels: {x: { field: "region" },xOffset: { field: "year", scale: { padding: 0.1 } },y: { field: "sales", edit: move() },fill: { field: "year", scale: { range: ["#BCC9D4", "#50AEB7"] },legend: { anchor: "top", title: false } },},}),],});mount(chart.dom());
Slicing a stack
A stacked bar divides one total among its rows. Three gestures reshape it, the same three that work on a pie or donut:
edit.stack.cut()— click inside a segment to divide it in two at that point.edit.stack.edge()— drag a boundary to move value across it. Only the two segments it separates change.edit.stack.merge()— double-click a boundary to fuse those two back into one.
All three preserve the band's total. Cutting divides a magnitude, dragging moves value from one row to its neighbour, and merging adds two into one. Boundary handles appear once one of these edits is set, and take the shared handles / handleSize / handleColor options. With pick: "nearest", a press near a boundary grabs it, which helps with thin segments. A focused boundary also takes the arrow keys.
A cut mints a row and writes its category into the stack field, taking a value from that field's schema domain, so building a stack takes no typing. The new segment takes a category that sorts next to the one you cut, so the other segments keep their places. A closed domain (the default) hands out the values that band doesn't yet use, and refuses the cut once they run out. Each band draws from its own copy of the domain, so bonds in 2023 and bonds in 2024 are both available.
Set open: true on the field's schema entry and the domain becomes a starting set instead: a cut appends a new category to it, named from label ("Category" by default) for you to rename later with edit.scale.categories(). An open field also skips the out-of-domain check on seed data.
Cut a stack into parts
Each year starts as one 100% block. The asset domain declares four categories, so each band takes four cuts.
const chart = Elicit({width: 380, height: 260,margins: { top: 16, right: 16, bottom: 28, left: 40 },data: [{ year: "2023", asset: "stocks", pct: 100 },{ year: "2024", asset: "stocks", pct: 100 },],schema: {year: { type: "categorical", domain: ["2023", "2024"] },asset: { type: "categorical", domain: ["stocks", "bonds", "cash", "gold"] },pct: { type: "quantitative", domain: [0, 100] },},legends: true,marks: [bar({channels: {x: { field: "year" },y: { field: "pct" },stack: { field: "asset" },fill: { field: "asset" },},edits: [edit.stack.cut(),edit.stack.edge(),edit.stack.merge(),],}),],});mount(chart.dom());
An open domain
holding is declared open, so each cut adds a category rather than running out. label: "Holding" names them.
const chart = Elicit({width: 380, height: 260,margins: { top: 16, right: 16, bottom: 28, left: 40 },data: [{ year: "2024", holding: "VTI", pct: 100 }],schema: {year: { type: "categorical", domain: ["2024"] },holding: { type: "categorical", domain: ["VTI"], open: true },pct: { type: "quantitative", domain: [0, 100] },},legends: true,marks: [bar({channels: {x: { field: "year" },y: { field: "pct" },stack: { field: "holding" },fill: { field: "holding" },},edits: [edit.stack.cut({ label: "Holding" }),edit.stack.edge(),edit.stack.merge(),],}),],});mount(chart.dom());
Explicit spans (x1/x2, y1/y2)
Give the value axis two endpoints, x1/x2 for barX or y1/y2 for barY, and each bar spans from one to the other instead of starting at the baseline. Use it for a range per category, as in a Gantt chart. x1/x2 share the scale of x (likewise y1/y2 with y), so only one needs an explicit type and domain.
Years active per person
barX with x1/x2 spans; y is the category band.
const chart = Elicit({width: 380, height: 220,margins: { top: 14, right: 16, bottom: 26, left: 60 },data: [{ person: "Ada", start: 1830, end: 1852 },],schema: {person: { type: "categorical", domain: ["Ada", "Grace", "Alan"] },start: { type: "quantitative", domain: [1820, 2000] },end: { type: "quantitative", domain: [1840, 2000] },},marks: [barX({fill: "#8A78C0",channels: {y: { field: "person" },x1: { field: "start" },x2: { field: "end" },},}),],});mount(chart.dom());
Brush: edges + body together
brushSpan() combines both: grab near an edge to resize just that end, grab the body to move the whole span. Drag an edge past the other and release — the fields re-sort (start stays ≤ end) with no visual jump.
const chart = Elicit({width: 380, height: 220,margins: { top: 14, right: 16, bottom: 26, left: 60 },data: [{ person: "Ada", start: 1830, end: 1852 },],schema: {person: { type: "categorical", domain: ["Ada", "Grace", "Alan"] },start: { type: "quantitative", domain: [1820, 2000] },end: { type: "quantitative", domain: [1840, 2000] },},marks: [barX({fill: "#50AEB7",channels: {y: { field: "person" },x1: { field: "start" },x2: { field: "end" },},edits: [brushSpan({ channels: ["x1", "x2"], threshold: 40,edgeInset: 10, guide: true }),],}),],});mount(chart.dom());
Editing bars
Put edit: move() on the value channel and drag a bar to rewrite its value. With pick: "nearest", you grab a bar from anywhere in its column, which helps with thin bars. A bar dragged to the baseline collapses to zero length but keeps a minimum grab target, with either pick mode, so you can drag it back up.
Drag a value
y carries edit: move(). Dragging writes y back through the same scale.
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 },],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() },},}),],});mount(chart.dom());
Nearest pick + self-drawn guide
pick: "nearest" grabs a bar from anywhere in its column; guide: true draws 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 },],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());