Skip to content

Commit

Permalink
WIP Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
johannes-wolf committed Nov 26, 2024
1 parent 39ef220 commit f6d163b
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 141 deletions.
18 changes: 14 additions & 4 deletions src/plot.typ
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#import "/src/spine.typ"
#import "/src/ticks.typ"
#import "/src/sub-plot.typ"
#import "/src/compat.typ"

#import "/src/plot/sample.typ": sample-fn, sample-fn2
#import "/src/plot/line.typ": add, add-hline, add-vline, add-fill-between
Expand Down Expand Up @@ -256,18 +257,24 @@
)

if template != none and template in templates {
body += (templates.at(template))(ptx)
body = (templates.at(template))(ptx) + body
}

// Wrap old style elements
body = body.map(elem => {
return if "type" in elem {
compat.wrap(elem)
} else {
elem
}
})

let plot-elements = body
.filter(elem => type(elem) == dictionary)
.sorted(key: elem => elem.at("priority", default: 0))
let cetz-elements = body
.filter(elem => type(elem) == function)

// Create axes
//ptx = plot-util.create-axes(ptx, plot-elements, options.named())

for elem in plot-elements.filter(elem => elem.priority <= 0) {
assert("fn" in elem,
message: "Invalid plot element: " + repr(elem))
Expand Down Expand Up @@ -353,11 +360,13 @@

if ptx.legend != none {
draw.scope({
/*
draw.set-origin("plot." + options.at("legend", default: "north-east"))
draw.group(name: "legend", anchor: options.at("legend-anchor", default: "north-west"), {
draw.anchor("default", (0,0))
draw-legend(ptx)
})
*/
})
}

Expand Down Expand Up @@ -400,6 +409,7 @@
for plot in ptx.plots {
for proj in plot.projections {
if axes.all(name => proj.axes.contains(name)) {
// FIXME: Broken
let pt = (proj.transform)(position).first()
ptx.anchors.push((name, pt))
}
Expand Down
2 changes: 1 addition & 1 deletion src/plot/legend.typ
Original file line number Diff line number Diff line change
Expand Up @@ -243,5 +243,5 @@

// TODO: Stub
#let draw-legend(ptx) = {
draw.rect((0,0), (1,1))
//draw.rect((0,0), (1,1))
}
47 changes: 5 additions & 42 deletions src/plot/util.typ
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@
(-float.inf, float.inf)
}

clipped-paths(points, (x.min, y.min), (x.max, y.max), fill: true)
clipped-paths(points, (x-min, y-min), (x-max, y-max), fill: true)
}

/// Return points of a sampled catmull-rom through the
Expand Down Expand Up @@ -328,34 +328,6 @@
return pts
}

// Get the default axis orientation
// depending on the axis name
#let get-default-axis-horizontal(name) = {
return lower(name).starts-with("x")
}

// Create axes specified by options
#let create-axes(ptx, elements, options) = {
import "/src/axis.typ"
for element in elements {
if "axes" in element {
for name in element.axes {
if not name in ptx.axes {
let mode = options.at(name + "-mode", default: "lin")

ptx.axes.insert(name, if mode == "log" {
axis.logarithmic(name, none, none, 10)
} else {
axis.linear(name, none, none)
})
}
}
}
}

return ptx
}

// Setup axes dictionary
//
// - axis-dict (dictionary): Existing axis dictionary
Expand All @@ -370,19 +342,6 @@
if v == auto { default } else { v }
}

// Mode switching
for (name, ax) in axes {
let mode = get-opt(name, "mode", "lin")
if mode == "lin" {
ax.transform = axis._transform-lin
} else if mode == "log" {
ax.transform = axis._transform-log
ax.base = get-opt(name, "base", ax.at("base", default: 10))
} else {
panic("Invalid axis mode: " + repr(mode))
}
}

for (name, ax) in axes {
ax.min = get-opt(name, "min", ax.min)
ax.max = get-opt(name, "max", ax.max)
Expand All @@ -395,6 +354,10 @@
ax.ticks.minor-step = get-opt(name, "minor-tick-step", ax.ticks.minor-step)
ax.grid = get-opt(name, "grid", ax.grid)

if get-opt(name, "mode", none) != none {
panic("Mode switching is no longer supported. Use log-axis/lin-axis to create the axis.")
}

axes.at(name) = axis.prepare(ptx, ax)
}

Expand Down
16 changes: 14 additions & 2 deletions src/ticks.typ
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@
let num-negative = int((0 - min) / step)
let num-positive = int((max - 0) / step)

if num-negative + num-positive > limit {
panic("Tick limit reached! Adjust axis ticks.limit or ticks.minor-limit.")
}

return range(-num-negative, num-positive + 1).map(t => {
t * step
})
Expand All @@ -92,12 +96,20 @@
/// - ax (axis): Axis
/// -> List of ticks
#let compute-logarithmic-ticks(ax) = {
let min = calc.log(calc.max(axis.min, util.float-epsilon), base: ax.base)
let max = calc.log(calc.max(axis.max, util.float-epsilon), base: ax.base)
let min = calc.log(calc.max(ax.min, util.float-epsilon), base: ax.base)
let max = calc.log(calc.max(ax.max, util.float-epsilon), base: ax.base)

