scale-x-datetime

Continuous x scale that formats axis labels as datetimes.

Column values may be numeric seconds since 2000-01-01T00:00:00 or ISO-8601 strings of the form YYYY-MM-DDTHH:MM[:SS] or YYYY-MM-DD HH:MM[:SS]. Each break is converted via datetime(year: 2000, month: 1, day: 1, hour: 0, minute: 0, second: 0) + duration(seconds: int(n)) and rendered with dt.display(date-format).

Usage

scale-x-datetime(
  name: none,
  limits: none,
  oob: "drop",
  breaks: auto,
  labels: auto,
  expand: auto,
  date-format: "[year]-[month repr:numerical]-[day] [hour]:[minute]",
)

Parameters

Parameter Default Description
name none Axis title. Overrides any name set via labels when both are present.
limits none Pair (lo, hi) clipping the trained domain (numeric seconds since the epoch or ISO-8601 strings; either side may be auto), or none for automatic limits.
oob "drop" Out-of-range policy: "drop" (default) removes rows whose value falls outside limits; "squish" clamps continuous values to the nearest endpoint.
breaks auto Array of break values (numeric seconds since the epoch or ISO-8601 strings), or auto for automatic tick selection.
labels auto Array of tick labels aligned with breaks, or auto.
expand auto Padding around the domain. Accepts a ratio (5%) for proportional breathing room, a length (5pt) for canvas-space padding, a relative (5pt + 5%) for both, or a (lo, hi) 2-tuple for asymmetric padding, where either element may be auto to keep the per-scale default on that side. auto keeps the per-scale default; false collapses to zero.
date-format "[year]-[month repr:numerical]-[day] [hour]:[minute]" Typst datetime.display pattern used for break labels.

Returns

Scale object consumed by plot.

Examples

ISO-8601 datetime strings rendered with hour-and-minute ticks.

#let d = range(0, 6).map(i => (
  x: "2024-04-01T0" + str(8 + i) + ":00",
  y: i * i,
))
#plot(
  data: d,
  mapping: aes(x: "x", y: "y"),
  layers: (geom-line(), geom-point(size: 2pt)),
  scales: (scale-x-datetime(date-format: "[hour]:[minute]"),),
  width: 12cm,
  height: 6cm,
)

Line-and-point chart of six squared values on an x axis showing hour-minute ticks parsed from ISO-8601 datetime strings.

Line-and-point chart of six squared values on an x axis showing hour-minute ticks parsed from ISO-8601 datetime strings.

See also

scale-y-datetime, scale-x-date, scale-x-time.

Back to top