Axis, Grid & Rule
Axes and gridlines are chart elements: they draw a scale, not data. Configure them with the spec's axes: {…} option, or compose elements.axisX / elements.gridY yourself. ruleY is a data mark.
API reference
axes (on Elicit)
The implicit layer. Every key desugars into the axis and grid marks below, and an explicit one you compose yourself always wins over the injected one.
axes: false // no axes at all
axes: { x, y, count, origin } // per-channel config
axes: { y: { grid: true, ticks: 8 } } // any axis option| Option | Type · default | Description |
|---|---|---|
x / y | object | default{} | Options for that channel’s axis — any option in the table below. false suppresses the channel. |
count | boolean | defaultfalse | |
origin | booleandefaultauto | Cross both axes at zero, instead of the left/bottom frame. Inferred true when a trend is present and you wrote no axes at all; state it either way to override. |
The injected axis / grid marks, in the background layer.
axis · axisX · axisY
A spine, ticks, labels and an optional title for one channel’s scale. Import elements from elicitjs, or reach it through the axes option above.
axisX({ ticks, tickFormat, title, anchor, transform, grid, … }) → ChartElement| Option | Type · default | Description |
|---|---|---|
channel | 'x' | default'x' | Which scale to draw (pinned by axisX/axisY). |
anchor | 'bottom'|defaultper axis | Which side the axis sits on. |
transform | (ctx) => | Override the base translate — cross at the origin, or a centred 1D axis. |
ticks | numberdefault5 | Approximate tick count (linear scales). |
tickValues | any[] | Explicit tick values (overrides ticks). |
tickFormat | string | defaultauto | A d3-format string or a formatter function. A newline in a label breaks it onto a second line. |
tickSize | number | defaulttheme (6) | Tick mark length in pixels; 0 draws none. A function sets each tick's length. |
tickPadding | numberdefaulttheme (3) | Gap between a tick's end and its label, in pixels. |
labelAngle | numberdefaulttheme (0) | Tick-label rotation in degrees, counter-clockwise like a mark's angle. The axis reserves the room the turned labels need. |
title | string | Axis title, centred along the axis and pushed past the tick labels. A y title is rotated to run along its axis. |
titleAngle | numberdefault90 left · -90 right | Rotation of the title in degrees. 0 lays a y title flat. On an x axis the title is flat unless set. |
stroke / strokeWidth / line | styledefaulttheme (#6b7280 / 1 / true) | Spine and tick colour and width; line: false drops the spine. stroke also takes (v, i) => colour per tick. |
fill / fontSize / fontFamily / fontWeight | styledefaulttheme (#374151 / 10) | Label paint (labels are text nodes, so they take a fill). fill and fontWeight also take a per-tick function. |
titleFill / titleFontSize / titleFontWeight / titlePadding | styledefaulttheme (label fill / fontSize + 1 / — / 0) | The title's paint, and extra pixels between the labels and the title. |
labelAlign / labelOffset | 'start' | defaulttheme (side's own / 0) | A tick label's text-anchor, and pixels every label moves along the axis. With a negative tickPadding, labelAlign: 'start' on a left axis puts the labels inside the plot. |
titleAnchor | 'start' | defaulttheme ('middle') | Where the title sits along the axis; 'end' is the right of x, the top of y. A flat y title at an end sits past it, over the plot: leave room in that margin. |
offset | numberdefault0 | Move the axis this many pixels away from the plot. |
grid | boolean | defaultfalse | In the global axes config, also add a matching grid. An object styles it (strokeDash, zero, …). |
edit / edits | Edit / Edit[] | An axis is inert until you give it one. edit.axis.scale() drags the domain; see Editing the scale. |
field | stringdefaultthe scale's first field | Which column an edit on this axis writes, when the scale carries several. |
table | stringdefaultthe primary table | Which table that edit writes to, by name. |
handleColor / handleSize | string / numberdefaulttheme / 5 | The grip an axis edit draws. Inert axes draw none. |
id | string | Stable identity, as on every mark. |
A chart element emitting line + text nodes; redraws as the domain grows.
grid · gridX · gridY
Full-span gridlines across the plot, one per tick.
gridY({ ticks, tickValues, stroke, strokeWidth }) → ChartElement| Option | Type · default | Description |
|---|---|---|
channel | 'x' | default'x' | Which scale to draw lines for (pinned by gridX/gridY). |
ticks | numberdefault5 | Approximate line count. |
tickValues | any[] | Explicit positions (overrides ticks). |
stroke / strokeWidth / strokeDash / opacity | styledefaulttheme (#e5e7eb / 1) | Line paint. stroke, strokeWidth and strokeDash also take (v, i) => value per line. |
zero | boolean | defaulttheme (none) | An emphasised line at 0 on a continuous scale. true takes the theme's grid.zero style. |
bandPosition | numberdefaulttheme (0.5) | On a band axis, where in each band a line sits: 0 its start edge, so the lines run between the categories. |
id | string | Stable identity, as on every mark. |
A chart element emitting one full-span line per tick.
rule · ruleX · ruleY
A straight reference line at a value on one axis — or, with a pair of endpoint channels, a span segment (a lollipop stem / error-bar whisker) at a category. An ordinary editable mark: put an edit on an endpoint channel and its cap becomes a handle. A rule with no edit is left pointerEvents:"none" by the engine, so an inert whisker never swallows a sibling handle’s drag.
ruleY({ channels: { y: { datum: 50 } } }) → Mark // reference line at y = 50
ruleX({ channels: { x, y1, y2 } }) → Mark // span per datum| Option | Type · default | Description |
|---|---|---|
channels | objectdefault{} | The value channel — { datum } for a constant, { field } for one rule per row. y1/y2 or x1/x2 switch to span mode. |
orientation | 'vertical' | defaultauto | Which axis carries the reference value ( 'vertical' = y). Inferred from the bound channel; ruleY / ruleX pin it. |
edits | Edit[] | As on any mark. A per-channel edit makes that endpoint draggable. |
discreteScale | 'band' | | Which variant of a categorical axis the rule sits on. Left unset so a composite can stamp its own. |
strokeDash | string | Dash pattern passthrough. |
stroke, | styledefaultstroke:'black' | Standard style surface. |
| Channel | Type | Description |
|---|---|---|
x / y | datum | | The reference value axis. { datum: 50 } is DATA space — it goes through the scale. ({ value: 50 } would be 50 pixels.) |
y1 / y2 | linear | Span endpoints (vertical). A lone endpoint spans from the value-axis baseline. |
x1 / x2 | linear | Span endpoints (horizontal). |
A mark emitting one non-interactive line per rule (full-extent, or a span segment).
Chart elements are not data marks
axis, grid, legend and axisRadial are members of elements. Pass them in elements: […] on the spec, or in marks. See Mark & element contracts.
A data mark views the dataset's columns through a channels map. A chart element views one scale, named by a singular channel ('x', 'fill', 'theta'). It has no fields of its own. axisRadial is the exception: it can take x/y channels to place one ring per row.
An element's stroke, fill and fontSize paint its spine and labels, so they are plain options, not channels.
An edit on a data mark writes a row. An edit on a chart element writes the domain (edit.axis.*), so create doesn't apply to an element. The edit takes its field from the scale, which is how a legend picker knows what to write.
A legend reserves space on its side, and so do an axis title and turned or multi-line tick labels. The plot shrinks to fit.
Both halves have an implicit layer on the spec: axes for the positional scales, legends for the rest.
elements and ElicitSpec.elements
Compose axes with elements.axisX, elements.axisY and elements.gridY. Pass them in elements: […] on the spec, or in marks. A composed axis replaces that channel's implicit axis. A composed grid replaces only the implicit grid, so the axis still draws unless axes turns it off.
elements: […] with barY
axes: false, then explicit elements.axisX / axisY / gridY. The bar is the only data mark.
// Chart elements via elicit.elements + ElicitSpec.elements (also still work in marks).const chart = Elicit({width: 420, height: 220,margins: { top: 16, right: 16, bottom: 42, left: 52 },schema: {cat: { type: 'categorical', domain: ['A', 'B', 'C'] },n: { type: 'quantitative', domain: [0, 100] },},data: [{ cat: 'A', n: 40 },],axes: false,elements: [elements.axisX({ ticks: 3, title: 'category' }),elements.axisY({ ticks: 4, title: 'n' }),elements.gridY(),],marks: [barY({channels: {x: { field: 'cat' },y: { field: 'n', edit: move() },},}),],});mount(chart.dom());
The global axes:{} convenience
Pass a per-channel config to shape ticks, formats and titles, or a transform to reposition an axis. axes: false drops them entirely.
origin: true crosses both axes at zero, in place of a transform on each channel. A chart with a trend mark gets this frame by default when axes is unset. origin: false forces the ordinary left/bottom frame. Use a per-channel transform to cross at a value other than zero, or on one axis only.
Gridlines, ticks, format & title
y gets a grid and percent formatting; x a tick count and a title.
const chart = Elicit({width: 380, height: 300,margins: { top: 16, right: 16, bottom: 42, left: 62 },axes: {x: { ticks: 4, title: "Funnel step" },y: { grid: true, tickFormat: ".0%", title: "Conversion" },},data: [{ step: "Visit", rate: 1.0 }, { step: "Signup", rate: 0.62 },{ step: "Active", rate: 0.41 }, { step: "Paid", rate: 0.18 },],schema: {step: { type: "categorical", domain: ["Visit", "Signup", "Active", "Paid"] },rate: { type: "quantitative", domain: [0, 1] },},marks: [bar({channels: {x: { field: "step" },y: { field: "rate" },},}),],});mount(chart.dom());
Origin-crossing axes
axes: { origin: true } crosses both scales at zero, without a hand-written transform.
const chart = Elicit({width: 360, height: 300,margins: { top: 16, right: 16, bottom: 24, left: 28 },axes: {origin: true,x: { ticks: 5 },y: { ticks: 5 },},data: [{ x: -8, y: 6 }, { x: 5, y: -3 }, { x: -4, y: -7 },{ x: 9, y: 4 }, { x: 2, y: 8 }, { x: -6, y: 2 },],schema: {x: { type: "quantitative", domain: [-10, 10] },y: { type: "quantitative", domain: [-10, 10] },},marks: [point({size: 5, fill: "#50AEB7",channels: {x: { field: "x" },y: { field: "y" },},}),],});mount(chart.dom());
1D centred slider axis
One point channel; the axis is pinned to the vertical center via transform, and y is dropped.
const chart = Elicit({width: 360, height: 200,margins: { top: 16, right: 20, bottom: 24, left: 20 },axes: {x: { transform: ({ height }) => ({ y: height / 2 }), ticks: 5, title: "Belief (0-100)" },y: false, // drop the default y axis},data: [{ v: 20 }, { v: 55 }, { v: 80 }],schema: {v: { type: "quantitative", domain: [0, 100] },},marks: [point({size: 8, fill: "#1B3F60",channels: {x: { field: "v" },},edits: [ move({ channels: ["x"] }) ],}),],});mount(chart.dom());
Axis titles and band order
title labels an axis. An x title sits centred below the tick labels. A y title runs along the axis, reading bottom to top on the left, and sits past the widest tick label, so it never crosses one. The plot shrinks to make room for it.
A band or point y axis lists domain[0] at the bottom, the same direction a value increases. Set scale: { reverse: true } on the channel to list the categories top-down, as Vega-Lite and Observable Plot do for a horizontal bar chart.
titleAngle sets the title's rotation in degrees. titleAngle: 0 lays a y title flat, for a short unit label.
Titles along the axis
axes: { x: { title }, y: { title } }; scale: { reverse: true } on the band y lists Sleep first.
const chart = Elicit({width: 400, height: 240,margins: { top: 12, right: 16, bottom: 40, left: 96 },axes: {x: { title: "Hours per week" },// The y title runs along the axis, past the widest label.y: { title: "Activity" },},data: [{ activity: "Sleep", h: 52 }, { activity: "Work", h: 40 },{ activity: "Screens", h: 24 }, { activity: "Exercise", h: 4 },],schema: {activity: { type: "categorical", domain: ["Sleep", "Work", "Screens", "Exercise"] },h: { type: "quantitative", domain: [0, 60] },},marks: [barX({fill: "#1B3F60",channels: {// A band y axis puts domain[0] at the bottom; reverse lists it top-down.y: { field: "activity", scale: { reverse: true } },x: { field: "h", edit: move() },},}),],});mount(chart.dom());
A flat y title
axes: { y: { title: "°C", titleAngle: 0 } }
const chart = Elicit({width: 400, height: 220,margins: { top: 14, right: 16, bottom: 28, left: 58 },axes: { y: { title: "°C", titleAngle: 0 } },data: [{ m: 1, t: 4 }, { m: 2, t: 6 }, { m: 3, t: 10 }, { m: 4, t: 14 },{ m: 5, t: 18 }, { m: 6, t: 22 },],schema: {m: { type: "quantitative", domain: [1, 6] },t: { type: "quantitative", domain: [0, 30] },},marks: [lineY({stroke: "#EE7B5E", strokeWidth: 2,channels: { x: { field: "m" }, y: { field: "t", edit: move() } },}),],});mount(chart.dom());
Styling an axis
Every axis option below defaults to the theme's axis token of the same name, so set it once in a theme or here on one axis. line: false drops the spine and tickSize: 0 the ticks. tickPadding sets the gap from tick to label, and offset moves the whole axis away from the plot. labelAngle turns the tick labels, and the axis reserves the room they need. A \n in a formatted label starts a second line.
tickSize, stroke, fill and fontWeight also take a function of the tick's value and index, and so do a grid's stroke, strokeWidth and strokeDash. Use it to mark a period boundary.
labelAlign sets a label's text-anchor and labelOffset moves every label along the axis. A negative tickPadding with labelAlign: "start" puts a y axis's labels inside the plot, and a grid's bandPosition: 0 draws its lines between the categories. titleAnchor places the title along the axis; "end" with titleAngle: 0 sets a y title at the top left, over the plot, so leave room in margins.top.
Turned labels
axes: { x: { labelAngle: 40, tickSize: 0 }, y: { grid: { strokeDash: "2 3" } } }
// Turned labels and two-line labels. `labelAngle` turns the tick labels (the// axis makes room for them); a `\n` in a formatted label starts a second line.const chart = Elicit({width: 460, height: 280,margins: { top: 16, right: 16, bottom: 20, left: 40 },axes: {x: { labelAngle: 40, tickSize: 0 },y: { grid: { strokeDash: "2 3" } },},data: [{ brand: "Northwind Traders", v: 42 }, { brand: "Contoso", v: 30 },],schema: {brand: { type: "categorical", domain: ["Northwind Traders", "Contoso", "Fabrikam Fiber", "Adventure Works", "Tailspin Toys"] },v: { type: "quantitative", domain: [0, 50] },},marks: [barY({ channels: { x: { field: "brand" }, y: { field: "v", edit: move() } } })],});mount(chart.dom());
Per-tick styling
Long ticks, bold two-line labels and solid gridlines on each January.
// Per-tick styling: a long tick, a bold two-line label and a solid gridline on// each January; short ticks and dashed lines elsewhere.const months = d3.timeMonths(new Date(2006, 0, 1), new Date(2008, 0, 2));const isJan = (d) => d.getMonth() === 0;const chart = Elicit({width: 520, height: 260,margins: { top: 16, right: 20, bottom: 20, left: 40 },axes: {x: {tickValues: months,tickFormat: (d) => (isJan(d) ? d3.timeFormat("%b\n%Y")(d) : d3.timeFormat("%b")(d)[0]),tickSize: (d) => (isJan(d) ? 14 : 4),fontWeight: (d) => (isJan(d) ? "bold" : null),line: false,// The paired grid borrows the axis's tickValues; its dash is per line too.grid: { strokeDash: (d) => (isJan(d) ? null : "2 3") },},},data: months.map((date, i) => ({ date, price: 420 + 140 * Math.sin(i / 4) + i * 8 })),schema: {date: { type: "temporal", domain: [months[0], months[months.length - 1]] },price: { type: "quantitative", domain: [200, 800] },},marks: [lineY({ channels: { x: { field: "date" }, y: { field: "price", edit: move() } }, handles: true })],});mount(chart.dom());
A space-saving axis
Labels above each bar, gridlines between the bands, the title at the top left.
// A space-saving y axis: no spine or ticks, labels moved inside the plot above// each bar, gridlines between the bands, and a flat title at the top left.const chart = Elicit({width: 420, height: 230,margins: { top: 22, right: 12, bottom: 34, left: 6 },data: [{ team: "Design", n: 12 }, { team: "Engineering", n: 31 }, { team: "Research", n: 8 }],schema: {team: { type: "categorical", domain: ["Research", "Engineering", "Design"] },n: { type: "quantitative", domain: [0, 40] },},axes: {x: { title: "Headcount" },y: {line: false, tickSize: 0,tickPadding: -5, labelAlign: "start", labelOffset: -18,title: "Team", titleAngle: 0, titleAnchor: "end",grid: { bandPosition: 0 },},},marks: [rectX({ cornerRadius: 2, channels: {y: { field: "team", scale: { padding: 0 } },x: { field: "n", edit: move() },height: { value: 22 },} }),],});mount(chart.dom());
Axis, grid & rule as marks
Turn the global axes off (axes: false) and compose axes, grids and rules in marks yourself. They draw in the order you list them, interleaved with your data marks.
Explicit gridY + axisX/axisY + ruleY
A gridline layer under the dots, a dashed rule at y = 50, and two titled axes — all as marks.
const chart = Elicit({width: 380, height: 280,margins: { top: 16, right: 16, bottom: 42, left: 50 },axes: false, // no auto axes; we place our owndata: [{ x: 15, y: 30 }, { x: 40, y: 62 }, { x: 65, y: 44 }, { x: 88, y: 74 }],schema: {x: { type: "quantitative", domain: [0, 100] },y: { type: "quantitative", domain: [0, 100] },},marks: [gridY({ ticks: 5 }),// `datum` is a DATA-space constant: it goes through the y scale, so the// line lands where y = 50 is. (`value` would mean 50 pixels.)ruleY({ channels: { y: { datum: 50 } }, stroke: "#EE7B5E", strokeDash: "4 3" }),point({fill: "#50AEB7",size: 8,channels: {x: { field: "x" },y: { field: "y" },},}),axisX({ title: "x", ticks: 5 }),axisY({ title: "y", ticks: 5 }),],});mount(chart.dom());