Rect
rect draws a rectangle whose extent on each axis resolves separately: a span (x1/x2, y1/y2), a fixed pixel size (width/height) centred on an anchor, a categorical band, or a baseline→value length. Use it for heatmap cells, 2-D regions, binned histograms and annotation boxes, and edit it with brushRect(). rectX / rectY force one axis to a baseline→value length. When one axis is a band and the other a value, use bar.
API reference
rect(options) · rectX(options) · rectY(options)
Import plot from elicitjs. rect spans both axes; rectX forces value on x (y a span/band), rectY forces value on y. All three share these options.
rect({ channels, orientation, width, height, cornerRadius, edits, id }) → Mark
rectX(options) → Mark // value on x
rectY(options) → Mark // value on y| Option | Type · default | Description |
|---|---|---|
channels | objectdefault{} | Channel map — spans (x1/x2, y1/y2), bands, or single x/y values per axis. 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' | | Draws one axis from the baseline to the value ( 'vertical' = y). rectY / rectX set it. Bare rect reads each axis from its own channels: a span, a band or a value. |
width / height | number | Fixed pixel extent for every rect, centred on its x/y anchor. Overrides the band cell and the baseline→value span. Also available per datum as channels. |
cornerRadius | numberdefaulttheme (0) | Corner radius in px. Also available per datum as a cornerRadius channel. Defaults to the theme’s marks.rect.cornerRadius. |
gradient | { stops, | A linear-gradient fill over each rect’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). Same option as on bar and area. |
edits | Edit[] | Mark-level edits; per-channel edits live in channels[ch].edit. Use brushRect for 2-D edge/corner/body editing. |
fill, | styledefaultfill: 'steelblue' | Style shorthands / channels (the shared style surface). |
| Channel | Type | Description |
|---|---|---|
x1 / x2 | linear | Horizontal span endpoints. Share x’s resolved scale. With one end given, the other is the baseline. |
y1 / y2 | linear | Vertical span endpoints. Share y’s resolved scale. With one end given, the other is the baseline. |
x / y | band | | A single value (baseline→value), or a band cell when that axis is categorical. For a heatmap, add scale: { padding: 0 } so cells touch. |
width / height | px | Explicit pixel extent — the rect becomes a fixed-size box centred on its x/y anchor. Overrides band and baseline→value. |
fill, | const | | Standard style surface; a field tints through the ordinal palette. |
A mark emitting one rect per datum.
A 2-D rectangle (both axes are spans)
Give both axes explicit endpoints and each row draws a rectangle from x1 to x2 and y1 to y2.
Region boxes
x1/x2 and y1/y2 place each rect; fill by a field.
const chart = Elicit({width: 360, height: 260,margins: { top: 16, right: 16, bottom: 28, left: 34 },data: [{ x1: 1, x2: 4, y1: 1, y2: 3, kind: "a" },],schema: {x1: { type: "quantitative", domain: [0, 10] },x2: { type: "quantitative", domain: [0, 10] },y1: { type: "quantitative", domain: [0, 10] },y2: { type: "quantitative", domain: [0, 10] },kind: { type: "categorical", domain: ["a", "b"] },},marks: [rect({fillOpacity: 0.6,channels: {x1: { field: "x1" }, x2: { field: "x2" },y1: { field: "y1" }, y2: { field: "y2" },fill: { field: "kind" },},}),],});mount(chart.dom());
Heatmap (both axes categorical)
Put a category on both axes and each row draws one cell at that (x, y) intersection, giving a heatmap. A quantitative fill resolves to a sequential colour ramp, a categorical fill to a palette. Band scales pad by default; set scale: { padding: 0 } on each axis so the cells touch. A drag can't invert a colour, so edit a cell's value through the legend: select() on the cells makes the one you click the target row, and legendColor({ edit: edit.legend.value() }) drags that row's value along the ramp.
Coloured cells
A category on x and y; fill by a quantitative field; padding 0 so cells touch.
const chart = Elicit({width: 420, height: 260,margins: { top: 16, right: 84, bottom: 30, left: 56 },data: [{ day: "Mon", slot: "AM", load: 4 }, { day: "Mon", slot: "PM", load: 9 }, { day: "Mon", slot: "Eve", load: 6 },],schema: {day: { type: "categorical", domain: ["Mon", "Tue", "Wed", "Thu"] },slot: { type: "categorical", domain: ["AM", "PM", "Eve"] },load: { type: "quantitative", domain: [0, 20] },},marks: [// A category on BOTH axes draws one cell per (day, slot). `scale: { padding: 0 }`// removes the band gap so the cells touch, as a heatmap wants. A quantitative// `fill` resolves to a sequential colour ramp.// `select()` makes a cell the target row, so the legend edits the one you click.rect({edits: [ edit.select() ],channels: {x: { field: "day", scale: { padding: 0 } },y: { field: "slot", scale: { padding: 0 } },fill: { field: "load" },},}),// Colour isn't gesture-invertible, so the ramp handle sets the value by hand.// It tracks the selected cell — click a cell, then drag the handle to set it.legendColor({ anchor: "right", title: "load", edit: edit.legend.value() }),],});mount(chart.dom());
Fixed-size boxes (width/height)
Give an axis a pixel extent with width (x) or height (y) and the rect becomes a fixed-size box centred on its x/y anchor. An explicit size overrides the band interval and the baseline→value length. The anchor is a positional channel, so a move() on x and y moves the whole box.
Boxes at data points
x/y anchor each box; width/height set a constant 28×28 px size.
const chart = Elicit({width: 420, height: 260,margins: { top: 16, right: 16, bottom: 30, left: 40 },data: [{ x: 1, y: 8, tag: "a" },],schema: {x: { type: "quantitative", domain: [0, 8] },y: { type: "quantitative", domain: [0, 10] },tag: { type: "categorical", domain: ["a", "b", "c", "d"] },},marks: [// `width` / `height` are pixel extents, so each rect is a fixed 28x28 box// centred on its (x, y) anchor — the SVG-native form, alongside x1/x2 spans// and band cells. The anchor stays a positional channel: a drag on x and y// inverts the pointer through both scales, so the whole box moves.rect({fill: "#50AEB7", fillOpacity: 0.8,channels: {x: { field: "x", edit: edit.move() },y: { field: "y", edit: edit.move() },width: { value: 28 },height: { value: 28 },},}),],});mount(chart.dom());
Binned histogram (rectY)
rectY draws a histogram: x is a quantitative bin span (x1/x2) and y is a count from the baseline. Pass data already binned, one row per bin. Use barY when bins are categorical labels instead of numeric edges.
Pre-binned counts
x1/x2 place each bin on a shared quantitative x; y is the count.
const chart = Elicit({width: 400, height: 260,margins: { top: 16, right: 16, bottom: 28, left: 34 },data: [{ x1: 0, x2: 2, n: 4 },],schema: {x1: { type: "quantitative", domain: [0, 10] },x2: { type: "quantitative", domain: [0, 10] },n: { type: "quantitative", domain: [0, 24] },},marks: [rectY({fill: "#50AEB7", fillOpacity: 0.75,channels: {x1: { field: "x1" },x2: { field: "x2" },y: { field: "n" },},}),],});mount(chart.dom());
Editable bin heights
Drag on y to reshape the distribution; clamp keeps counts ≥ 0.
const chart = Elicit({width: 400, height: 260,margins: { top: 16, right: 16, bottom: 28, left: 34 },data: [{ x1: 0, x2: 2, n: 4 },],schema: {x1: { type: "quantitative", domain: [0, 10] },x2: { type: "quantitative", domain: [0, 10] },n: { type: "quantitative", domain: [0, 24] },},constraints: [clamp({ min: 0, max: 24, field: "n" })],marks: [rectY({fill: "#1B3F60", fillOpacity: 0.75,channels: {x1: { field: "x1" },x2: { field: "x2" },y: { field: "n", edit: move({ guide: true }) },},}),],});mount(chart.dom());
Composable 2-D editing (brushRect)
brushRect() edits all four extents. Grab an edge to resize one side, a corner to resize two extents, or the body to move the whole rect. resize ('both' | 'x' | 'y' | 'none') sets which axes resize. move (boolean) turns the body drag on or off. Together they give resize-one-axis, move-only and resize-only variants.
Full 2-D: edges, corners, body
Default brushRect(): every direction live.
const chart = Elicit({width: 360, height: 260,margins: { top: 16, right: 16, bottom: 28, left: 34 },data: [ { x1: 2, x2: 6, y1: 2, y2: 6 } ],schema: {x1: { type: "quantitative", domain: [0, 10] },x2: { type: "quantitative", domain: [0, 10] },y1: { type: "quantitative", domain: [0, 10] },y2: { type: "quantitative", domain: [0, 10] },},marks: [rect({fill: "#50AEB7", fillOpacity: 0.5,channels: {x1: { field: "x1" }, x2: { field: "x2" },y1: { field: "y1" }, y2: { field: "y2" },},edits: [ brushRect({ edgeInset: 12 }) ],}),],});mount(chart.dom());
Resize x only
brushRect({ resize: "x" }) — only the vertical edges resize; body still moves.
const chart = Elicit({width: 360, height: 260,margins: { top: 16, right: 16, bottom: 28, left: 34 },data: [ { x1: 2, x2: 6, y1: 2, y2: 6 } ],schema: {x1: { type: "quantitative", domain: [0, 10] },x2: { type: "quantitative", domain: [0, 10] },y1: { type: "quantitative", domain: [0, 10] },y2: { type: "quantitative", domain: [0, 10] },},marks: [rect({fill: "#1B3F60", fillOpacity: 0.5,channels: {x1: { field: "x1" }, x2: { field: "x2" },y1: { field: "y1" }, y2: { field: "y2" },},edits: [ brushRect({ resize: "x", edgeInset: 12 }) ],}),],});mount(chart.dom());
Move only
brushRect({ resize: "none" }) — no edges grab; the whole rect translates.
const chart = Elicit({width: 360, height: 260,margins: { top: 16, right: 16, bottom: 28, left: 34 },data: [ { x1: 3, x2: 7, y1: 3, y2: 7 } ],schema: {x1: { type: "quantitative", domain: [0, 10] },x2: { type: "quantitative", domain: [0, 10] },y1: { type: "quantitative", domain: [0, 10] },y2: { type: "quantitative", domain: [0, 10] },},marks: [rect({fill: "#8A78C0", fillOpacity: 0.5,channels: {x1: { field: "x1" }, x2: { field: "x2" },y1: { field: "y1" }, y2: { field: "y2" },},edits: [ brushRect({ resize: "none" }) ],}),],});mount(chart.dom());
Creating rects
create() works on a rect as on any data mark (see Create · Remove · Anchors). A click gives one point and a rect needs a size, so the creator must seed the extent. A rectY-style rect (band or baseline→value length) takes its size from the axis and creates like a bar. A 2-D span rect (x1/x2, y1/y2) needs the opposite corner from create({ defaults: { x2, y2 } }); brushRect() then shapes it. Without an extent, the new row mints at zero size, invisible, and logs a warning. On a map, edit.geo.createRect() mints a box sized from default spans around the click.