Skip to content

Guides ​

Guides explain the scales of a chart: axes for positions, legends for colors, radii and opacities, and tips for rows. cm.plane draws its axes implicitly, and both coordinate systems draw the legends of scales given legend, with the options of their scales, such as {x: {axis: "top", grid: true}, fill: {legend: true}}. An axis or legend added explicitly with flow.add replaces the implicit one of its kind for the same scale. Guides are drawn after the coordinate system.

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

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

root
  .data(aapl)
  .transform(cm.encode, {x: "Date", y: "Close"})
  .transform(cm.plane, {marginRight: 40, y: {grid: true}})
  .add(cm.lineY)
  .add(cm.axisY, {anchor: "right", label: "Close ($)", tickFormat: "$,.0f"});

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

cm.axisX([options]) ​

Draws the axis of x recorded by cm.plane: its tick marks, tick labels and title, at the bottom of the frame by default, and in cm.polar, the angle axis on a ring. Its options default to those of the scale of x.

  • anchor: "bottom" or "top"; in polar, "outer" or "inner", the edge of the hole, which is the default if there is one.
  • ticks: a count of ticks, or an array of tick values. By default, one per 80px.
  • tickFormat: a function, or a d3-format or d3-time-format specifier, such as "s" or "%b".
  • tickSize: the length of the tick marks, 6 by default.
  • tickPadding: the gap between tick marks and labels, 3 by default.
  • tickRotate: the rotation of the tick labels, in degrees.
  • grid: whether to draw grid lines across the frame, false by default; spokes in polar.
  • label: the title, by default the scale's label, with an arrow for a continuous scale; null for none.
  • labelAnchor: where the title is: "left", "center" or "right"; by default at the right of a continuous axis and the middle of a discrete one.
  • offset: the distance from the frame, 0 by default.
  • color: the color of the tick marks, labels and title.

With {y: {by: "x"}}, there is an axis of y at each category of x. In facets, only the cells along the anchor are labeled. The margins of cm.plane fit the implicit axes; an explicit axis with other options may need a margin given.

cm.axisY([options]) ​

Like cm.axisX, for y: on the left by default, or "right". Its ticks are one per 35px by default. Its labelAnchor is "top", "center" or "bottom": by default level at the top of a continuous axis, and turned along the middle of a discrete one. Axes of y on the same side stack outward.

In cm.polar, it's the radius axis along 12 o'clock, with a ring per tick as its grid, true by default, and no tick marks; it takes no anchor. By the categories of the angle, {y: {by: "x"}}, there is a radius axis along each spoke, and the grid draws rings evenly spaced rings, 4 by default.

cm.axis([options]) ​

Draws both axes. The options x and y are the options of each axis, or false to skip it; the other options apply to both.

js
flow.add(cm.axis, {grid: true, y: {anchor: "right"}});

cm.legend([options]) ​

Draws the legends of the colors, radii and opacities of the chart, under their titles: swatches for categorical colors, a ramp with ticks for continuous colors and opacities, and circles for radii. Like Plot, they're at the top of the chart by default, side by side, wrapping into rows; on the right, like Vega-Lite, they're stacked, wrapping into columns. The options fill, stroke, r, fillOpacity and strokeOpacity are the options of each legend, or false to skip it; the other options apply to all of them.

  • anchor: "top" or "right".
  • label: the title, by default the scale's label.
  • ticks: the count of ticks of a ramp, or of circles, 3 by default.
  • tickFormat: a function, or a d3-format specifier.
  • width: the width of a ramp at the top, 240 by default.
  • height: the height of a ramp on the right, 120 by default.
  • as: a selection that clicking the items of a categorical legend sets; see Interaction.
  • source, multiple, fade: how it selects.
js
flow
  .transform(cm.plane, {fill: {legend: true}}) // implicit, at the top
  .add(cm.rect)
  .add(cm.legend, {fill: {label: "Age", tickFormat: (d) => d.toUpperCase()}}); // replaces it

A chart nested in a grid of cells draws its legends once for the grid.

cm.tip([options]) ​

Like Plot's tip, draws a box describing each row it draws, pointing at it: above it if it fits, else below, moving inward at the edges. Add it with a filter, such as a selection set by cm.nearestX, so that it describes the rows hovered. It's drawn over everything else.

  • channels: the lines of the box, by label, as fields of the datum or functions of the row, such as {Date: "date", Price: (d) => d.datum.close}. By default, the positions and the series as encoded, labeled like their scales.
  • title: a title, a field of the datum or a function of the row.
  • format: a function of the values, or functions by label, such as {Close: (d) => d.toFixed(2)}. By default, numbers in en-US and dates in ISO 8601.
  • shared: "x" or true to describe every row at the same x in one box, such as the series of a multi-line chart at the date hovered, titled by the x, with a line per series; "y" for the same along y.
  • value: with shared, the value of each line, y by default (x with "y"), labeled by its name if a string.
  • anchor: with shared, where the box is along the frame: "top", "middle" or "bottom", or with "y", "left", "middle" or "right".
  • rule: with shared, also draws a rule across the frame at the shared position.

In cm.polar, a box points at the middle of its row, and a shared box sits outside the spoke or ring shared.

js
const symbols = ["AAPL", "AMZN", "GOOG", "IBM"];
const tables = await Promise.all(symbols.map((s) => d3.csv(`/data/${s.toLowerCase()}.csv`, d3.autoType)));
const data = symbols.flatMap((Symbol, i) => tables[i].map((d) => ({Symbol, ...d})));
const hover = cm.selection();

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

root
  .data(data)
  .transform(cm.encode, {x: "Date", y: "Close", stroke: "Symbol"})
  .transform(cm.plane, {y: {grid: true}, stroke: {legend: true}})
  .add(cm.lineY)
  .add(cm.nearestX, {as: hover, shared: "x"})
  .add(cm.tip, {filter: hover, shared: true, rule: true, format: {Close: (d) => d.toFixed(2)}});

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