scale-x-date

Continuous x scale that formats axis labels as dates.

Column values may be numeric days since 2000-01-01 or ISO-8601 strings of the form YYYY-MM-DD. Each break is converted via datetime(year: 2000, month: 1, day: 1) + duration(days: int(n)) and rendered with dt.display(date-format).

Usage

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

Parameters

Parameter Default Description
name none Axis title. Overrides any name set via labs when both are present.
limits none Pair (lo, hi) clipping the trained domain (in days), or none for automatic limits.
breaks auto Array of break values (in days), 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. auto keeps the per-scale default; false collapses to zero.
date-format "[year]-[month repr:numerical]-[day]" Typst datetime.display pattern used for break labels.

Returns

Scale object consumed by plot.

Examples

Numeric days since 2000-01-01 formatted as year-month ticks.

#let d = range(0, 12).map(i => (x: 8766 + 30 * i, y: i))
#plot(
  data: d,
  mapping: aes(x: "x", y: "y"),
  layers: (geom-line(), geom-point(size: 2pt)),
  scales: (scale-x-date(date-format: "[year]-[month repr:numerical]"),),
  width: 12cm,
  height: 6cm,
)

Line-and-point chart of twelve rising values on an x axis showing year-month ticks decoded from numeric days since 2000-01-01.

Line-and-point chart of twelve rising values on an x axis showing year-month ticks decoded from numeric days since 2000-01-01.

ISO-8601 strings work just as well; pick a longer date-format to spell the month out.

#let d = (
  (x: "2024-01-15", y: 1),
  (x: "2024-04-15", y: 4),
  (x: "2024-07-15", y: 9),
  (x: "2024-10-15", y: 16),
)
#plot(
  data: d,
  mapping: aes(x: "x", y: "y"),
  layers: (geom-line(), geom-point(size: 2pt)),
  scales: (scale-x-date(date-format: "[month repr:short] [year]"),),
  width: 12cm,
  height: 6cm,
)

Line-and-point chart of four rising squared values on an x axis showing short month names parsed from ISO-8601 date strings.

Line-and-point chart of four rising squared values on an x axis showing short month names parsed from ISO-8601 date strings.

See also

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

Back to top