Coordinates
A coordinate system comes last among the transforms of a chart, after cm.encode and the data transforms. It resolves the scales of the channels, maps the positions to pixels, or to angles and radii, lays out the frame and its margins, and records the axes and legends to draw. cm.plane is cartesian and cm.polar polar. The shapes added after it, the marks, draw in pixels.
Both throw "No renderer: call flow.renderer(renderer) before cm.plane." without a renderer.
cm.plane([options])
Maps x and x1 to the frame from left to right, and y and y1 from bottom to top, like Observable Plot. Its own options are reserved:
- width: the width of the chart; by default, the renderer's width, or 640 if the renderer has none;
"auto"to fit the categories of x. - height: the height of the chart; by default, the renderer's height, or one that fits y.
- margin: every margin; marginTop, marginRight, marginBottom, marginLeft: each margin. By default, the margins fit what's drawn past the frame.
- resolve: how each scale is shared by the groups of the flow, by name, such as
{y: "independent"}. - clip: whether to clip the marks to the frame, false by default.
Every other option is the options of a scale, by its name, such as {x: {grid: true}, fill: {legend: true}}. A scale can't be named like a reserved option: {value: "v", scale: "width"} throws "The scale name "width" is reserved by cm.plane".
const penguins = await d3.csv("/data/penguins.csv", d3.autoType);
const renderer = cm.svg();
const root = cm.flow().renderer(renderer);
root
.data(penguins.filter((d) => Number.isFinite(d.body_mass_g)))
.transform(cm.encode, {x: "culmen_length_mm", y: "body_mass_g", fill: "flipper_length_mm"})
.transform(cm.plane, {
y: {transform: (d) => d / 1000, label: "Body mass (kg)", zero: true, grid: true},
fill: {scheme: "viridis", legend: "right"},
})
.add(cm.circle, {r: 3});
root.render();
document.body.append(renderer.node());Size
A renderer created without a width or height, such as cm.svg(), is resized by the chart, like Plot:
- the width is 640, or with width
"auto", 20px per category of x, or wider for its tick labels, up to 60 categories, plus 10, and 640 for a continuous x; - the height is 400 for a continuous y; 20px per category of y, up to 60, plus 10 and the margins, for a categorical y; and 30px plus the margins without y.
A chart whose rows hold nested charts without a given height is as tall, and with width "auto" as wide, as its cells ask.
Margins
The margins that aren't given fit the axes, their titles, the tick labels that reach past the frame, and the legends at the top and on the right, plus 3px. They are at least 20, and 10 at the top, or 0 without y. The legends add to margin, unless marginTop or marginRight is given.
Resolve
By default, a scale is shared by every group of the flow, such as facets, and by every layer of a branched flow, so that they compare. Channels share a scale by name: the axis by default, or the name given with {value, scale}. With resolve, a position scale can be "independent", inferred from each group's own rows, or "shared". Colors are always shared. Any other value throws "invalid resolve".
const [temperature, precipitation] = root
.data(seattle)
.branch(
(flow) => flow.transform(cm.encode, {x: "date", y: {value: "temp_max", scale: "temperature"}}),
(flow) => flow.transform(cm.encode, {x: "date", y: {value: "precipitation", scale: "precipitation"}}),
)
.transform(cm.plane, {
temperature: {label: "Temperature (°C)"},
precipitation: {label: "Precipitation (in)", axis: "right"},
});See Dual axis and Three axes.
Clip
With clip true, the marks are clipped to the frame, such as bars sliding in from outside it. A mark's own clip option wins: false to draw outside, true to be clipped without the plane's clip. Axes aren't clipped.
Scale options
The options of a scale are given to cm.plane or cm.polar by its name: x and y, a name given with {value, scale}, fill, stroke, r, fillOpacity, strokeOpacity, or time for the timings of transitions. They take precedence over what the transforms recorded and what's inferred.
- type: the type of the scale, case-insensitive, or a d3 scale. Positions take
"linear","log","sqrt","pow","symlog","ordinal"and"point", and"radial"in cm.polar; colors, radii, opacities and timings take"linear","log","sqrt","pow","symlog","ordinal"and"identity", which leaves values as they are, such as colors or radii in pixels. Another type throws "unknown scale type". The base of log, the exponent of pow and the constant of symlog are options too. - domain: the input extent or categories, inferred by default. A domain of bands, from cm.spreadX, comes from the spread: order its categories there.
- range: the output of a color, radius, opacity or timing.
- label: the title of its axis or legend, by default the field name of the channel; null for none.
- reverse: reverses a position, or, like Plot, a color, radius or opacity: a sequential color flips its scheme, and a categorical one its domain, and so its legend.
- transform: a function of the values, such as
(d) => d / 1000, applied before the scale. - percent: maps proportions to percentages, adding "(%)" to the inferred label.
- zero: extends a continuous domain to include 0, except for log.
- nice: rounds a continuous domain, true or a count of ticks.
- clamp: pins values past the domain to its ends.
- scheme: the color scheme of a continuous color, the name of a d3-scale-chromatic interpolator, case-insensitive,
"turbo"by default. An unknown name throws "unknown scheme". - by: for a position, the other position, as
{y: {by: "x"}}: a scale and an axis per category of x, such as the dimensions of parallel coordinates. - scales: with by, the options of each category's scale, such as
{y: {by: "x", scales: {year: {type: "point"}}}}.
Positions are linear for numbers, UTC times for dates and points for categories. Colors are categorical by default, and sequential for numbers. A radius is a square root from 0, its first quartile at 3px and its maximum at most 30px. An opacity is linear from 0 to 1.
A position scale also takes the options of its axis: axis, its side, "bottom", "top" or "both" for x, "left", "right" or "both" for y, true for the default side, or null for none; and ticks, tickFormat, tickSize, tickPadding, tickRotate, grid, labelAnchor, offset and color. A color, radius or opacity takes the options of its legend: legend, true or "top" for the top of the chart, "right" for its right; and ticks, tickFormat, width, height, as, source, multiple and fade.
Implicit and explicit guides
Like Plot, a plane draws the axes of its positions, at the bottom and on the left, and the legends of the scales given legend. These guides are implicit: an axis or legend added to the chart explicitly, such as .add(cm.axisY, {anchor: "right"}), replaces the one of its kind drawn for the same scale. See Guides.
Nested charts
A chart in a flow nested in the rows of another, such as flow.append(cm.g).data((d) => d.rows), is drawn in the frame of its parent row: the cell of a band, such as a facet, or a box of width × height around a point, 40 × 40 by default, such as a glyph. A nested chart has no margins by default and draws no axes unless asked, such as {x: {axis: "bottom"}}. In a grid of cells, its axes are drawn along the edges of the grid, its legends once for the grid, and its scales are shared by the cells, so that they compare.
const penguins = await d3.csv("/data/penguins.csv", d3.autoType);
const renderer = cm.svg({width: 640});
const root = cm.flow().renderer(renderer);
root
.data(d3.groups(penguins.filter((d) => Number.isFinite(d.body_mass_g)), (d) => d.species))
.transform(cm.encode, {y: ([species]) => species})
.transform(cm.spreadY)
.transform(cm.plane, {y: {tickSize: 0}})
.append(cm.g)
.data(([, rows]) => rows)
.transform(cm.encode, {x: "body_mass_g", fill: "sex"})
.transform(cm.binX, {y: "count", thresholds: 20})
.transform(cm.stackY)
.transform(cm.plane, {x: {axis: "bottom"}, fill: {legend: true}})
.add(cm.rect, cm.insetX(0.5));
root.render();
document.body.append(renderer.node());See Nested unit chart, Bars around a circle, Marginal histograms and Scatterplot matrix.
cm.polar([options])
Maps x and x1 to angles, clockwise from 12 o'clock like d3.arc, and y and y1 to radii, with a radial scale by default, so that areas grow with values. A pie is then a stack along x, cm.stackX, a rose a spread along x, cm.spreadX, and a radar chart categories of x at points. Its reserved options are those of cm.plane, and:
- innerRadius: the radius of the hole, as a fraction of the outer radius, 0 by default.
- startAngle: the angle of the start of x, in radians, 0 by default.
- endAngle: the angle of its end, 2π by default.
A renderer without a size is resized to 640 × 640. The margins that aren't given are 40, and grow to fit the tick labels of the angle axis and the legends. With clip, marks are clipped to the disc, ring or sector.
Unlike a plane, a polar chart draws no axes unless asked: {x: {axis: true}} for the angle axis on its default ring, "outer", "inner" (the hole) or "both"; {y: {axis: true}} for the radius axis along 12 o'clock, with rings at its ticks. With {y: {by: "x"}}, each category of the angle has its own radius, linear from 0 by default, and the grid draws rings evenly spaced rings, 4 by default.
Draw wedges with cm.arc, and lines and areas with cm.lineRadial and cm.areaRadial. Other shapes, such as cm.circle, are placed at the point of their angle and radius, a shape in a box at the middle of its wedge.
const data = await d3.csv("/data/access.csv", d3.autoType);
const renderer = cm.svg({width: 640, height: 400});
const root = cm.flow().renderer(renderer);
root
.data(data)
.transform(cm.encode, {x: "value", fill: "name"})
.transform(cm.stackX)
.transform(cm.polar, {innerRadius: 0.5, fill: {legend: "right"}})
.add(cm.arc, {stroke: "white", padAngle: 0.01, cornerRadius: 4})
.add(cm.label, {text: "value", fill: "white"});
root.render();
document.body.append(renderer.node());