Skip to content

Marks ​

Marks are the shapes added to a chart after its coordinate system, with flow.add(shape, options). They read the channels in pixels, and their options follow the rules for strings. Most marks are core shapes: a bar is a cm.rect from x to x1 and y to y1, a dot a cm.circle, and a rule cm.annotate(cm.link). The vis layer adds the marks drawn per series, wedges and labels, and annotations.

Every mark takes the filter option: a function of the row, or a selection, to draw only the rows it chooses.

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", y1: 0})
  .transform(cm.plane, {y: {grid: true}})
  .add(cm.areaY, {fill: "steelblue", fillOpacity: 0.2})
  .add(cm.lineY, {stroke: "steelblue"});

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

cm.lineY([options]) ​

Draws one line per series, by z, else fill, else stroke, along x at y, such as a time series. The points of a series are sorted by x, and a missing value breaks the line. A row with a band on x, such as a bar of cm.spreadX, is at the middle of its band. Like Plot, a line is stroked with the current color, 1.5px wide, with round caps and joins, and isn't filled.

  • curve: the curve, "linear" by default.
  • stroke, strokeOpacity, strokeWidth, strokeCap, strokeJoin: its paint, read from the first row of each series.

cm.lineX([options]) ​

Like cm.lineY, along y at x, such as a vertical line.

cm.lineRadial([options]) ​

The line of cm.polar: x is the angle and y the radius. Its segments follow the circle, and its curve applies in angle and radius; between categories of angles at points, such as the spokes of a radar chart, segments are straight and the curve applies on the screen, such as "cardinal-closed".

cm.areaY([options]) ​

Draws one area per series along x, from y1 to y, such as the layers of a stacked area chart after cm.stackY. Without y1, the area is empty. Over the bands of cm.spreadX, it's flat across each bar, so that with curve "bump-x" it's a ribbon between bars.

  • curve: the curve, "linear" by default.
  • fill, fillOpacity, stroke, strokeOpacity, strokeWidth: its paint, read from the first row of each series.

cm.areaX([options]) ​

Like cm.areaY, along y from x1 to x, such as the halves of a population pyramid.

cm.areaRadial([options]) ​

The area of cm.polar: x is the angle, y the outer radius and y1 the inner radius. Like cm.lineRadial, its edges follow the circle, except between categories of angles at points.

Curves ​

The curve of a line or area names one of Plot's curves, case-insensitive: "linear", "linear-closed", "step", "step-after", "step-before", "basis", "basis-closed", "bump-x", "bump-y", "cardinal", "cardinal-closed", "catmull-rom", "catmull-rom-closed", "monotone-x", "monotone-y" and "natural"; or a d3 curve factory. An unknown name throws "unknown curve".

cm.arc([options]) ​

Draws the wedges of cm.polar with d3.arc, such as the slices of a pie or the petals of a rose. x and x1 are the angles of a wedge, in either order, and y and y1 its outer and inner radii. As arcs, wedges interpolate in angle and radius in transitions.

  • padAngle: the angle between wedges, 0 by default.
  • cornerRadius: the radius of the rounded corners, 0 by default.
  • sweep: the fraction of the wedge drawn from its start angle, such as enter: {sweep: 0} for a wedge that opens like a wheel.
  • fill, fillOpacity, stroke, strokeOpacity, strokeWidth: its paint.

cm.label([options]) ​

Like G2's labels, draws a text placed in or around the box of each row, from x to x1 and y to y1, such as a bar, with the font of the axes. A position given without its pair, such as {x: "x1"}, is a point. On a wedge of cm.polar, it's placed by angle and radius. Every option can vary by row.

  • text: the text, such as a field "name".
  • format: a function or a d3-format specifier for the text.
  • position: "center" by default; a side or corner inside the box, such as "inside-right" or "inside-top-left"; or outside it, such as "top" or "right". On a wedge, "center", "inside" or "outside" its outer radius.
  • padding: the distance from the edge, 4 by default.
  • dx, dy: nudges after placing it.
  • contrast: true for a black or white fill that contrasts with the fill bound to the flow; or a color to contrast with.
  • halo: true for a white halo, or a color; haloWidth: its width, 3 by default.
  • fill, fillOpacity, stroke, strokeOpacity, strokeWidth: its own paint; it doesn't take the colors of the mark it labels.
  • textAlign, textBaseline, fontFamily, fontSize, fontWeight, rotate: its text.
js
const alphabet = await d3.csv("/data/alphabet.csv", d3.autoType);

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

root
  .data(alphabet)
  .transform(cm.encode, {y: "letter", x: "frequency"})
  .transform(cm.spreadY, {sort: "-x"})
  .transform(cm.plane, {x: {percent: true, grid: true}})
  .add(cm.rect, {fill: "steelblue"})
  .add(cm.label, {text: "frequency", format: ".1%", position: "inside-right", contrast: "steelblue"});

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

cm.annotate(shape) ​

Turns a shape into an annotation, which draws its own data in data units through the scales of the chart, such as a rule at a value. It returns a mark: flow.add(cm.annotate(cm.link), {y: 0}). Its options are those of the shape, with:

  • data: its own data; without it, it draws one row. With data, strings of its options name fields of it, such as {x: "date", text: "note"}; without, strings are constants, such as {x: "A"} for a category.
  • x, x1, y, y1: positions in data units. A missing position spans the frame for shapes with extents, cm.link and cm.rect, else sits in the middle of the frame. A rect covers whole bands.

Annotations don't change the scales.

js
flow
  .add(cm.annotate(cm.link), {y: 0}) // a rule along y = 0
  .add(cm.annotate(cm.rect), {x: 1990, x1: 2000, fill: "currentColor", fillOpacity: 0.1}) // a band
  .add(cm.annotate(cm.text), {data: notes, x: "date", y: "value", text: "note"});