Skip to content

Commit 18ee6c3

Browse files
committed
plot: Use custom coordinate systems
1 parent 317eb19 commit 18ee6c3

File tree

2 files changed

+30
-9
lines changed

2 files changed

+30
-9
lines changed

src/plot.typ

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -297,15 +297,38 @@
297297

298298
// Adjust axis bounds for annotations
299299
for a in annotations {
300+
let sub-ctx = ctx
301+
302+
// Register a stub-resolver that just forwards coordinates
303+
sub-ctx.resolve-coordinate.push((ctx, c) => {
304+
if type(c) == dictionary and "plot" in c {
305+
return c.plot
306+
}
307+
return c
308+
})
309+
300310
let (x, y) = a.axes.map(name => axis-dict.at(name))
301-
(x, y) = calc-annotation-domain(ctx, x, y, a)
311+
(x, y) = calc-annotation-domain(sub-ctx, x, y, a)
302312
axis-dict.at(a.axes.at(0)) = x
303313
axis-dict.at(a.axes.at(1)) = y
304314
}
305315

306316
// Set axis options
307317
axis-dict = plot-util.setup-axes(ctx, axis-dict, options.named(), size)
308318

319+
// Register axis coordinate systems
320+
let axis-coordinate-resolver(ctx, c) = {
321+
if type(c) == dictionary and "plot" in c {
322+
let pt = c.plot
323+
let names = c.at("axes", default: ("x", "y"))
324+
let (x, y) = names.map(name => axis-dict.at(name))
325+
326+
return axes.transform-vec(size, x, y, none, pt)
327+
}
328+
return c
329+
}
330+
draw.register-coordinate-resolver(axis-coordinate-resolver)
331+
309332
// Prepare styles
310333
for i in range(data.len()) {
311334
if "style" not in data.at(i) { continue }
@@ -369,8 +392,7 @@
369392
let (x, y) = a.axes.map(name => axis-dict.at(name))
370393
let plot-ctx = make-ctx(x, y, size)
371394

372-
data-viewport(a, x, y, size, {
373-
draw.anchor("default", (0, 0))
395+
draw.scope({
374396
a.body
375397
})
376398
}
@@ -467,8 +489,7 @@
467489
let (x, y) = a.axes.map(name => axis-dict.at(name))
468490
let plot-ctx = make-ctx(x, y, size)
469491

470-
data-viewport(a, x, y, size, {
471-
draw.anchor("default", (0, 0))
492+
draw.scope({
472493
a.body
473494
})
474495
}

tests/axes/log-mode/test.typ

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,12 +125,12 @@
125125
mark: "o"
126126
)
127127
plot.annotate({
128-
rect((0, 1), (calc.pi, 10), fill: rgb(50,50,200,50))
129-
content((2, 3), [Annotation])
128+
rect((plot: (0, 1)), (plot: (calc.pi, 10)), fill: rgb(50,50,200,50), name: "r")
129+
content("r.center", [Annotation])
130130
})
131131
plot.annotate({
132-
rect((0, 1000), (calc.pi, 10000), fill: rgb(50,50,200,50))
133-
content((2, 3000), [Annotation])
132+
rect((plot: (0, 1000)), (plot: (calc.pi, 10000)), fill: rgb(50,50,200,50), name: "r")
133+
content("r.center", [Annotation])
134134
})
135135
}
136136
)

0 commit comments

Comments
 (0)