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: Do not draw marks outside axis intervals #77

Closed
wants to merge 3 commits into from
Closed
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
9 changes: 1 addition & 8 deletions src/plot.typ
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}
}
Expand Down
15 changes: 9 additions & 6 deletions src/plot/mark.typ
Original file line number Diff line number Diff line change
@@ -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) = {
Expand Down Expand Up @@ -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, (:))
}
}
}
15 changes: 15 additions & 0 deletions src/plot/util.typ
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Binary file modified tests/plot/marks/ref/1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 22 additions & 0 deletions tests/plot/marks/test.typ
Original file line number Diff line number Diff line change
Expand Up @@ -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")
})
})
Loading