diff --git a/src/axes.typ b/src/axes.typ index cfd4669..57e3cd5 100644 --- a/src/axes.typ +++ b/src/axes.typ @@ -497,7 +497,7 @@ } // Prepares the axis post creation. The given axis -// must be completely set-up, including its intervall. +// must be completely set-up, including its interval. // Returns the prepared axis #let prepare-axis(ctx, axis, name) = { let style = styles.resolve(ctx.style, root: "axes", @@ -531,15 +531,17 @@ // - vec (vector): Input vector to transform // -> vector #let transform-vec(size, x-axis, y-axis, z-axis, vec) = { + let axes = (x-axis, y-axis) - let (x,y,) = for (dim, axis) in (x-axis, y-axis).enumerate() { - + let (x, y,) = for (dim, axis) in axes.enumerate() { let s = size.at(dim) - axis.inset.sum() let o = axis.inset.at(0) - let transform-func(n) = if (axis.mode == "log") { + let transform-func(n) = if axis.mode == "log" { calc.log(calc.max(n, util.float-epsilon), base: axis.base) - } else {n} + } else { + n + } let range = transform-func(axis.max) - transform-func(axis.min) diff --git a/src/plot/bar.typ b/src/plot/bar.typ index 2c40233..bacd268 100644 --- a/src/plot/bar.typ +++ b/src/plot/bar.typ @@ -143,12 +143,12 @@ /// - data (array): Array of data items. An item is an array containing a x an one or more y values. /// For example `(0, 1)` or `(0, 10, 5, 30)`. Depending on the `mode`, the data items /// get drawn as either clustered or stacked rects. -/// - x-key: (int,string): Key to use for retreiving a bars x-value from a single data entry. +/// - x-key: (int,string): Key to use for retrieving a bars x-value from a single data entry. /// This value gets passed to the `.at(...)` function of a data item. -/// - y-key: (auto,int,string,array): Key to use for retreiving a bars y-value. For clustered/stacked +/// - y-key: (auto,int,string,array): Key to use for retrieving a bars y-value. For clustered/stacked /// data, this must be set to a list of keys (e.g. `range(1, 4)`). If set to `auto`, att but the first /// array-values of a data item are used as y-values. -/// - error-key: (none,int,string): Key to use for retreiving a bars y-error. +/// - error-key: (none,int,string): Key to use for retrieving a bars y-error. /// - mode (string): The mode on how to group data items into bars: /// / basic: Add one bar per data value. If the data contains multiple values, /// group those bars next to each other. diff --git a/src/plot/contour.typ b/src/plot/contour.typ index db611c9..e6c3f20 100644 --- a/src/plot/contour.typ +++ b/src/plot/contour.typ @@ -209,12 +209,10 @@ let (x, y) = (ctx.x, ctx.y) self.contours = self.contours.map(c => { - c.stroke-paths = util.compute-stroke-paths(c.line-data, - (x.min, y.min), (x.max, y.max)) + c.stroke-paths = util.compute-stroke-paths(c.line-data, x, y) if self.fill { - c.fill-paths = util.compute-fill-paths(c.line-data, - (x.min, y.min), (x.max, y.max)) + c.fill-paths = util.compute-fill-paths(c.line-data, x, y) } return c }) diff --git a/src/plot/formats.typ b/src/plot/formats.typ index c51dbc4..8d0a7b5 100644 --- a/src/plot/formats.typ +++ b/src/plot/formats.typ @@ -115,7 +115,7 @@ /// ``` /// /// - value (number): Value to format -/// - digits (int): Number of digits for rouding the factor +/// - digits (int): Number of digits for rounding the factor /// -> Content #let sci(value, digits: 2) = { let exponent = if value != 0 { diff --git a/src/plot/line.typ b/src/plot/line.typ index 053ae58..be8671d 100644 --- a/src/plot/line.typ +++ b/src/plot/line.typ @@ -86,16 +86,14 @@ let (x, y) = (ctx.x, ctx.y) // Generate stroke paths - self.stroke-paths = util.compute-stroke-paths(self.line-data, - (x.min, y.min), (x.max, y.max)) + self.stroke-paths = util.compute-stroke-paths(self.line-data, x, y) // Compute fill paths if filling is requested self.hypograph = self.at("hypograph", default: false) self.epigraph = self.at("epigraph", default: false) self.fill = self.at("fill", default: false) if self.hypograph or self.epigraph or self.fill { - self.fill-paths = util.compute-fill-paths(self.line-data, - (x.min, y.min), (x.max, y.max)) + self.fill-paths = util.compute-fill-paths(self.line-data, x, y) } return self @@ -463,15 +461,12 @@ // Generate stroke paths self.stroke-paths = ( - a: util.compute-stroke-paths(self.line-data.a, - (x.min, y.min), (x.max, y.max)), - b: util.compute-stroke-paths(self.line-data.b, - (x.min, y.min), (x.max, y.max)) + a: util.compute-stroke-paths(self.line-data.a, x, y), + b: util.compute-stroke-paths(self.line-data.b, x, y), ) // Generate fill paths - self.fill-paths = util.compute-fill-paths(self.line-data.a + self.line-data.b.rev(), - (x.min, y.min), (x.max, y.max)) + self.fill-paths = util.compute-fill-paths(self.line-data.a + self.line-data.b.rev(), x, y) return self } diff --git a/src/plot/util.typ b/src/plot/util.typ index bff54fc..5c59c80 100644 --- a/src/plot/util.typ +++ b/src/plot/util.typ @@ -109,8 +109,8 @@ let is-inside = in-rect(pt) - let (x1, y1) = prev - let (x2, y2) = pt + let (x1, y1, ..) = prev + let (x2, y2, ..) = pt // Ignore lines if both ends are outsides the x-window and on the // same side. @@ -173,21 +173,21 @@ /// Compute clipped stroke paths /// /// - points (array): X/Y data points -/// - low (vector): Lower clip-window coordinate -/// - high (vector): Upper clip-window coordinate +/// - x (axis): X-Axis +/// - y (axis): Y-Axis /// -> array List of stroke paths -#let compute-stroke-paths(points, low, high) = { - clipped-paths(points, low, high, fill: false) +#let compute-stroke-paths(points, x, y) = { + clipped-paths(points, (x.min, y.min), (x.max, y.max), fill: false) } /// Compute clipped fill path /// /// - points (array): X/Y data points -/// - low (vector): Lower clip-window coordinate -/// - high (vector): Upper clip-window coordinate +/// - x (axis): X-Axis +/// - y (axis): Y-Axis /// -> array List of fill paths -#let compute-fill-paths(points, low, high) = { - clipped-paths(points, low, high, fill: true) +#let compute-fill-paths(points, x, y) = { + clipped-paths(points, (x.min, y.min), (x.max, y.max), fill: true) } /// Return points of a sampled catmull-rom through the diff --git a/src/plot/violin.typ b/src/plot/violin.typ index 3f8f876..5da01e9 100644 --- a/src/plot/violin.typ +++ b/src/plot/violin.typ @@ -3,25 +3,24 @@ #import "sample.typ" #let kernel-normal(x, stdev: 1.5) = { - (1/calc.sqrt(2*calc.pi*calc.pow(stdev,2))) * calc.exp( - (x*x)/(2*calc.pow(stdev,2))) + (1 / calc.sqrt(2 * calc.pi*calc.pow(stdev, 2))) * calc.exp(-(x*x) / (2 * calc.pow(stdev, 2))) } #let _violin-render(self, ctx, violin, filling: true) = { let path = range(self.samples) - .map((t)=>violin.min + (violin.max - violin.min) * (t /self.samples )) - .map((u)=>(u, (violin.convolve)(u))) - .map(((u,v)) => { - (violin.x-position + v, u) - }) + .map((t)=>violin.min + (violin.max - violin.min) * (t / self.samples )) + .map((u)=>(u, (violin.convolve)(u))) + .map(((u,v)) => { + (violin.x-position + v, u) + }) if self.side == "both"{ path += path.rev().map(((x,y))=> {(2 * violin.x-position - x,y)}) } else if self.side == "left"{ - path = path.map( ((x,y))=>{(2 * violin.x-position - x,y)}) + path = path.map(((x,y)) => (2 * violin.x-position - x,y)) } - let (x, y) = (ctx.x, ctx.y) - let stroke-paths = util.compute-stroke-paths(path, (x.min, y.min), (x.max, y.max)) + let stroke-paths = util.compute-stroke-paths(path, ctx.x, ctx.y) for p in stroke-paths{ let args = arguments(..p, closed: self.side == "both") @@ -46,7 +45,7 @@ min: min - (self.extents * range), max: max + (self.extents * range), convolve: (t) => { - points.map((y)=>(self.kernel)((y - t)/self.bandwidth)).sum() / (points.len() * self.bandwidth) + points.map(y => (self.kernel)((y - t) / self.bandwidth)).sum() / (points.len() * self.bandwidth) } ) }) @@ -77,8 +76,8 @@ /// /// - data (array): Array of data items. An item is an array containing an `x` and one /// or more `y` values. -/// - x-key (int, string): Key to use for retreiving the `x` position of the violin. -/// - y-key (int, string): Key to use for retreiving values of points within the category. +/// - x-key (int, string): Key to use for retrieving the `x` position of the violin. +/// - y-key (int, string): Key to use for retrieving values of points within the category. /// - side (string): The sides of the violin to be rendered: /// / left: Plot only the left side of the violin. /// / right: Plot only the right side of the violin. @@ -132,4 +131,4 @@ plot-legend-preview: _plot-legend-preview, ),) -} \ No newline at end of file +} diff --git a/tests/axes/log-mode/test.typ b/tests/axes/log-mode/test.typ index f732959..57f44f8 100644 --- a/tests/axes/log-mode/test.typ +++ b/tests/axes/log-mode/test.typ @@ -163,4 +163,3 @@ } ) })) - diff --git a/tests/plot/annotation/ref/1.png b/tests/plot/annotation/ref/1.png index 160481a..f2fb296 100644 Binary files a/tests/plot/annotation/ref/1.png and b/tests/plot/annotation/ref/1.png differ diff --git a/tests/plot/annotation/test.typ b/tests/plot/annotation/test.typ index 917066c..1526a0a 100644 --- a/tests/plot/annotation/test.typ +++ b/tests/plot/annotation/test.typ @@ -23,3 +23,24 @@ }) }) }) + +#test-case({ + import draw: * + set-style(rect: (stroke: none)) + + plot.plot(size: (6, 4), x-horizontal: false, y-horizontal: true, { + 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 $) + }) + plot.annotate(padding: .1, { + line((calc.pi / 2,-.1), (calc.pi / 2, .8), mark: (end: "stealth")) + }) + }) +}) diff --git a/tests/plot/violin/test.typ b/tests/plot/violin/test.typ index ed1cbfc..3822e2f 100644 --- a/tests/plot/violin/test.typ +++ b/tests/plot/violin/test.typ @@ -1,6 +1,6 @@ #set page(width: auto, height: auto) -#import "/src/lib.typ": * #import "/src/cetz.typ": * +#import "/src/lib.typ": * #import "/tests/helper.typ": * /* Empty plot */ @@ -60,4 +60,4 @@ label: [Female] ) }) -}) \ No newline at end of file +})