Encode
The vis layer starts with encoding: cm.encode evaluates the channels of each row, such as x, y and fill, and passes them down the flow, together with hints for their scales. Data transforms then rearrange the rows, a coordinate system resolves the scales, and marks draw the rows in pixels.
const penguins = await d3.csv("/data/penguins.csv", d3.autoType);
const renderer = cm.svg({width: 640, height: 400});
const root = cm.flow().renderer(renderer);
root
.data(penguins.filter((d) => Number.isFinite(d.body_mass_g)))
.transform(cm.encode, {x: "flipper_length_mm", y: "body_mass_g", stroke: "species"})
.transform(cm.plane, {stroke: {legend: true}})
.add(cm.circle, {r: 3, fill: "none"});
root.render();
document.body.append(renderer.node());cm.encode(channels)
A transform, applied with flow.transform(cm.encode, channels): binds the given channels to the rows of the flow, like core's cm.bind, and records how each is scaled. It must come before any other vis transform, which otherwise throw "No channels: call flow.transform(cm.encode, channels) before …".
flow.transform(cm.encode, {x: "date", y: "close", stroke: "symbol"});Each channel is one of:
- a string, the name of a field of the datum, such as
"date". The field name becomes the label of the channel's axis or legend. For fill and stroke, a string that is a color, such as"steelblue", is a constant instead. - a function of the datum, its index and the data, such as
(d) => d.weight / 1000. - an array of values, one per row.
- a constant, such as
3or"red". - a descriptor,
{value, scale}: value is any of the above, and scale names the scale it uses, such as{value: "culmen_length_mm", scale: "length"}. Channels of the same scale name share one scale, across marks and charts, and the coordinate system takes its options by that name. A channel other than a position can join a position scale, such as{value: "low", scale: "y"}.
The channels are free-form, but these have meaning downstream:
- x, x1, y, y1: the positions, scaled by cm.plane or cm.polar.
- fill, stroke, r, fillOpacity, strokeOpacity: scaled only if they vary with the data, a field, function or array; a constant such as
"red"or3is drawn as is. - z: the series of a row, for lines, areas, stacks and groups. Without z, the series is fill, else stroke.
- key and groupKey: the identity of a row for transitions. A row updates into the row of the same key, and may split from or merge into the row whose key is its group key.
- enterDelay, enterDuration, exitDelay, exitDuration, appearDelay, appearDuration: the timings of transitions, scaled by a scale named time; see cm.stackEnter.
Encoding again adds to the channels: the hints of the channels encoded before are kept, unless encoded again.
Strings in the options of shapes
In the options of a shape added to an encoded flow, a string:
- names a bound channel, if there is one, such as
{fill: "stroke"}to fill dots with the color of their series, or{y: "y1"}; - else names a field of the datum, such as
{text: "name"}for a label or{r: "count"}; - is a constant if it can only be one: a color for fill, stroke or halo, such as
{fill: "white"}, and options of style rather than data, such as curve, textAlign, fontFamily, position or format.
On a flow that isn't encoded, strings are constants. cm.annotate given data reads its strings as fields of that data. Function options receive the row as the shape sees it: its bound channels, such as d.x and d.fill, after scaling, and its datum, d.datum.
flow
.transform(cm.encode, {x: "letter", y: "frequency"})
.transform(cm.spreadX)
.transform(cm.plane)
.add(cm.rect, {fill: "steelblue"}) // a constant color
.add(cm.label, {text: "letter", position: "top"}); // a field of the datum, and a literal