Terminal โ
The Terminal renderer draws shapes as ASCII art, in a terminal-like grid drawn on a canvas. Shapes are positioned in cells instead of pixels, and colored with cells โ a character with a foreground and a background color โ instead of plain colors. The grid is computed by a software rasterizer written in Rust and compiled to WebAssembly.
const renderer = cm.terminal({cols: 30, rows: 24, mode: "double"});
const root = cm.flow().renderer(renderer);
root
.data(Array.from({length: 240}, (_, i) => (i / 240) * Math.PI * 2))
.append(cm.g, {x: 15, y: 12})
.append(cm.point, {
x: (t) => 10 * Math.cos(t) * Math.cos(t * 3),
y: (t) => 10 * Math.sin(t) * Math.cos(t * 3),
stroke: "cell(๐)",
});
root.render();
document.body.append(renderer.node());cm.terminal([options]) โ
Returns a renderer that draws a terminal. Options:
- units โ the units of the shapes:
"cells"(default), wherex: 1is the second column, for ASCII art, or"pixels", for charts. - cols, rows โ the size of the grid in cells. If not specified, they're computed from width and height.
- width, height โ the size, in pixels, from which the grid is computed (default
640and480): by the size of a character as shown in cell units, or by cellWidth and cellHeight in pixel units. - cellWidth, cellHeight โ in pixel units, the size of a cell in pixels (default
6and12), that of a 10px character, the size the vis layer measures text at. - mode โ
"single"(default) or"double". In double mode, each cell is two characters wide, so wide characters such as emoji and CJK line up. A normal character is drawn twice to fill the cell. - fontFamily โ the font of the cells (default
"courier-new, courier, monospace"). - fontSize โ the font size in pixels, how large the cells are shown (default
15). - fontWeight โ the font weight (default
"normal").
const renderer = cm.terminal({cols: 80, rows: 24});
document.body.append(renderer.node());In cell units, cm.fit maps to the grid. A shape covers the cells whose centers it contains, so a rect at x: 1 with width: 3 covers columns 1 to 3. A shape smaller than a cell, such as the dot of a scatterplot, is drawn in one cell, and a shape thinner than one, such as a narrow bar, by its edges.
The terminal renderer draws every shape: point, link, rect, circle, triangle, polygon, path, text, in figlet fonts too, clear and g, and the shapes of the vis layer, such as arcs, areas and lines. It clips, and plays transitions. A stroke is one cell wide.
renderer.node() returns the canvas element of the grid, and renderer.toString() returns the grid as a string, a line per row, with ยท for empty cells.
Cells โ
A paint, a fill or a stroke, is a color or a cell. A cell is a string, cell(ch, fg, bg):
- ch โ the character, read as it is, so that
cell(,, red)is a red comma. In double mode, a cell can hold two, such ascell(ab). Wide characters, such as emoji and CJK characters, are detected. - fg โ the color of the character (default the terminal's foreground, white).
- bg โ the background color of the cell (default none, keeping the background under it).
A color paints like a cell made for the shape:
- a fill color is the background of blank cells, so that a bar is a block of color;
- a stroke color draws characters that follow the shape:
-,|,/and\along a line, by its slope,-,|and+around an outline,ofor a dot and*for a point; - a text's fill is the color of its characters.
An opacity blends a color into the background, since a cell can't be see-through, so that a grid is faint.
const renderer = cm.terminal({cols: 40, rows: 10});
const root = cm.flow().renderer(renderer);
root.append(cm.rect, {x: 1, y: 1, width: 12, height: 6, fill: "cell(#, orange)"});
root.append(cm.rect, {x: 15, y: 1, width: 12, height: 6, stroke: "steelblue"});
root.append(cm.link, {x: 29, y: 1, x1: 38, y1: 6, stroke: "cell(*, seagreen)"});
root.append(cm.circle, {x: 33, y: 7, r: 2, fill: "tomato"});
root.append(cm.point, {x: 1, y: 8, stroke: "cell(@, black, yellow)"});
root.render();
document.body.append(renderer.node());On the terminal, text draws one character per cell. Its fill is the color of the characters, or a cell. In pixel units, it's placed in the cells next to its position, and text turned a quarter, such as the label of a y-axis, reads down a column.
Figlet fonts โ
Text in a figlet font is drawn as an ASCII-art banner with figlet: its fontFamily is "figlet", in the standard font, or "figlet ghost". It's a font like any other, so any shape or mark that draws text can use it, such as cm.text or an annotation, cm.annotate(cm.text); other renderers draw the text as it is.
const renderer = cm.terminal({cols: 60, rows: 11});
const root = cm.flow().renderer(renderer);
root.append(cm.text, {
text: "hello",
x: 30,
y: 5,
textAlign: "center",
textBaseline: "middle",
fontFamily: "figlet ghost",
fill: () => (x, y, width) => `hsl(${(x / width) * 360}, 90%, 65%)`,
});
root.render();
document.body.append(renderer.node());The fill of text can be a function, called for each character with its column (x), row (y), and the width and height of the text. It colors the characters individually, for example as a gradient. Because options that are functions are evaluated per datum, the function is returned from an accessor: fill: () => (x, y, width) => color. A d3-scale-chromatic interpolator works well: fill: () => (x, y, width) => d3.interpolateSinebow(x / width).
Charts โ
In pixel units, the vis layer draws on the terminal as on any renderer: a 640 by 400 chart is a grid of about 106 by 33 cells. Its positions are snapped to whole cells, so that bands, points and ticks repeat evenly: every bar has the same width, and grid lines are the same number of rows apart. To fit, a chart may be a little narrower or shorter than its frame, or grow by up to 2 cells into its margins. Here, the stacked bars are blocks of color, the axes are lines of characters, and the legend's swatches are cells.
const seafood = await d3.csv("/data/seafood.csv", d3.autoType);
const renderer = cm.terminal({width: 640, height: 400, units: "pixels"});
const root = cm.flow().renderer(renderer);
root
.data(seafood)
.transform(cm.encode, {x: "lake", y: "count", fill: "species"})
.transform(cm.spreadX)
.transform(cm.stackY)
.transform(cm.plane, {fill: {legend: true}})
.add(cm.rect);
root.render();
document.body.append(renderer.node());A cell is a paint like any color, so a chart can draw with characters: fill: "cell(#, steelblue)". See the terminal chart example.
Animating โ
Draw frames as with any renderer, or with transitions: a transition moves shapes cell by cell, colors blend, and cells change at the end. Here is a fire of characters:
const cols = 70;
const rows = 28;
const fire = Array.from({length: rows}, () => new Array(cols).fill(0));
const renderer = cm.terminal({cols, rows});
const root = cm.flow().renderer(renderer);
function randomChar() {
return String.fromCharCode(32 + Math.floor(Math.random() * 95));
}
// Each cell takes the heat of the cell below it, or of its neighbor, minus a random decay.
function update() {
const noise = cm.noise(0, rows);
for (let y = 0; y < rows - 1; y++) {
for (let x = 0; x < cols; x++) {
const decay = Math.floor(Math.random() * 3);
const spread = Math.floor(Math.random() * 2) - 1;
const index = Math.min(Math.max(0, x - spread), cols - 1);
fire[y][x] = Math.max(0, fire[y + 1][index] - decay);
}
}
for (let x = 0; x < cols; x++) fire[rows - 1][x] = noise(x / 10) | 0;
}
function draw() {
root.append(cm.clear, {fill: "black"});
root
.data(fire)
.append(cm.g, {x: 0, y: (_, i) => i})
.data((row) => row)
.append(cm.point, {
x: (_, i) => i,
y: 0,
stroke: (heat) => (heat === 0 ? null : `cell(${randomChar()}, , ${d3.interpolateCool(heat / rows)})`),
});
root.render();
}
setInterval(() => {
update();
draw();
}, 1000 / 15);
document.body.append(renderer.node());