diff --git a/src/plot.typ b/src/plot.typ index 1aad373..cd879d2 100644 --- a/src/plot.typ +++ b/src/plot.typ @@ -451,15 +451,8 @@ if "mark" in d and d.mark != none { draw.scope({ - if y.horizontal { - draw.set-ctx(ctx => { - ctx.transform = matrix.swap-cols(ctx.transform, 0, 1) - return ctx - }) - } - draw.set-style(..d.style, ..d.mark-style) - mark.draw-mark(d.data, x, y, d.mark, d.mark-size, size) + mark.draw-mark(d.data, (x, y), d.mark, d.mark-size, size) }) } } diff --git a/src/plot/mark.typ b/src/plot/mark.typ index 9450d21..97e0986 100644 --- a/src/plot/mark.typ +++ b/src/plot/mark.typ @@ -1,5 +1,6 @@ #import "/src/cetz.typ": draw #import "/src/axes.typ" +#import "/src/plot/util.typ" // Draw mark at point with size #let draw-mark-shape(pt, size, mark, style) = { @@ -34,12 +35,14 @@ } } -#let draw-mark(pts, x, y, mark, mark-size, plot-size) = { - let pts = pts.map(pt => { - axes.transform-vec(plot-size, x, y, none, pt) - }).filter(pt => pt != none) - +/// Draw mark shapes for each point in pts +/// - pts (list): Points +/// - all-axes (list): List of axes, order must match pts value order! +#let draw-mark(pts, all-axes, mark, mark-size, plot-size) = { for pt in pts { - draw-mark-shape(pt, mark-size, mark, (:)) + if util.point-in-range(pt, all-axes) { + pt = axes.transform-vec(plot-size, all-axes.at(0), all-axes.at(1), none, pt) + draw-mark-shape(pt, mark-size, mark, (:)) + } } } diff --git a/src/plot/util.typ b/src/plot/util.typ index 5c59c80..9ca5e25 100644 --- a/src/plot/util.typ +++ b/src/plot/util.typ @@ -370,3 +370,18 @@ return axis-dict } + +/// Tests if point pt is contained in the +/// axis interval of each axis in axes +/// - pt (list): Data array +/// - axes (list): List of axes +#let point-in-range(pt, axes) = { + for i in range(0, axes.len()) { + let a = axes.at(i) + let v = pt.at(i) + if v < a.min or v > a.max { + return false + } + } + return true +} diff --git a/tests/plot/marks/ref/1.png b/tests/plot/marks/ref/1.png index ad6e334..9cba8f8 100644 Binary files a/tests/plot/marks/ref/1.png and b/tests/plot/marks/ref/1.png differ diff --git a/tests/plot/marks/test.typ b/tests/plot/marks/test.typ index aa584f0..489d4ba 100644 --- a/tests/plot/marks/test.typ +++ b/tests/plot/marks/test.typ @@ -24,3 +24,25 @@ } ) }) + +#test-case({ + import cetz-plot: plot + + plot.plot(size: (5,5), x-min: 1, y-min: 1, x-max: 9, y-max: 9, { + plot.add(domain: (0, 10), samples: 11, x => x, mark: "square") + }) +}) + +#test-case({ + import cetz-plot: plot + plot.plot(size: (5,6), { + plot.add(domain: (20, 30), x=>x, mark: "x", axes:("y", "x")) + }) +}) + +#test-case({ + import cetz-plot: plot + plot.plot(size: (5,6), y-horizontal: true, x-horizontal: false, { + plot.add(domain: (20, 30), x=>x, mark: "x") + }) +})