diff --git a/src/plot.typ b/src/plot.typ index 94f6dad..aa821b2 100644 --- a/src/plot.typ +++ b/src/plot.typ @@ -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) @@ -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) @@ -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 }) @@ -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) @@ -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) @@ -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 }) @@ -452,4 +457,4 @@ } } -}) \ No newline at end of file +}) diff --git a/src/plot/axis-style.typ b/src/plot/axis-style.typ index 81b09cd..d0abf65 100644 --- a/src/plot/axis-style.typ +++ b/src/plot/axis-style.typ @@ -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" \ No newline at end of file +#import "axis-styles/polar-2d/impl.typ": polar-2d +#import "axis-styles/orthorect-2d/impl.typ": orthorect-2d diff --git a/src/plot/axis-styles/orthorect-2d/impl.typ b/src/plot/axis-styles/orthorect-2d/impl.typ index 0c80f51..7a5a365 100644 --- a/src/plot/axis-styles/orthorect-2d/impl.typ +++ b/src/plot/axis-styles/orthorect-2d/impl.typ @@ -1,2 +1,11 @@ -#import "orthorect-2d.typ": make-ctx, data-viewport, draw-axes, -#import "transforms.typ": transform-vec \ No newline at end of file +#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, + ) +} diff --git a/src/plot/axis-styles/polar-2d/impl.typ b/src/plot/axis-styles/polar-2d/impl.typ index 7d4af6d..50a82e1 100644 --- a/src/plot/axis-styles/polar-2d/impl.typ +++ b/src/plot/axis-styles/polar-2d/impl.typ @@ -1,2 +1,11 @@ -#import "polar-2d.typ": make-ctx, data-viewport, draw-axes -#import "transforms.typ": transform-vec \ No newline at end of file +#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, + ) +} diff --git a/src/plot/axis-styles/polar-2d/polar-2d.typ b/src/plot/axis-styles/polar-2d/polar-2d.typ index 2f680ca..bf43354 100644 --- a/src/plot/axis-styles/polar-2d/polar-2d.typ +++ b/src/plot/axis-styles/polar-2d/polar-2d.typ @@ -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") @@ -31,7 +31,7 @@ } return ( - axes: (x,y), + axes: (x, y), size: size, x-scale: x-scale, y-scale: y-scale, @@ -43,7 +43,7 @@ } #let draw-axes( - (w,h), + (w, h), axis-dict, name: none, ..style @@ -51,7 +51,7 @@ 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)) @@ -95,7 +95,6 @@ // Draw axes draw.group(name: "axes", { - // Render distal draw.on-layer(style.axis-layer, { draw.group(name: "axis", { @@ -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) - // } - // }) - // } }) }) } diff --git a/src/plot/axis-styles/polar-2d/transforms.typ b/src/plot/axis-styles/polar-2d/transforms.typ index f2bac4d..3f7c014 100644 --- a/src/plot/axis-styles/polar-2d/transforms.typ +++ b/src/plot/axis-styles/polar-2d/transforms.typ @@ -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) @@ -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 @@ -76,5 +71,5 @@ } // Setup the viewport - axis-viewport(size, (x,y), body, name: name) -} \ No newline at end of file + axis-viewport(size, (x, y), body, name: name) +} diff --git a/src/plot/elements/xy.typ b/src/plot/elements/xy.typ index 092623c..4d65fa6 100644 --- a/src/plot/elements/xy.typ +++ b/src/plot/elements/xy.typ @@ -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 @@ -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) @@ -439,7 +438,7 @@ domain: auto, samples: 50, sample-at: (), - line: "linear", + line: "raw", axes: ("x", "y"), label: none, style: (:)) = { @@ -518,4 +517,4 @@ draw.rect((0,0), (1,1), ..self.style) } ),) -} \ No newline at end of file +}