Skip to content

Transforms ​

The core transforms bind channels to the rows of a flow and map them with scales, for drawings that don't need the vis layer. Each is passed to flow.transform, and the shapes appended after it draw the channels.

js
const data = Array.from({length: 60}, (_, i) => ({x: i, y: Math.sin(i / 6) * (i + 10), r: i}));
const renderer = cm.svg({width: 640, height: 200});
const root = cm.flow().renderer(renderer);

root
  .data(data)
  .transform(cm.bind, {x: (d) => d.x, y: (d) => d.y, r: (d) => d.r, fill: (d) => d.r})
  .transform(cm.fit, {margin: 20, reverseY: true})
  .transform(cm.scale, {r: {range: [2, 10]}, fill: {scale: d3.scaleSequential(d3.interpolateViridis)}})
  .append(cm.circle, {fillOpacity: 0.7});

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

cm.bind ​

Used as flow.transform(cm.bind, channels). Evaluates channels for the rows of each group, like the options of flow.append: a constant, a function of the datum (d), the index (i) and the rows, or an array by index. The channels merge with those bound before. The shapes appended after it draw them, sibling shapes share them, and their options override them or name them with strings, such as {y: "y1"}.

js
root
  .data([{price: 5}, {price: 7}])
  .transform(cm.bind, {x: (d, i) => i * 20, y: 50, width: 10, height: (d) => d.price})
  .append(cm.rect, {fill: "steelblue"});

cm.encode binds channels with field names and scale hints for the vis layer.

cm.scale ​

Used as flow.transform(cm.scale, scales). Maps the bound channels named by the keys of scales with scales, and leaves the others as they are. It throws an error before any channels are bound. Each scale takes:

  • scale — "linear", "ordinal", or a d3-scale, such as d3.scaleLog(), which is copied. By default, linear for numbers and dates, ordinal otherwise.
  • domain — the input values: by default, the extent of the values, or the distinct values for an ordinal scale.
  • range — the output values (default [0, 1] for linear, the d3 scale's own for a d3 scale).
  • clamp — whether values past the domain of a continuous scale map to the ends of the range (default false).

Missing values, null and undefined, map to NaN with a linear scale, and shapes don't draw rows with a missing position or size, nor what's appended under them.

js
root
  .data(data)
  .transform(cm.bind, {x: (d) => d.x, y: (d) => d.y, r: (d) => d.value, fill: (d) => d.type})
  .transform(cm.scale, {r: {range: [2, 10]}, fill: {range: ["steelblue", "orange"]}})
  .append(cm.circle);

cm.fit ​

Used as flow.transform(cm.fit[, options]). Maps the positions to the size of the renderer: x and x1 to its width, sharing one domain, and y and y1 to its height. Discrete values spread evenly, like points. It throws an error before any channels are bound, or without a renderer. Options:

  • scaleX, scaleY — the scales, as for cm.scale.
  • domainX, domainY — the domains, inferred from the values by default.
  • margin — the space between the shapes and the edges (default 0), and marginTop, marginRight, marginBottom, marginLeft, for each side (default margin).
  • reverseX, reverseY — whether to reverse the range (default false); reverseY: true puts larger values at the top.
  • keysX, keysY — the channels to map (default ["x", "x1"] and ["y", "y1"]).
js
root
  .data(data)
  .transform(cm.bind, {x: (d) => d.x, y: (d) => d.y})
  .transform(cm.fit, {margin: 20, reverseY: true})
  .append(cm.circle, {r: 4});

For charts with axes and legends, use cm.plane instead.