let compute-list(min, max, step, limit) = {
if step == none or step <= 0 or min == none or max == none {
return ()
}

let num-positive = int((max - 0) / step)

if num-positive > limit {
panic("Tick limit reached! Adjust axis ticks.limit or ticks.minor-limit.")
}

// TODO

return ()
Expand Down
60 changes: 25 additions & 35 deletions tests/axes/log-mode/test.typ
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,54 @@
#import "/tests/helper.typ": *
#import "/src/lib.typ": *
#import cetz: draw, canvas
#import cetz-plot: axes,

#test-case({
import draw: *

plot.plot(
size: (9, 6),
axis-style: "scientific",
y-mode: "log", y-base: 10,
y-format: "sci",
x-min: 1, x-max: 10, x-tick-step: 1,
y-min: 1, y-max: 10000, y-tick-step: 1, y-minor-tick-step: 1,
x-grid: "both",
y-grid: "both",
{
plot.log-axis("y", base: 10)

plot.add(
domain: (0, 10),
x => {calc.pow(10, x)},
samples: 100,
line: "raw",
label: $ y=10^x $,
)

plot.add(
domain: (1, 10),
x => {x},
samples: 100,
line: "raw",
hypograph: true,
label: $ y=x $,
)
}
)
})

// Column chart test
#test-case({
plot.plot(
size: (9, 6),
y-format: "sci",
x-min: -0.5, x-max: 4.5, x-tick-step: 1,
y-min: 0.1, y-max: 10000, step: 1, minor-step: 1,
x-grid: "both",
y-grid: "both",
{
plot.log-axis("y")
plot.add-bar(
(1, 10, 100, 1000, 10000).enumerate().map(((x,y))=>{(x,y)}),
bar-width: 0.8,
)
}
)
})
// Bode plot test
#box(stroke: 2pt + red,{
canvas({
Expand All @@ -47,7 +61,6 @@
)
plot.plot(
size: (16, 6),
axis-style: "scientific",
x-format: none, x-label: none,
x-mode: "log",
x-min: 0.01, x-max: 100, x-tick-step: 1, x-minor-tick-step: 1,
Expand All @@ -68,7 +81,6 @@
)
plot.plot(
size: (16, 6),
axis-style: "scientific",
x-mode: "log",
x-min: 0.01, x-max: 100, x-tick-step: 1, x-minor-tick-step: 1,
x-label: [Frequency ($upright(r a d)\/s$)],
Expand All @@ -83,35 +95,13 @@
})
})

// Column chart test
#box(stroke: 2pt + red, canvas({
import draw: *

plot.plot(
size: (9, 6),
axis-style: "scientific",
y-mode: "log", y-base: 10,
y-format: "sci",
x-min: -0.5, x-max: 4.5, x-tick-step: 1,
y-min: 0.1, y-max: 10000, y-tick-step: 1, y-minor-tick-step: 1,
x-grid: "both",
y-grid: "both",
{
plot.add-bar(
(1, 10, 100, 1000, 10000).enumerate().map(((x,y))=>{(x,y)}),
bar-width: 0.8,
)
}
)
}))

// Scatter plot test
#box(stroke: 2pt + red, canvas({
import draw: *

plot.plot(
size: (9, 6),
axis-style: "scientific",
y-mode: "log", y-base: 100,
y-format: "sci",
x-min: -0.5, x-max: 4.5, x-tick-step: 1,
Expand All @@ -135,21 +125,21 @@
}
)
}))
*/

// Box plot test
#box(stroke: 2pt + red, canvas({
#test-case({
import draw: *

plot.plot(
size: (9, 6),
axis-style: "scientific",
y-mode: "log", y-base: 10,
y-format: "sci",
x-min: -0.5, x-max: 2.5, x-tick-step: 1,
y-min: 0.1, y-max: 15000, y-tick-step: 1, y-minor-tick-step: 1,
x-grid: "both",
y-grid: "both",
{
plot.log-axis("y")
plot.add-boxwhisker(
(
(x: 0, min: 1, q1: 10, q2: 100, q3: 1000, max: 10000),
Expand All @@ -159,4 +149,4 @@
)
}
)
}))
})
8 changes: 4 additions & 4 deletions tests/plot/equal-axis/test.typ
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
x-equal: "y",
b-equal: "a",
{
plot.add-cartesian-axis("a", (0,0), (6,0))
plot.add-cartesian-axis("b", (0,0), (0,3))
plot.lin-axis("a")
plot.lin-axis("b")
plot.add(domain: (0, 2 * calc.pi), t => (calc.cos(t), calc.sin(t)))
plot.add(domain: (0, 2 * calc.pi), t => (calc.cos(t), calc.sin(t)),
axes: ("a", "b"))
Expand All @@ -29,8 +29,8 @@
x-equal: "y",
b-equal: "a",
{
plot.add-cartesian-axis("a", (0,0), (3,0))
plot.add-cartesian-axis("b", (0,0), (0,6))
plot.lin-axis("a")
plot.lin-axis("b")
plot.add(domain: (0, 2 * calc.pi), t => (calc.cos(t), calc.sin(t)))
plot.add(domain: (0, 2 * calc.pi), t => (calc.cos(t), calc.sin(t)),
axes: ("a", "b"))
Expand Down
2 changes: 1 addition & 1 deletion tests/plot/marks/test.typ
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#test-case({
import cetz-plot: plot

let axis-options = (("x", "y"), ("x2", "y"), ("x", "y2"), ("x2", "y2"))
let axis-options = (("x", "y"), ("u", "y"), ("x", "v"), ("u", "v"))

plot.plot(
size: (5,5),
Expand Down
Loading

0 comments on commit f6d163b

Please sign in to comment.