-
-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implements #136 partly (in plot coordinates). This PR adds `add-annotation` to allow drawing in plots. The plot also gets resized to the drawing if it exceeds the axis bounds. In addition, it fixes `place-anchors`, which raised an error because elements are not dictionaries anymore.
- Loading branch information
1 parent
3a1705d
commit 40477d2
Showing
8 changed files
with
164 additions
and
19 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
#import "util.typ" | ||
#import "sample.typ" | ||
#import "/src/draw.typ" | ||
#import "/src/process.typ" | ||
#import "/src/util.typ" | ||
|
||
/// Add an annotation to the plot | ||
/// | ||
/// An annotation is a sub-canvas that uses the plots coordinates specified | ||
/// by its x and y axis. | ||
/// | ||
/// #example(``` | ||
/// import cetz.plot | ||
/// plot.plot(size: (2,2), x-tick-step: none, y-tick-step: none, { | ||
/// plot.add(domain: (0, 2*calc.pi), calc.sin) | ||
/// plot.annotate({ | ||
/// rect((0, -1), (calc.pi, 1), fill: rgb(50,50,200,50)) | ||
/// content((calc.pi, 0), [Here]) | ||
/// }) | ||
/// }) | ||
/// ```) | ||
/// | ||
/// Bounds calculation is done naively, therefore fixed size content _can_ grow | ||
/// out of the plot. You can adjust the padding manually to adjust for that. The | ||
/// feature of solving the correct bounds for fixed size elements might be added | ||
/// in the future. | ||
/// | ||
/// - body (drawable): Elements to draw | ||
/// - axes (axes): X and Y axis names | ||
/// - resize (bool): If true, the plots axes get adjusted to contain the annotation | ||
/// - padding (none,number,dictionary): Annotation padding that is used for axis | ||
/// adjustment | ||
/// - background (bool): If true, the annotation is drawn behind all plots, in the background. | ||
/// If false, the annotation is drawn above all plots. | ||
#let annotate(body, axes: ("x", "y"), resize: true, padding: none, background: false) = { | ||
(( | ||
type: "annotation", | ||
body: body, | ||
axes: axes, | ||
resize: resize, | ||
background: background, | ||
padding: util.as-padding-dict(padding), | ||
),) | ||
} | ||
|
||
// Returns the adjusted axes for the annotation object | ||
// | ||
// -> array Tuple of x and y axis | ||
#let calc-annotation-domain(ctx, x, y, annotation) = { | ||
if not annotation.resize { | ||
return (x, y) | ||
} | ||
|
||
let (ctx: ctx, bounds: bounds, drawables: _) = process.many(ctx, annotation.body) | ||
if bounds == none { | ||
return (x, y) | ||
} | ||
|
||
let (x-min, y-max, ..) = bounds.low | ||
y-max *= -1 | ||
let (x-max, y-min, ..) = bounds.high | ||
y-min *= -1 | ||
|
||
x-min -= annotation.padding.left | ||
x-max += annotation.padding.right | ||
y-min -= annotation.padding.bottom | ||
y-max += annotation.padding.top | ||
|
||
x.min = calc.min(x.min, x-min) | ||
x.max = calc.max(x.max, x-max) | ||
y.min = calc.min(y.min, y-min) | ||
y.max = calc.max(y.max, y-max) | ||
|
||
return (x, y) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#set page(width: auto, height: auto) | ||
#import "/src/lib.typ": * | ||
|
||
#box(stroke: 2pt + red, canvas({ | ||
import draw: * | ||
set-style(rect: (stroke: none)) | ||
|
||
plot.plot(size: (6, 4), { | ||
plot.add(domain: (-calc.pi, 3*calc.pi), calc.sin) | ||
plot.annotate(background: true, { | ||
rect((0, -1), (calc.pi, 1), fill: blue.lighten(90%)) | ||
rect((calc.pi, -1.1), (2*calc.pi, 1.1), fill: red.lighten(90%)) | ||
rect((2*calc.pi, -1.5), (3.5*calc.pi, 1.5), fill: green.lighten(90%)) | ||
}) | ||
plot.annotate(padding: .1, { | ||
line((calc.pi / 2, 1.1), (rel: (0, .2)), (rel: (2*calc.pi, 0)), (rel: (0, -.2))) | ||
content((calc.pi * 1.5, 1.5), $ lambda $) | ||
}) | ||
}) | ||
})) |