Shapes
The primitives are the shapes that renderers draw. flow.append draws one for each row of a flow, except line and some polygons, which draw one for a group of rows, with each channel computed from an option. Positions are in pixels, or in cells with the terminal renderer. To build shapes of your own, see Extensions.
const renderer = cm.svg({width: 640, height: 120});
const root = cm.flow().renderer(renderer);
root.append(cm.circle, {x: 60, y: 60, r: 40, fill: "steelblue"});
root.append(cm.rect, {x: 130, y: 20, width: 80, height: 80, fill: "orange"});
root.append(cm.triangle, {x: 240, y: 100, x1: 320, y1: 100, x2: 280, y2: 20, fill: "seagreen"});
root.append(cm.link, {x: 350, y: 100, x1: 430, y1: 20, stroke: "black", strokeWidth: 4});
root
.data([460, 490, 520, 550, 580, 610])
.append(cm.line, {x: (d) => d, y: (_, i) => (i % 2 ? 30 : 90), stroke: "crimson", strokeWidth: 2});
root.render();
document.body.append(renderer.node());Most primitives take these channels, as each entry lists:
- fill — the fill color.
- fillOpacity — the fill opacity, from 0 to 1, which replaces the alpha of the fill.
- stroke — the stroke color.
- strokeOpacity — the stroke opacity, from 0 to 1.
- strokeWidth — the stroke width in pixels.
Every shape also takes the options of flow.append, such as filter and clip, and the options of transitions.
cm.circle
Draws circles. Channels: x, y, the center (default 0); r, the radius; fill, fillOpacity, stroke, strokeOpacity, strokeWidth.
Like rect, a circle can also span a box from x to x1 and from y to y1: it's centered in the box, and without r, fits it.
root.append(cm.circle, {x: 50, y: 50, r: 30, fill: "steelblue"});cm.rect
Draws rectangles. Channels:
- x, y — the top-left corner, or the center with
anchor: "center". - width, height — the size.
- x1, y1 — the other corner, instead of width or height, in either direction.
- anchor —
"start"(default) or"center". - rotate — the angle in radians, clockwise, around the center.
- fill, fillOpacity, stroke, strokeOpacity, strokeWidth.
const renderer = cm.svg({width: 320, height: 120});
const root = cm.flow().renderer(renderer);
root.append(cm.rect, {x: 20, y: 20, x1: 140, y1: 100, fill: "orange"});
root.append(cm.rect, {
x: 240,
y: 60,
width: 60,
height: 60,
anchor: "center",
rotate: Math.PI / 4,
fill: "steelblue",
});
root.render();
document.body.append(renderer.node());cm.link
Draws straight lines from (x, y) to (x1, y1). Channels:
- stroke — the color (default
"#000"), with strokeOpacity and strokeWidth (default1). - strokeCap —
"butt"(default),"round"or"square". - rotate — the angle in radians, clockwise.
- transformOrigin — the point to turn around:
"start"(default),"center"or"end".
root.append(cm.link, {x: 0, y: 0, x1: 100, y1: 50, stroke: "steelblue", strokeCap: "round"});cm.line
Draws one line through the points (x, y) of the rows of each group. Its stroke and strokeWidth are those of the first row. Option:
- close — whether to close the line (default
false).
const renderer = cm.svg({width: 320, height: 120});
const root = cm.flow().renderer(renderer);
root
.data(d3.range(40))
.append(cm.line, {x: (d) => 10 + d * 7.5, y: (d) => 60 + Math.sin(d / 3) * 40, stroke: "black", strokeWidth: 2});
root.render();
document.body.append(renderer.node());cm.triangle
Draws triangles through (x, y), (x1, y1) and (x2, y2). Channels: fill, stroke, strokeWidth, and rotate, the angle in radians.
root.append(cm.triangle, {x: 0, y: 0, x1: 10, y1: 0, x2: 10, y2: 10, fill: "seagreen"});cm.polygon
Draws polygons. Channels: x, y, the points; fill, fillOpacity, stroke, strokeOpacity, strokeWidth.
If x and y are numbers, the rows of a group are the points of one polygon, colored by the first row:
root.data(d3.range(6)).append(cm.polygon, {
x: (i) => 160 + Math.cos((i / 6) * Math.PI * 2) * 50,
y: (i) => 60 + Math.sin((i / 6) * Math.PI * 2) * 50,
fill: "steelblue",
});If they're arrays, each row is a polygon:
root.data([{x: [0, 10, 10], y: [0, 0, 10]}]).append(cm.polygon, {x: (d) => d.x, y: (d) => d.y});cm.path
Draws paths. Channels:
- d — an SVG path string, or an array of commands such as
[["M", 0, 0], ["L", 10, 0], ["Z"]], whose arc rotations are in radians. - x, y — an offset (default
0). - fill, fillOpacity, stroke, strokeOpacity, strokeWidth, strokeCap, strokeJoin. A path is filled or stroked only if given a fill or a stroke.
A d3-shape generator gives d as a string:
const renderer = cm.svg({width: 320, height: 120});
const root = cm.flow().renderer(renderer);
const curve = d3.line().curve(d3.curveBasis);
root.append(cm.path, {
d: curve([
[20, 100],
[100, 0],
[220, 120],
[300, 20],
]),
stroke: "black",
strokeWidth: 2,
});
root.render();
document.body.append(renderer.node());cm.point
Draws dots at x and y (default 0), 1 pixel in radius, colored by stroke (default black), with strokeWidth. On the terminal, a point is one cell, and its stroke can be a cell, such as "cell(@, black, yellow)"; see cells.
root.data(points).append(cm.point, {x: (d) => d[0], y: (d) => d[1]});cm.text
Draws texts. Channels:
- text — the text.
- format — a function of the text, or a d3-format specifier such as
",d", for numbers. With it, a number interpolates in transitions, as a ticker. - x, y — the position. With x1 or y1, the text is centered in that box.
- fontSize — in pixels (default
14), with fontFamily and fontWeight (default"normal"). - textAlign —
"start","center"or"end". - textBaseline —
"top","middle","bottom","hanging","alphabetic"(default) or"ideographic". - rotate — the angle in radians, around the position.
- fill, fillOpacity, stroke, strokeOpacity, strokeWidth, strokeJoin. A stroked text is filled only if given a fill.
- paintOrder —
"stroke"paints the stroke under the fill, such as for the halo of a label.
const renderer = cm.svg({width: 320, height: 80});
const root = cm.flow().renderer(renderer);
root.append(cm.text, {
text: 1234567,
format: ",d",
x: 160,
y: 40,
fontSize: 36,
fontWeight: "bold",
textAlign: "center",
textBaseline: "middle",
fill: "orange",
stroke: "black",
});
root.render();
document.body.append(renderer.node());cm.g
Draws nothing itself, and holds the shapes appended to it, drawn at each of its rows, translated by x and y and turned by rotate, in radians. Any shape holds what's appended to it this way; cm.g is the one that draws nothing.
const renderer = cm.svg({width: 320, height: 120});
const root = cm.flow().renderer(renderer);
root
.data([0, 1, 2, 3])
.append(cm.g, {x: (d) => 40 + d * 80, y: 60, rotate: (d) => (d * Math.PI) / 8})
.append(cm.rect, {x: 0, y: 0, width: 40, height: 40, anchor: "center", fill: "steelblue"});
root.render();
document.body.append(renderer.node());cm.clear
Fills the whole renderer with fill, such as the background of a frame. On SVG, clearing at the root also removes what was drawn before.
root.append(cm.clear, {fill: "black"});Clip insets
The channels clipTop, clipRight, clipBottom and clipLeft cut a fraction of a primitive's own box from each side, from 0 (default) to 1: clipRight: 0.5 shows its left half, and clipTop: 1 hides it. A text's box is estimated from its characters; a path isn't clipped. In transitions, they interpolate, so that enter: {clipTop: 1} wipes a bar in from the bottom. Only renderers that clip, SVG, Canvas and the terminal, draw them.
const renderer = cm.svg({width: 320, height: 140});
const root = cm.flow().renderer(renderer);
const sides = ["clipTop", "clipRight", "clipBottom", "clipLeft"];
root.data(sides).append(cm.rect, {
x: (_, i) => 20 + i * 75,
y: 10,
width: 60,
height: 100,
fill: "steelblue",
clipTop: (d) => (d === "clipTop" ? 0.5 : 0),
clipRight: (d) => (d === "clipRight" ? 0.5 : 0),
clipBottom: (d) => (d === "clipBottom" ? 0.5 : 0),
clipLeft: (d) => (d === "clipLeft" ? 0.5 : 0),
});
root.data(sides).append(cm.text, {x: (_, i) => 50 + i * 75, y: 130, text: (d) => d, textAlign: "center", fontSize: 11});
root.render();
document.body.append(renderer.node());Filtering
The filter option of any shape draws only the rows for which it's truthy: a constant, an array, a function of the row, or the name of a bound channel. The rows drawn keep their indices, so channels and transitions still match them.
root.data(d3.range(10)).append(cm.circle, {x: (d) => d * 20, y: 20, r: 5, filter: (d) => d % 2 === 0});Clipping
The clip option of any primitive clips it to the clip of its context, such as the frame of cm.plane, with what's appended to it. It defaults to the context's, such as cm.plane({clip: true}): clip: false draws one shape outside the frame, and clip: true clips one shape in a plane that doesn't. Custom shapes aren't clipped as a whole; the primitives they make are. Only SVG, Canvas and the terminal clip.