From 19def48fee9a5b106702f18255cf9e5bddc47e43 Mon Sep 17 00:00:00 2001 From: Johannes Wolf Date: Thu, 5 Dec 2024 01:44:43 +0100 Subject: [PATCH] scientific: Draw axis labels --- src/spine/scientific.typ | 33 ++++++++++++++++++++++++++++++++- src/style.typ | 8 ++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/src/spine/scientific.typ b/src/spine/scientific.typ index c17595a..d79e57c 100644 --- a/src/spine/scientific.typ +++ b/src/spine/scientific.typ @@ -81,7 +81,38 @@ } }) - // TODO: Draw labels + let label-config = ( + ("south", "north", 0deg), + ("west", "south", 90deg), + ("north", "south", 0deg), + ("east", "north", 90deg), + ) + for (i, (side, default-anchor, default-angle)) in label-config.enumerate() { + let (ax, dir, _, proj, style, mirror) = axes.at(i) + if ax.label != none and ax.label != [] { + let pos = proj((ax.max + ax.min) / 2) + let offset = vector.scale(dir, -style.label.offset) + let is-horizontal = calc.rem(i, 2) == 0 + pos = if is-horizontal { + (pos, "|-", (rel: offset, to: "spine." + side)) + } else { + (pos, "-|", (rel: offset, to: "spine." + side)) + } + + let angle = style.label.angle + if angle == auto { + angle = default-angle + } + + let anchor = style.label.anchor + if anchor == auto { + anchor = default-anchor + } + + draw.content(pos, + [#ax.label], anchor: anchor, angle: angle) + } + } }, ) } diff --git a/src/style.typ b/src/style.typ index c25ad5d..cc72087 100644 --- a/src/style.typ +++ b/src/style.typ @@ -72,6 +72,12 @@ minor-stroke: black + .25pt, ), + label: ( + angle: auto, + offset: .5em, + anchor: auto, + ), + // Overrides x: ( tick: ( @@ -136,6 +142,8 @@ style.tick.label.offset = resolve-number(style.tick.label.offset) + style.label.offset = resolve-number(style.label.offset) + return style }