Skip to content

Flow ​

A flow is a tree of data and shapes. Each method records a step, such as data, a transform or a shape, and returns a flow to chain from. Nothing runs as the flow is built: the steps run in the order they were built when the flow is rendered, or when a later step needs their rows.

js
const renderer = cm.svg({width: 640, height: 640});
const root = cm.flow().renderer(renderer);

root
  .data(Array.from({length: 120}, (_, i) => (i * Math.PI) / 120))
  .transform(cm.bind, {
    x: (t) => Math.cos(t) * Math.cos(t * 3),
    y: (t) => Math.sin(t) * Math.cos(t * 3),
    r: (t) => t,
  })
  .transform(cm.fit, {margin: 30})
  .transform(cm.scale, {r: {range: [8, 15]}})
  .append(cm.circle, {fill: "black"});

root.render();
document.body.append(renderer.node());

A flow goes through three kinds of steps:

cm.flow([options]) ​

Returns a new root flow, whose only row is 0. Set its renderer, build it, then render it.

  • transition — whether to animate from each render to the next: true for the defaults, an object of defaults such as {duration: 500, ease: "linear"}, or false (default). See Transitions.
js
const root = cm.flow({transition: true}).renderer(cm.svg({width: 200, height: 100}));

flow.renderer([renderer]) ​

If renderer is specified, sets the renderer of every flow of the root, such as cm.svg() or cm.canvas(), and returns this flow. It throws an error for what isn't a renderer, such as cm.svg not called. If renderer isn't specified, returns the renderer, null by default.

js
const renderer = cm.svg({width: 200, height: 100});
const root = cm.flow().renderer(renderer);

root.data([1, 2]).renderer(); // renderer

flow.data([data]) ​

If data is an iterable, such as an array, a typed array or a set, returns a new flow at the same level whose rows are its values. It throws an error for anything else, such as a number.

js
root.data([1, 2, 3]).data(); // [[1, 2, 3]]

If data is a function, returns a new flow with a group of rows for each row of this flow: data is called with the row (d), its index (i), its group and this flow, and returns the rows of the group. A function on the root flow is called once, which is how data reads a signal. Nested in a shape, such as cm.g, each group is drawn under its parent row:

js
const renderer = cm.svg({width: 200, height: 120});
const root = cm.flow().renderer(renderer);

root
  .data([
    [1, 0, 1, 1],
    [0, 1, 1, 0],
    [1, 1, 0, 1],
  ])
  .append(cm.g, {x: 30, y: (_, i) => i * 30 + 30})
  .data((row) => row)
  .append(cm.circle, {x: (_, i) => i * 46, y: 0, r: 10, fill: (d) => (d ? "black" : "#ddd")});

root.render();
document.body.append(renderer.node());

An array nested in a shape is one group drawn under every parent row. If data isn't specified, returns the groups of rows of this flow, running the steps they need.

flow.datum([datum]) ​

Equivalent to flow.data([datum]). If datum isn't specified, returns the groups of this flow.

flow.append(shape[, options]) ​

Appends a shape for each row of this flow and returns a new flow, under that shape, to nest more shapes in. Appended to a shape, it's drawn at each row of the shape, translated by its x and y and turned by its rotate. Each option is a channel, one value per row:

  • A constant is the same for every row: fill: "red".
  • A function is called for each row with the datum (d), the index (i) and the rows: x: (d) => d * 10. After channels are bound, such as with cm.bind, d is the row's channels, such as d.x, with d.datum.
  • An array gives one value per row, by its position. In a composite shape, the rows of the flow it draws into are the indices of the rows of its group, and an array is read at those indices, such as r: value.r.
  • A string that names a bound channel takes its values, such as y: "y1"; other strings, and colors for fill and stroke, are constants.

The bound channels are drawn too, and options override them. Other options of every shape:

  • filter — whether to draw each row, such as (d) => d.value > 0; the rows drawn keep their indices.
  • clip — whether to clip the shape to the clip of its context, such as the frame of cm.plane; defaults to the context's.
  • key, groupKey, enter, exit, appear, delay, duration, ease — for transitions.
js
root.data([1, 2, 3]).append(cm.circle, {x: (d) => d * 50, y: 50, r: (d) => d * 8, fill: "steelblue"});

A joined flow must be split before appending into it.

flow.add(shape[, options]) ​

