Skip to content

rect ​

Draws a rectangle for each data item.

js
gf.chart([{ value: 80 }])
  .mark(gf.rect({ w: 120, h: "value", fill: "steelblue", rx: 4 }))
  .render(root, { w: 200, h: 150 });

Signature ​

ts
rect({ w?, h?, dims?, fill?, stroke?, strokeWidth = 0, rx?, ry?, inset? })

Parameters ​

A rectangle. Box geometry via the shared dims channels.

OptionTypeDefaultDescription
wnumber | string | FieldExpr | LengthWidth. Also percent(n), n percent of the room it is laid out in (its parent's, or the canvas), or fr(k), k shares of the room its nearest scope divides.
hnumber | string | FieldExpr | LengthHeight. Also percent(n), n percent of the room it is laid out in (its parent's, or the canvas), or fr(k), k shares of the room its nearest scope divides.
filterFilter | stringA filter over this whole drawing: a Filter strategy (Filter.grayscale(), Filter.luminanceToAlpha()) or a raw CSS filter string ("blur(2px)", "url(#shadow)").
keystringInternal per-node key override.
rxnumberCorner radius, x. Default: ry, or 0 when neither is given.
rynumberCorner radius, y. Default: rx, or 0 when neither is given.
aspectRationumberw/h ratio to enforce; the constraining axis wins when both are data-driven.
insetnumber | Length0Pixels drawn in from each side of the space the rect fills, on an axis where it has no size or span of its own (a bar's width in a spread or a partition cell), so neighbors are drawn apart. A side the rect sizes itself, such as a bar's data height, is never inset. It changes only what is drawn: the rect's box stays the space it was given. Also percent(n), n percent of that space on each axis: percent(10) draws a bar at 80% of its band. The percent and any pixels added to it are each 0 or more.
debugbooleanDev-only flag: logs this mark's key and datum to the console as it is built. It changes nothing about what is drawn.

Box dimensions ​

OptionTypeDefaultDescription
xnumber | string | FieldExprLeft edge position.
cxnumber | string | FieldExprCenter x.
x2number | string | FieldExprRight edge position.
emXbooleanEmbed x in the parent's x space.
ynumber | string | FieldExprStart edge on y: the top edge where y reads top-down, the bottom edge where it grows upward.
cynumber | string | FieldExprCenter y.
y2number | string | FieldExprOther y edge position.
emYbooleanEmbed y in the parent's y space.
dimsRecord<string, AxisDimsValue>Box dimensions by axis name: x/y, or a name the enclosing coordinate space declares (polar theta/r, geo lon/lat). Each value is a position (like x) or an interval {min, center, max, size, embedded}.

Paint ​

OptionTypeDefaultDescription
fillstring | FieldExprFill color, or a field name for a color scale.
strokestring | FieldExprStroke color. Defaults to fill.
strokeWidthnumber0Stroke width in pixels.
opacitynumber1Opacity, 0 to 1.

Chain .label(accessor, ...) on the returned mark to attach a text label — there is no label option here.

Axis names ​

x, y, w, and h mean the first and second axis in any coordinate space. Under polar that is the angle and the radius; under geo it is longitude and latitude.

To use the names a coordinate space gives its axes, put them in dims. Each key is an axis name, and each value is either a position (like x) or an object with any of min, center, max, size, and embedded:

ts
// Inside Coord.polar(): the same wedge as rect({ w: 0.4, h: "value" })
rect({ dims: { theta: { size: 0.4 }, r: { size: "value" } } });

// Inside Coord.geo(): the same box as rect({ x: "lon", y: "lat", w: 4, h: 4 })
rect({ dims: { lon: "lon", lat: "lat" }, w: 4, h: 4 });

x and y are always valid keys too. A name the enclosing coordinate space does not declare throws an error that lists the ones it does, and setting the same part of an axis twice (for example w and dims.theta.size) is an error. The same dims option works on ellipse, petal, text, image, and layer.

Examples ​

ts
// Bar chart: height encodes "value" field
.mark(rect({ h: "value" }))

// Fixed size with rounded corners
.mark(rect({ w: 20, h: 20, rx: 4 }))

// Color encodes "category" field
.mark(rect({ h: "value", fill: "category" }))

// Named for use with selectAll()
.mark(rect({ h: "value" }).name("bars"))