Skip to content

Extensions ​

A shape is a function that flow.append calls. There are three kinds, each defined by a function:

  • a primitive draws with the renderer, such as cm.circle;
  • a shape is made of other shapes, evaluated per row, such as a dot with a label;
  • a mark receives its options as they are, such as a chart with its own data, an axis or an interactor.

Shapes made of shapes are expanded once per group when the flow renders, after every step has run, and again only if their rows or a signal they read changed.

cm.primitive(draw) ​

Returns a shape drawn by the renderer itself. When the flow renders, draw is called for each group with the renderer, the indices of the rows to draw, I, the channels, value, an array per option such as value.r[i], the options as given, and the rows, group. It draws with the renderer's methods, such as renderer.circle({x, y, r}).

js
const dot = cm.primitive((renderer, I, value) => {
  for (const i of I) renderer.circle({x: value.x[i], y: value.y[i], r: 3, fill: value.fill?.[i]});
});

root.data(points).append(dot, {x: (d) => d[0], y: (d) => d[1]});

cm.shape(render[, options]) ​

Returns a shape made of other shapes. When the flow renders, render is called for each group with a flow whose rows are the indices of the group's rows, the channels, value, computed from the options per row, and the context of the flow, such as the scales of a chart, with context.options, context.group and context.index, which group it is. It appends shapes to flow, which are drawn where the custom shape is, in its context.

  • overlay — whether to draw it over everything the flow draws, such as a tip (default false).

A function appended as it is is drawn as a cm.shape.

js
const label = cm.shape((flow, value) => {
  flow.append(cm.circle, {x: (i) => value.x[i], y: (i) => value.y[i], r: 3});
  flow.append(cm.text, {
    x: (i) => value.x[i],
    y: (i) => value.y[i] - 6,
    text: (i) => value.text[i],
    fontSize: 10,
    textAlign: "center",
  });
});

const alphabet = (await d3.csv("/data/alphabet.csv", d3.autoType)).slice(0, 10);
const renderer = cm.svg({width: 640, height: 240});
const root = cm.flow().renderer(renderer);

root
  .data(alphabet)
  .transform(cm.encode, {x: "letter", y: "frequency"})
  .transform(cm.plane)
  .add(label, {text: (d) => d.datum.letter});

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

cm.mark(render[, options]) ​

Returns a mark, which receives its options as they are rather than per row: render is called for each group with a flow whose rows are the indices of the group's rows, the channels bound to the flow, value, and the context, whose options are the options given, such as the mark's own data or encoding, with group, the rows, and index. What's appended to flow is drawn once; what's nested in its rows, with flow.data((i) => data), is drawn at each row, such as a chart in each cell of another.

If render returns a flow built from flow, its outlet, what's appended to the mark goes there, such as a rule in the scales of the chart it made. Otherwise, what's appended to the mark is drawn at each of its rows, as for other shapes.

  • overlay — whether to draw it over everything the flow draws (default false).

Here, a chart of bars is a mark that brings its data and encoding, and returns its chart, so that a rule appended to it is drawn in its scales:

js
const barY = cm.mark((flow, value, {group, options: {data, encode, plane, ...style}}) =>
  flow
    .data((i) => (typeof data === "function" ? data(group[i]) : data))
    .transform(cm.encode, encode)
    .transform(cm.spreadX)
    .transform(cm.stackY)
    .transform(cm.plane, plane)
    .add(cm.rect, style),
);

const alphabet = await d3.csv("/data/alphabet.csv", d3.autoType);
const renderer = cm.svg({width: 640, height: 400});
const root = cm.flow().renderer(renderer);

root
  .append(barY, {data: alphabet, encode: {x: "letter", y: "frequency"}, plane: {y: {tickFormat: "%"}}})
  .add(cm.annotate(cm.link), {y: d3.mean(alphabet, (d) => d.frequency), stroke: "red"});

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

Appended to a flow of many rows, with data: (d) => d.rows, the same mark draws small multiples; see the bar chart mark example.