Like flow.append, but returns this flow, so that siblings share its rows and channels, such as the marks of a chart. In a joined flow, the shape is added to every branch, such as an axis for all the layers.

js
root.data(data).transform(cm.encode, {x: "date", y: "close"}).transform(cm.plane).add(cm.lineY).add(cm.circle, {r: 2});

flow.transform(transform[, ...params]) ​

Records transform with params and returns a new flow. When it runs, transform is called with the flow, its contexts (or null before any channels are bound), and params, and returns the new contexts, one per group. A context holds the indices of its rows, I, the channels, value, an array per channel, the rows, group, and whatever earlier transforms added, such as scales.

js
const double = (flow, contexts) => contexts.map((c) => ({...c, value: {...c.value, r: c.value.r.map((r) => r * 2)}}));

root
  .data([1, 2, 3])
  .transform(cm.bind, {x: (d) => d * 50, y: 50, r: 5})
  .transform(double)
  .append(cm.circle);

A context with a group of its own replaces the rows, such as one row per bin. A transform may also have marks, a function of params returning [shape, options] pairs, which are added after it, such as the axes of cm.plane; a pair ending with {front: true} is drawn after the shapes added to the flow. See Transforms and the vis layer for the built-in ones.

flow.branch(...branches) ​

Builds a pipeline for each of branches, a function of this flow returning its flow, and joins them: the joined flow holds the contexts of every branch, in order, so that the next transform sees them all, such as cm.plane sharing scales across layers. flow.add adds a shape to every branch.

Destructuring the joined flow, right away or after more transforms, splits it into one flow per branch, which take their own shapes:

js
const aapl = await d3.csv("/data/aapl.csv", d3.autoType);
const goog = await d3.csv("/data/goog.csv", d3.autoType);

const renderer = cm.svg({width: 640, height: 300});
const root = cm.flow().renderer(renderer);

const [a, b] = root
  .branch(
    (flow) => flow.data(aapl).transform(cm.encode, {x: "Date", y: "Close"}),
    (flow) => flow.data(goog).transform(cm.encode, {x: "Date", y: "Close"}),
  )
  .transform(cm.plane);

a.add(cm.lineY, {stroke: "steelblue"});
b.add(cm.lineY, {stroke: "orange"});

root.render();
document.body.append(renderer.node());

Each branch must end at the level of this flow. Either every branch or none binds channels. A transform of a joined flow must keep one context per context; nesting data in it, or appending into it, needs a split first.

flow.call(callback[, ...params]) ​

Calls callback with this flow and params, and returns this flow, to reuse a piece of a flow in a chain.

js
function dots(flow, fill) {
  flow.append(cm.circle, {r: 3, fill});
}

root
  .data(data)
  .transform(cm.bind, {x: (d) => d.x, y: (d) => d.y})
  .call(dots, "red");

flow.pipe(callback[, ...params]) ​

Like flow.call, but returns what callback returns, such as the flow it built, so the chain continues from it.

js
const scatter = (flow) => flow.transform(cm.encode, {x: "x", y: "y"}).transform(cm.plane);

root.data(data).pipe(scatter).add(cm.circle);

flow.render() ​

Runs the steps of the root, expands its custom shapes, draws the tree with the renderer, and returns this flow. It can be called on any flow of the root, and throws an error if no renderer is set.

The tree is kept once drawn: rendering again redraws it, and a signal it read redraws what changed on the next frame. Building the flow after it's drawn starts anew, as for each frame of an animation, and draws over the last frame, so a frame usually starts with cm.clear:

js
const renderer = cm.svg({width: 640, height: 160});
const root = cm.flow().renderer(renderer);
let x = 0;

requestAnimationFrame(function frame() {
  x = (x + 2) % 640;
  root.append(cm.clear, {fill: "white"});
  root.append(cm.circle, {x, y: 80, r: 30, fill: "black"});
  root.render();
  requestAnimationFrame(frame);
});

document.body.append(renderer.node());

A render that throws discards what was built, so the next one starts anew.

flow.clear() ​

Drops what the root has built, so that the next render draws only what's built after it, and returns this flow. What was drawn is erased on that render, such as to make every mark exit.

js
root.clear().render(); // draws nothing

flow.finished() ​

Returns a promise that resolves when the transition of the last render ends, or its keyframes, or at once without one.

js
root.render();
await root.finished();