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 axis style as function #35

Merged
merged 3 commits into from
Aug 6, 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
29 changes: 17 additions & 12 deletions src/plot.typ
Original file line number Diff line number Diff line change
Expand Up @@ -310,9 +310,14 @@
legend-style: (:),
..options
) = draw.group(name: name, ctx => {

// TODO: Assert cetz min version here!

let (make-ctx, draw-axes, data-viewport) = if type(axis-style) == function {
axis-style()
} else {
axis-style
}

let (data, anchors, annotations) = _destructure-body(body)
let axis-dict = _create-axis-dict(ctx, data, anchors, annotations, options, size)
data = _prepare-data-styles(data, plot-style, mark-style)
Expand All @@ -325,7 +330,7 @@
if "axes" not in data.at(i) { continue }

let axes = data.at(i).axes.map(name => axis-dict.at(name))
let plot-ctx = axis-style.make-ctx(axes, size)
let plot-ctx = make-ctx(axes, size)

if "plot-prepare" in data.at(i) {
data.at(i) = (data.at(i).plot-prepare)(data.at(i), plot-ctx)
Expand All @@ -337,9 +342,9 @@
// Background Annotations
for a in annotations.filter(a => a.background) {
let axes = a.axes.map(name => axis-dict.at(name))
let plot-ctx = axis-style.make-ctx(axes, size)
let plot-ctx = make-ctx(axes, size)

axis-style.data-viewport(axes, size, {
data-viewport(axes, size, {
draw.anchor("default", (0, 0))
a.body
})
Expand All @@ -350,9 +355,9 @@
if "axes" not in d { continue }

let axes = d.axes.map(name => axis-dict.at(name))
let plot-ctx = axis-style.make-ctx(axes, size)
let plot-ctx = make-ctx(axes, size)

axis-style.data-viewport(axes, size, {
data-viewport(axes, size, {
draw.anchor("default", (0, 0))
draw.set-style(..d.style)

Expand All @@ -362,16 +367,16 @@
})
}

axis-style.draw-axes(size, axis-dict)
draw-axes(size, axis-dict)

// Stroke + Mark data
for d in data {
if "axes" not in d { continue }

let axes = d.axes.map(name => axis-dict.at(name))
let plot-ctx = axis-style.make-ctx(axes, size)
let plot-ctx = make-ctx(axes, size)

axis-style.data-viewport(axes, size, {
data-viewport(axes, size, {
draw.anchor("default", (0, 0))
draw.set-style(..d.style)

Expand All @@ -391,9 +396,9 @@
// Foreground Annotations
for a in annotations.filter(a => not a.background) {
let axes = a.axes.map(name => axis-dict.at(name))
let plot-ctx = axis-style.make-ctx(axes, size)
let plot-ctx = make-ctx(axes, size)

axis-style.data-viewport(axes, size, {
data-viewport(axes, size, {
draw.anchor("default", (0, 0))
a.body
})
Expand Down Expand Up @@ -452,4 +457,4 @@
}
}

})
})
7 changes: 2 additions & 5 deletions src/plot/axis-style.typ
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
// #import "axis-styles/barycentric-2d/barycentric-2d.typ"
#import "axis-styles/polar-2d/impl.typ" as polar-2d
#import "axis-styles/orthorect-2d/impl.typ" as orthorect-2d
// #import "axis-styles/barycentric-2d/barycentric-2d.typ"
// #import "axis-styles/barycentric-2d/barycentric-2d.typ"
#import "axis-styles/polar-2d/impl.typ": polar-2d
#import "axis-styles/orthorect-2d/impl.typ": orthorect-2d
13 changes: 11 additions & 2 deletions src/plot/axis-styles/orthorect-2d/impl.typ
Original file line number Diff line number Diff line change
@@ -1,2 +1,11 @@
#import "orthorect-2d.typ": make-ctx, data-viewport, draw-axes,
#import "transforms.typ": transform-vec
#let orthorect-2d() = {
import "orthorect-2d.typ": make-ctx, data-viewport, draw-axes,
import "transforms.typ": transform-vec

return (
make-ctx: make-ctx,
data-viewport: data-viewport,
draw-axes: draw-axes,
transform-vec: transform-vec,
)
}
13 changes: 11 additions & 2 deletions src/plot/axis-styles/polar-2d/impl.typ
Original file line number Diff line number Diff line change
@@ -1,2 +1,11 @@
#import "polar-2d.typ": make-ctx, data-viewport, draw-axes
#import "transforms.typ": transform-vec
#let polar-2d() = {
import "polar-2d.typ": make-ctx, data-viewport, draw-axes
import "transforms.typ": transform-vec

return (
make-ctx: make-ctx,
data-viewport: data-viewport,
draw-axes: draw-axes,
transform-vec: transform-vec,
)
}
57 changes: 4 additions & 53 deletions src/plot/axis-styles/polar-2d/polar-2d.typ
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
)
)

// Consider refactor
// TODO: Consider refactor
#let make-ctx((x, y), size) = {
assert(x != none, message: "X axis does not exist")
assert(y != none, message: "Y axis does not exist")
Expand All @@ -31,7 +31,7 @@
}

return (
axes: (x,y),
axes: (x, y),
size: size,
x-scale: x-scale,
y-scale: y-scale,
Expand All @@ -43,15 +43,15 @@
}

#let draw-axes(
(w,h),
(w, h),
axis-dict,
name: none,
..style
) = {
let angular = axis-dict.at("x", default: none)
let distal = axis-dict.at("y", default: none)

let radius = calc.min(w,h)/2
let radius = calc.min(w, h) / 2

draw.group(name: name, ctx => {
draw.anchor("origin", (radius, radius))
Expand Down Expand Up @@ -95,7 +95,6 @@

// Draw axes
draw.group(name: "axes", {

// Render distal
draw.on-layer(style.axis-layer, {
draw.group(name: "axis", {
Expand Down Expand Up @@ -136,54 +135,6 @@
}
})
})
// let axes = (
// ("angular", (0, 0), (w, 0), (0, -1), false, angular-ticks, angular,),
// ("distal", (0, 0), (0, h), (-1, 0), true, distal-ticks, distal,),
// )
// let label-placement = (
// angular: ("south", "north", 0deg),
// distal: ("north", "south", 0deg),
// )

// for (name, start, end, outsides, flip, ticks, axis) in axes {
// let style = get-axis-style(ctx, style, name)
// let is-mirror = axis == none or axis.at("is-mirror", default: false)
// let is-horizontal = name in ("bottom", "top")

// if style.padding != 0 {
// let padding = vector.scale(outsides, style.padding)
// start = vector.add(start, padding)
// end = vector.add(end, padding)
// }

// let (data-start, data-end) = inset-axis-points(ctx, style, axis, start, end)

// draw.on-layer(style.axis-layer, {
// draw.group(name: "axis", {
// if axis != none {
// draw-axis-line(start, end, axis, is-horizontal, style)
// place-ticks-on-line(ticks, data-start, data-end, style, flip: flip, is-mirror: is-mirror)
// }
// })

// if axis != none and axis.label != none and not is-mirror {
// let offset = vector.scale(outsides, style.label.offset)
// let (group-anchor, content-anchor, angle) = label-placement.at(name)

// if style.label.anchor != auto {
// content-anchor = style.label.anchor
// }
// if style.label.angle != auto {
// angle = style.label.angle
// }

// draw.content((rel: offset, to: "axis." + group-anchor),
// [#axis.label],
// angle: angle,
// anchor: content-anchor)
// }
// })
// }
})
})
}
17 changes: 6 additions & 11 deletions src/plot/axis-styles/polar-2d/transforms.typ
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
#import "/src/cetz.typ": draw, matrix, process, util, drawable

// Transform a single vector along a x, y and z axis
// Transform a single vector along a x and y axis
//
// - size (vector): Coordinate system size
// - x-axis (axis): X axis
// - y-axis (axis): Y axis
// - z-axis (axis): Z axis
// - axes (tuple): Axis tuple (x and y)
// - vec (vector): Input vector to transform
// -> vector
#let transform-vec(size, (angular, distal), vec) = {

let radius = calc.min(..size)
let x-norm = (vec.at(0) - angular.min) / (angular.max - angular.min)
let y-norm = (vec.at(1) - distal.min) / (distal.max - distal.min)
Expand All @@ -24,11 +21,9 @@
// Draw inside viewport coordinates of two axes
//
// - size (vector): Axis canvas size (relative to origin)
// - x (axis): Horizontal axis
// - y (axis): Vertical axis
// - z (axis): Z axis
// - axes (tuple): Axis tuple
// - name (string,none): Group name
#let axis-viewport(size,(x, y,), body, name: none) = {
#let axis-viewport(size, (x, y), body, name: none) = {
draw.group(name: name, (ctx => {
let transform = ctx.transform

Expand Down Expand Up @@ -76,5 +71,5 @@
}

// Setup the viewport
axis-viewport(size, (x,y), body, name: name)
}
axis-viewport(size, (x, y), body, name: name)
}
43 changes: 21 additions & 22 deletions src/plot/elements/xy.typ
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
line = (type: line)
}

let line-type = line.at("type", default: "linear")
let line-type = line.at("type", default: "raw")
assert(line-type in ("raw", "linear", "spline", "vh", "hv", "hvh"))

// Transform data into line-data
Expand Down Expand Up @@ -213,31 +213,30 @@
/// ```)
/// - label (none,content): Legend label to show for this plot.
#let xy(domain: auto,
hypograph: false,
epigraph: false,
fill: false,
fill-type: "axis",
style: (:),
mark: none,
mark-size: .2,
mark-style: (:),
samples: 50,
sample-at: (),
line: "linear",
axes: ("x", "y"),
label: none,

data,
x-key: 0,
y-key: 1,
) = {
hypograph: false,
epigraph: false,
fill: false,
fill-type: "axis",
style: (:),
mark: none,
mark-size: .2,
mark-style: (:),
samples: 50,
sample-at: (),
line: "raw",
axes: ("x", "y"),
label: none,

data,
x-key: 0,
y-key: 1) = {
// If data is of type function, sample it
if type(data) == function {
data = sample.sample-fn(data, domain, samples, sample-at: sample-at)
}

// data
let data = data.map(it=>(it.at(x-key), it.at(y-key)))
let data = data.map(it => (it.at(x-key), it.at(y-key)))

// Transform data
let line-data = transform-lines(data, line)
Expand Down Expand Up @@ -439,7 +438,7 @@
domain: auto,
samples: 50,
sample-at: (),
line: "linear",
line: "raw",
axes: ("x", "y"),
label: none,
style: (:)) = {
Expand Down Expand Up @@ -518,4 +517,4 @@
draw.rect((0,0), (1,1), ..self.style)
}
),)
}
}
Loading