Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

plot: Support min/max for add-hline and add-vline #435

Merged
merged 1 commit into from
Jan 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/lib/plot.typ
Original file line number Diff line number Diff line change
Expand Up @@ -264,8 +264,10 @@
} else {
d.at("y-domain", default: (0, 0))
}
axis.min = util.min(axis.min, ..domain)
axis.max = util.max(axis.max, ..domain)
if domain != (none, none) {
axis.min = util.min(axis.min, ..domain)
axis.max = util.max(axis.max, ..domain)
}

axis-dict.at(name) = axis
}
Expand Down
26 changes: 24 additions & 2 deletions src/lib/plot/line.typ
Original file line number Diff line number Diff line change
Expand Up @@ -281,11 +281,15 @@
/// })
/// ```)
///
/// - ..y (number): Y axis value(s) to add a line at
/// - ..y (float): Y axis value(s) to add a line at
/// - min (auto,float): X axis minimum value or auto to take the axis minimum
/// - max (auto,float): X axis maximum value or auto to take the axis maximum
/// - axes (array): Name of the axes to use for plotting
/// - style (style): Style to use, can be used with a palette function
/// - label (none,content): Legend label to show for this plot.
#let add-hline(..y,
min: auto,
max: auto,
axes: ("x", "y"),
style: (:),
label: none,
Expand All @@ -297,6 +301,9 @@
let prepare(self, ctx) = {
let (x-min, x-max) = (ctx.x.min, ctx.x.max)
let (y-min, y-max) = (ctx.y.min, ctx.y.max)
let x-min = if min == auto { x-min } else { min }
let x-max = if max == auto { x-max } else { max }

self.lines = self.y.filter(y => y >= y-min and y <= y-max)
.map(y => ((x-min, y), (x-max, y)))
return self
Expand All @@ -308,10 +315,14 @@
}
}

let x-min = if min == auto { none } else { min }
let x-max = if max == auto { none } else { max }

((
type: "hline",
label: label,
y: y.pos(),
x-domain: (x-min, x-max),
y-domain: (calc.min(..y.pos()), calc.max(..y.pos())),
axes: axes,
style: style,
Expand All @@ -331,12 +342,16 @@
/// })
/// ```)
///
/// - ..x (number): X axis values to add a line at
/// - ..x (float): X axis values to add a line at
/// - min (auto,float): Y axis minimum value or auto to take the axis minimum
/// - max (auto,float): Y axis maximum value or auto to take the axis maximum
/// - axes (array): Name of the axes to use for plotting, note that not all
/// plot styles are able to display a custom axis!
/// - style (style): Style to use, can be used with a palette function
/// - label (none,content): Legend label to show for this plot.
#let add-vline(..x,
min: auto,
max: auto,
axes: ("x", "y"),
style: (:),
label: none,
Expand All @@ -348,6 +363,9 @@
let prepare(self, ctx) = {
let (x-min, x-max) = (ctx.x.min, ctx.x.max)
let (y-min, y-max) = (ctx.y.min, ctx.y.max)
let y-min = if min == auto { y-min } else { min }
let y-max = if max == auto { y-max } else { max }

self.lines = self.x.filter(x => x >= x-min and x <= x-max)
.map(x => ((x, y-min), (x, y-max)))
return self
Expand All @@ -359,11 +377,15 @@
}
}

let y-min = if min == auto { none } else { min }
let y-max = if max == auto { none } else { max }

((
type: "vline",
label: label,
x: x.pos(),
x-domain: (calc.min(..x.pos()), calc.max(..x.pos())),
y-domain: (y-min, y-max),
axes: axes,
style: style,
plot-prepare: prepare,
Expand Down