Skip to main content

Nanoplots

In-cell plots, drawn natively and scaled against one domain.

format-nanoplot and the four renderers Keisen ships: nanoplot-line, nanoplot-points, nanoplot-bar, and nanoplot-value-bar.

A nanoplot cell holds any value the renderer’s readings function can read numbers from, and format-nanoplot draws it through the renderer given as plot. The domain spans every reading in the column, taken from the column itself, so the cells can be compared; that is the whole point, since sparklines scaled per cell say nothing. domain: (low, high) overrides it for a column read against a scale that is not its own.

A nanoplot column sizes itself to the plot’s width plus the cell inset on both sides, and a set table-width does not stretch it. The plot’s width is a length in em or pt, not a percentage. A table with a nanoplot column needs cell-inset as a length or a dictionary of lengths, because the column width is computed from it. Where columns-width names the column, it must be a fixed length in em or pt, wider than twice the cell inset: the plot gets what the inset leaves.

Four renderers ship, each drawn with native Typst primitives and each built by calling it, as in plot: nanoplot-line().

Nanoplot renderers
Function Notable arguments
nanoplot-line(..) stroke: auto, thickness: 0.6pt. A sparkline.
nanoplot-points(..) The same, plus radius: 1pt. A sparkline with its readings marked.
nanoplot-bar(..) fill: auto, gap: 30%. Bars, measured from zero.
nanoplot-value-bar(..) fill: auto, label: auto, axis: false. One bar per cell, labelled with its value.

stroke: auto and fill: auto follow the surrounding text colour, so a plot in a styled cell is drawn in that cell’s ink. Bars are measured from zero rather than from the domain’s low, because a bar drawn from 5 to 10 makes a small difference look like the whole quantity; the line renderers scale to the domain instead, since a trend is about shape rather than magnitude.

A renderer is a dictionary with the keys kind, draw, readings and axis, so one written in a document works exactly as well as these.

A renderer can read records rather than plain numbers, because the cell value reaches draw as it is. The shipped draw reads numbers only, so draw and readings must read the value the same way. A renderer that changes readings alone draws a blank plot.

Both calls below format the same column of records.

The shipped renderer finds no numbers in a record, so the cell is blank:

#format-nanoplot("pay", plot: nanoplot-line())

A renderer that reads the salary draws it:

#let salaries(value) = if type(value) == array { value.map(record => float(record.salary)) } else { () }
#let by-salary = nanoplot-line() + (
  readings: salaries,
  draw: (records, domain: none, width: 4em, height: 0.8em) => (nanoplot-line().draw)(
    salaries(records), domain: domain, width: width, height: height,
  ),
)
#format-nanoplot("pay", plot: by-salary)

Axis

The axis key of a renderer is none, or a function that draws an axis. axis(domain: .., width: .., side: ..) takes named arguments only, where side is "top" or "bottom", and it returns content or none. The domain is the one the column’s plots share, and the width is the width of the plot inside the cell.

A renderer with an axis adds up to two rows to the table. The top row sits at the end of the repeating column header, right after the column labels. The bottom row sits right after the last body row, before any summary rows that follow it. Columns whose renderer draws no axis on that side get an empty cell in the row. A column with no readings has no domain and gets no axis.

The axis rows are not body rows. Striping, summaries, rows selectors, locations and styles do not see them.

The table below draws an axis that writes the domain of the trend column. The plain column beside it has no axis.

A table of four regions in two groups, North and South, each closed by a bold Total row of units. The trend column has a small axis label reading 0.9 to 2.1 under its column label, and the same label again under the last body row, above the final Total row. Each body row holds a sparkline in the trend column and another in the plain column, which has no axis label.

Value bar

nanoplot-value-bar draws one number per cell as a bar from zero, with the number written after it. The number can be an integer, a float, or a decimal. The bars of a column share its domain, so their lengths can be compared. fill is auto for the text colour, a colour, a gradient, or a function from the value to a colour. A gradient is sampled at the position of the value in the range from zero, so the colour and the length of a bar agree. label is auto for the value rounded to a whole number, none for no label, or a function from the value to content or to a number. For a column of fractions, give a label function, since auto writes 0.2 as 0. The label has room of its own on the right of the track, sized from the label of the larger end of the domain. A custom label that is wider for another value than for the ends can run past the cell, and the column then fails: make width larger or shorten the label. The track spans zero and both ends of the domain. With an explicit domain, a value outside the track fails while it has a label, because the room is kept for the ends of the track; with label: none, its bar stops at the track end. With axis: true, the top and bottom axis rows write the ends of the track, formatted like the label. The low end starts at the track start, and the high end starts after the track end, in the room of the label. With label: none, the axis writes the rounded values, and the track keeps room for them. If the label of the low end is wider than the track, the column fails: make width larger.

// A bar per cell, coloured along a gradient by its value and labelled with it.
// The axis rows label the ends of the track, lined up with the bars.

#import "@preview/keisen:0.2.0": *

#set page(width: auto, height: auto, margin: 0.5cm)
#set text(font: "Libertinus Serif", size: 10pt)

#display-table(
  (product: ("Bolt", "Nut", "Washer", "Screw", "Rebate"), units: (14, 50, 81, 33, -20)),
  format-nanoplot(
    "units",
    plot: nanoplot-value-bar(fill: gradient.linear(rgb("#9ecae1"), rgb("#08306b")), axis: true),
  ),
)

A table of five products with a units column drawn as bars from zero. Bolt 14, Nut 50, Washer 81 and Screw 33 have bars to the right, coloured from light blue for small values to dark blue for large ones, each with its value written after the bar. Rebate has a short light bar to the left of zero, labelled -20. An axis row above and below the bars writes -20 at the start of the track and 81 at its end.

Back to top