Scales & channels
A data type says what a field is: quantitative, categorical, ordinal or temporal. You declare it once, on the schema. A scale type says how a channel draws it: linear, log, band, ordinal, and so on. The scale type is normally derived. A categorical field on a bar’s x is a band, because a bar needs an interval; on a dot’s x it is a point, because a dot sits on a tick. When you do name a scale, scale takes a name, a spec, or a live d3 scale. Every positional scale is invertible, which is what makes editing possible.
API reference
Channel spec
Each entry in a mark’s channels is one channel: a field, a constant, or a raw field — plus an optional co-located edit. No domain here; a domain belongs to the data, not to a mark’s view of it.
| Option | Type · default | Description |
|---|---|---|
field | string | The data field to read and map through the channel’s scale. |
value | any | A visual-space constant — it skips the scale. fill: "red" desugars to this. |
datum | any | A data-space constant — it goes through the scale. y: { datum: 25 } lands where y = 25 is, not at pixel 25. |
fn | (d, | A derived channel, computed per datum in visual space — the result is used as-is, never scaled. fill: d => … is the shorthand. It is read-only: a derived channel cannot carry an edit, because it is opaque to the edit layer. Put the edit on the source field’s channel; the function re-derives from the committed rows on every render. |
type | MeasureTypedefaultfrom schema | The field’s data type. An override for a field the schema doesn’t cover — normally you declare it once on the schema instead. |
scale | string | defaultderived | How to draw it. null passes the field through unscaled (a literal colour / pixel). See Scale forms. |
frame | number | | Position this channel locally, inside the enclosing composite’s box, and switch that composite into box mode. Shorthand for a frame scale. |
legend | boolean | | Draw a key for this encoding. Non-positional channels only — an axis is the key for x and y. See Legend. |
edit | Edit | Co-locate an edit so a gesture writes this channel back through the same scale. |
The engine resolves one global scale per channel, unioning across marks and across x/x1/x2 and y/y1/y2 — including their schema domains, so an error bar’s mean, lo and hi share one y axis spanning all three. Scales reach build as scales.x, scales.y, ….
Scale forms
The three ways to name a scale, when the derived one isn’t what you want. A d3 scale is adopted as you built it; for a positional channel we hand it the plot’s pixel range (pixels are ours to know, palettes and radii are yours).
scale: "log" // by name
scale: { type: "sqrt", range: [4, 20] } // by spec
scale: d3.scaleBand().padding(0.3) // a live d3 scale
scale: null // unscaled passthrough| Option | Type · default | Description |
|---|---|---|
ScaleSpec.type | ScaleTypedefaultderived | linear, log, symlog, pow, sqrt, time, band, point, ordinal, sequential, diverging. |
ScaleSpec.constant | numberdefault1 | For symlog — how wide the linear region around zero is. symlog is log for a domain that crosses zero or goes negative, where plain log has no answer. It stays continuous and invertible, so it drags. |
ScaleSpec.pivot | numberdefault0, | For diverging — the data value that takes the middle colour, with each side scaled independently so the pivot keeps its colour even on a lopsided domain like [-2, 10]. Defaults to 0 when the domain straddles it. Like sequential, it is a colour scale: not invertible, so it can’t carry an edit. |
ScaleSpec.range | any[]defaultfrom geometry | The output extent — pixels, radii, colours. Positional ranges default to the plot size. |
ScaleSpec.scheme | string | A named colour palette ( "RdBu", "Tableau10", …), used when no explicit range is given. An explicit range still wins. |
ScaleSpec.reverse | booleandefaultfalse | Flip the range end for end — a scheme read the other way, or an axis that counts down. |
ScaleSpec.padding | numberdefault0.1 / 0.5 | Band / point padding. |
ScaleSpec.nice / clamp | booleandefaultfalse | Continuous-scale refinements. |
ScaleSpec.base / exponent | numberdefault10 / 1 | For log and pow. |
spec.scales | Record<channel, | The chart-level override, keyed by channel. Scales are global, so this is their honest home; it wins over a channel’s own scale. |
A scale carries what it can do — kind (band | point | continuous | discrete) and invertible — sniffed from the scale object itself. Marks and edits branch on that, never on a type name, which is why an adopted d3 scale drags exactly like a built-in one. On a map, lon and lat are paired: the projection places them together, so chart.scale('lon') encodes a longitude with its latitude and inverts a whole point.
The scale is derived from the data type
Nothing below names a scale. The schema says cat is categorical and n is quantitative; the bar says it needs an interval for discrete data. Band and linear fall out. Swap bar for point and x becomes a point scale, with no other change.
No scale named anywhere
Data type (schema) + what the mark needs = the scale. Drag a bar to see it invert.
const chart = Elicit({width: 340, height: 240,margins: { top: 14, right: 14, bottom: 26, left: 30 },schema: {cat: { type: "categorical", domain: ["A", "B", "C", "D"] },n: { type: "quantitative", domain: [0, 60] },},data: [{ cat: "A", n: 34 }, { cat: "B", n: 58 },{ cat: "C", n: 22 }, { cat: "D", n: 47 },],marks: [bar({fill: "#50AEB7",channels: {x: { field: "cat" }, // categorical + bar -> bandy: { field: "n", edit: move() }, // quantitative -> linear},}),],});mount(chart.dom());
Continuous → colour & size
The same numeric field through a colour channel becomes a ramp, and through the size channel a radius. A size channel’s output range (radii, in px) is a property of the scale, so it rides on scale.
Sequential ramp + size
fill: { field } → ramp, size: { field } → radius, from one numeric field.
const chart = Elicit({width: 340, height: 240,margins: { top: 14, right: 14, bottom: 26, left: 34 },schema: {h: { type: "quantitative", domain: [140, 200] },weight: { type: "quantitative", domain: [40, 100] },},data: [{ h: 150, weight: 48 }, { h: 160, weight: 57 }, { h: 170, weight: 66 },{ h: 180, weight: 78 }, { h: 190, weight: 92 }, { h: 165, weight: 61 },],marks: [point({channels: {x: { field: "h" },y: { field: "weight" },fill: { field: "weight" }, // -> sequential rampsize: { field: "weight", scale: { range: [4, 16] } }, // -> radius, in px},}),],});mount(chart.dom());
Crossing zero — symlog & diverging
Elicited quantities are often differences: a revision, an error, a surprise. They can span orders of magnitude in both directions. log is undefined at 0 and below, and a sequential ramp has no reference point. symlog is linear near zero and logarithmic beyond it. diverging colours by distance from a pivot, scaling each side on its own.
symlog position, diverging colour
A domain that crosses zero — where log has no answer and a sequential ramp has no reference.
const chart = Elicit({width: 440, height: 220,margins: { top: 16, right: 24, bottom: 34, left: 88 },schema: {metric: { type: "categorical", domain: ["Signups", "Revenue", "Churn", "Support load"] },delta: { type: "quantitative", domain: [-100, 1000] },},data: [{ metric: "Signups", delta: 400 },],marks: [point({size: 9,channels: {y: { field: "metric" },// symlog: the deltas span orders of magnitude AND cross zero, where// plain `log` has no answer. `constant` sets how wide the linear// region around zero is. Still continuous and invertible — it drags.x: { field: "delta",scale: { type: "symlog", constant: 10 },edit: move() },// diverging: colour by distance from a PIVOT. Each side is scaled on// its own, so 0 keeps the neutral colour even though the domain is// lopsided (-100 vs 1000).fill: { field: "delta",scale: { type: "diverging", pivot: 0,range: ["#EE7B5E", "#EAF0F4", "#50AEB7"] } },},}),// The zero line, through the symlog scale (`datum`, not `value` — `value`// would put it 0 pixels from the left edge).ruleX({ channels: { x: { datum: 0 } }, stroke: "#94A5B3", strokeDash: "3 3" }),],});mount(chart.dom());
Naming a scale: by name, by spec, by d3
scale takes three forms: a name (log x), a spec (a sqrt size range), or a d3 scale (a band with custom padding). A d3 scale is used as built. A positional d3 scale with no range gets the plot’s pixel range. It keeps its padding and stays invertible, so the drag works.
log · sqrt · an adopted d3 scale
x is logarithmic, size ramps by sqrt, and the categorical fill uses a d3 ordinal scale.
const chart = Elicit({width: 380, height: 250,margins: { top: 14, right: 14, bottom: 30, left: 40 },schema: {t: { type: "quantitative", domain: [1, 1000] },w: { type: "quantitative", domain: [0, 100] },g: { type: "categorical", domain: ["a", "b", "c"] },},data: [{ t: 2, w: 20, g: "a" }, { t: 20, w: 55, g: "b" },{ t: 120, w: 40, g: "c" }, { t: 700, w: 80, g: "a" },],marks: [point({channels: {x: { field: "t", scale: "log" },y: { field: "w", edit: move() },size: { field: "w", scale: { type: "sqrt", range: [5, 16] } },fill: { field: "g", scale: d3.scaleOrdinal(["#50AEB7", "#2B5D87", "#EE7B5E", "#F2B94B"]) },},}),],});mount(chart.dom());
Categorical → ordinal palette
A categorical field on a colour channel resolves through the ordinal palette — one hue per category.
Ordinal palette
fill: { field: "kind" } assigns palette colours across the category domain.
const chart = Elicit({width: 340, height: 240,margins: { top: 14, right: 14, bottom: 26, left: 30 },schema: {cat: { type: "categorical", domain: ["A", "B", "C", "D"] },n: { type: "quantitative", domain: [0, 60] },kind: { type: "categorical", domain: ["low", "high"] },},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" },],marks: [bar({channels: {x: { field: "cat" },y: { field: "n" },fill: { field: "kind" },},}),],});mount(chart.dom());
Ordered data gets an ordered ramp
Colour follows the field's data type as well as its scale. categorical values are unordered, so they get distinct hues. ordinal values run low to high, so they get a ramp sampled to one swatch per category, light to dark, in order. A Likert scale then reads as a scale rather than as four unrelated colours.
Declare the difference and the colour follows:
agree: { type: "ordinal", domain: ["disagree", "neutral", "agree", "strongly agree"] }team: { type: "categorical", domain: ["north", "south", "east"] }
Use categorical for a group identifier, even when you list its values in an order. A series key is unordered.
Ordinal above, categorical below
One ramp for the ordered field, distinct hues for the unordered one.
const rows = [{ q: "Q1", agree: "disagree", team: "north" },{ q: "Q2", agree: "neutral", team: "south" },{ q: "Q3", agree: "agree", team: "north" },{ q: "Q4", agree: "strongly agree", team: "east" },];const chart = Elicit({width: 420, height: 210,margins: { top: 14, right: 14, bottom: 26, left: 30 },schema: {q: { type: "categorical", domain: ["Q1", "Q2", "Q3", "Q4"] },// ORDERED: the values run low -> high, so colour should too.agree: { type: "ordinal", domain: ["disagree", "neutral", "agree", "strongly agree"] },// UNORDERED: three labels with no sequence.team: { type: "categorical", domain: ["north", "south", "east"] },},data: rows,legends: true,marks: [point({ size: 13, channels: {x: { field: "q" },y: { value: 60 },fill: { field: "agree" },} }),point({ size: 13, channels: {x: { field: "q" },y: { value: 110 },fill: { field: "team" },} }),],});mount(chart.dom());
Two encodings on one channel
A scale belongs to one encoding: a (channel, field) pair. Two marks colouring by different fields get two scales, each with its own domain and legend. They also get different scheme families, so the two stay perceptually distinct.
Two marks colouring by the same field share one scale, one set of colours, and one legend, so a shared colour means a shared value.
Categorical position → point scale
When a positional channel is categorical, a dot resolves it to a point scale and sits on the category’s tick. Two categorical axes make a discrete grid. Here y is ordinal, so its domain’s order is meaningful.
A discrete grid
Both positions are categories, so each dot lands on an (x, y) cell.
const chart = Elicit({width: 360, height: 300,schema: {x: { type: "categorical", domain: ["A", "B", "C"] },y: { type: "ordinal", domain: ["Low", "Mid", "High"] },group: { type: "categorical", domain: ["alpha", "beta", "gamma"] },},data: [{ x: "A", y: "Low", group: "alpha" },],marks: [point({size: 12,channels: {x: { field: "x" },y: { field: "y" },fill: { field: "group" },},}),],});mount(chart.dom());