Skip to content

Commit ca5e17e

Browse files
committed
WIP Tests
1 parent 39ef220 commit ca5e17e

File tree

8 files changed

+96
-141
lines changed

8 files changed

+96
-141
lines changed

src/plot.typ

+14-4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#import "/src/spine.typ"
88
#import "/src/ticks.typ"
99
#import "/src/sub-plot.typ"
10+
#import "/src/compat.typ"
1011

1112
#import "/src/plot/sample.typ": sample-fn, sample-fn2
1213
#import "/src/plot/line.typ": add, add-hline, add-vline, add-fill-between
@@ -256,18 +257,24 @@
256257
)
257258

258259
if template != none and template in templates {
259-
body += (templates.at(template))(ptx)
260+
body = (templates.at(template))(ptx) + body
260261
}
261262

263+
// Wrap old style elements
264+
body = body.map(elem => {
265+
return if "type" in elem {
266+
compat.wrap(elem)
267+
} else {
268+
elem
269+
}
270+
})
271+
262272
let plot-elements = body
263273
.filter(elem => type(elem) == dictionary)
264274
.sorted(key: elem => elem.at("priority", default: 0))
265275
let cetz-elements = body
266276
.filter(elem => type(elem) == function)
267277

268-
// Create axes
269-
//ptx = plot-util.create-axes(ptx, plot-elements, options.named())
270-
271278
for elem in plot-elements.filter(elem => elem.priority <= 0) {
272279
assert("fn" in elem,
273280
message: "Invalid plot element: " + repr(elem))
@@ -353,11 +360,13 @@
353360

354361
if ptx.legend != none {
355362
draw.scope({
363+
/*
356364
draw.set-origin("plot." + options.at("legend", default: "north-east"))
357365
draw.group(name: "legend", anchor: options.at("legend-anchor", default: "north-west"), {
358366
draw.anchor("default", (0,0))
359367
draw-legend(ptx)
360368
})
369+
*/
361370
})
362371
}
363372

@@ -400,6 +409,7 @@
400409
for plot in ptx.plots {
401410
for proj in plot.projections {
402411
if axes.all(name => proj.axes.contains(name)) {
412+
// FIXME: Broken
403413
let pt = (proj.transform)(position).first()
404414
ptx.anchors.push((name, pt))
405415
}

src/plot/legend.typ

+1-1
Original file line numberDiff line numberDiff line change
@@ -243,5 +243,5 @@
243243

244244
// TODO: Stub
245245
#let draw-legend(ptx) = {
246-
draw.rect((0,0), (1,1))
246+
//draw.rect((0,0), (1,1))
247247
}

src/plot/util.typ

+5-42
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@
247247
(-float.inf, float.inf)
248248
}
249249

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

253253
/// Return points of a sampled catmull-rom through the
@@ -328,34 +328,6 @@
328328
return pts
329329
}
330330

331-
// Get the default axis orientation
332-
// depending on the axis name
333-
#let get-default-axis-horizontal(name) = {
334-
return lower(name).starts-with("x")
335-
}
336-
337-
// Create axes specified by options
338-
#let create-axes(ptx, elements, options) = {
339-
import "/src/axis.typ"
340-
for element in elements {
341-
if "axes" in element {
342-
for name in element.axes {
343-
if not name in ptx.axes {
344-
let mode = options.at(name + "-mode", default: "lin")
345-
346-
ptx.axes.insert(name, if mode == "log" {
347-
axis.logarithmic(name, none, none, 10)
348-
} else {
349-
axis.linear(name, none, none)
350-
})
351-
}
352-
}
353-
}
354-
}
355-
356-
return ptx
357-
}
358-
359331
// Setup axes dictionary
360332
//
361333
// - axis-dict (dictionary): Existing axis dictionary
@@ -370,19 +342,6 @@
370342
if v == auto { default } else { v }
371343
}
372344

373-
// Mode switching
374-
for (name, ax) in axes {
375-
let mode = get-opt(name, "mode", "lin")
376-
if mode == "lin" {
377-
ax.transform = axis._transform-lin
378-
} else if mode == "log" {
379-
ax.transform = axis._transform-log
380-
ax.base = get-opt(name, "base", ax.at("base", default: 10))
381-
} else {
382-
panic("Invalid axis mode: " + repr(mode))
383-
}
384-
}
385-
386345
for (name, ax) in axes {
387346
ax.min = get-opt(name, "min", ax.min)
388347
ax.max = get-opt(name, "max", ax.max)
@@ -395,6 +354,10 @@
395354
ax.ticks.minor-step = get-opt(name, "minor-tick-step", ax.ticks.minor-step)
396355
ax.grid = get-opt(name, "grid", ax.grid)
397356

357+
if get-opt(name, "mode", none) != none {
358+
panic("Mode switching is no longer supported. Use log-axis/lin-axis to create the axis.")
359+
}
360+
398361
axes.at(name) = axis.prepare(ptx, ax)
399362
}
400363

src/ticks.typ

+5-2
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,13 @@
9292
/// - ax (axis): Axis
9393
/// -> List of ticks
9494
#let compute-logarithmic-ticks(ax) = {
95-
let min = calc.log(calc.max(axis.min, util.float-epsilon), base: ax.base)
96-
let max = calc.log(calc.max(axis.max, util.float-epsilon), base: ax.base)
95+
let min = calc.log(calc.max(ax.min, util.float-epsilon), base: ax.base)
96+
let max = calc.log(calc.max(ax.max, util.float-epsilon), base: ax.base)
9797

9898
let compute-list(min, max, step, limit) = {
99+
if step == none or step <= 0 or min == none or max == none {
100+
return ()
101+
}
99102
let num-positive = int((max - 0) / step)
100103

101104
// TODO

tests/axes/log-mode/test.typ

+25-35
Original file line numberDiff line numberDiff line change
@@ -3,40 +3,54 @@
33
#import "/tests/helper.typ": *
44
#import "/src/lib.typ": *
55
#import cetz: draw, canvas
6-
#import cetz-plot: axes,
76

87
#test-case({
9-
import draw: *
10-
118
plot.plot(
129
size: (9, 6),
13-
axis-style: "scientific",
14-
y-mode: "log", y-base: 10,
1510
y-format: "sci",
1611
x-min: 1, x-max: 10, x-tick-step: 1,
1712
y-min: 1, y-max: 10000, y-tick-step: 1, y-minor-tick-step: 1,
1813
x-grid: "both",
1914
y-grid: "both",
2015
{
16+
plot.log-axis("y", base: 10)
17+
2118
plot.add(
2219
domain: (0, 10),
2320
x => {calc.pow(10, x)},
2421
samples: 100,
25-
line: "raw",
2622
label: $ y=10^x $,
2723
)
24+
2825
plot.add(
2926
domain: (1, 10),
3027
x => {x},
3128
samples: 100,
32-
line: "raw",
3329
hypograph: true,
3430
label: $ y=x $,
3531
)
3632
}
3733
)
3834
})
3935

36+
// Column chart test
37+
#test-case({
38+
plot.plot(
39+
size: (9, 6),
40+
y-format: "sci",
41+
x-min: -0.5, x-max: 4.5, x-tick-step: 1,
42+
y-min: 0.1, y-max: 10000, step: 1, minor-step: 1,
43+
x-grid: "both",
44+
y-grid: "both",
45+
{
46+
plot.log-axis("y")
47+
plot.add-bar(
48+
(1, 10, 100, 1000, 10000).enumerate().map(((x,y))=>{(x,y)}),
49+
bar-width: 0.8,
50+
)
51+
}
52+
)
53+
})
4054
// Bode plot test
4155
#box(stroke: 2pt + red,{
4256
canvas({
@@ -47,7 +61,6 @@
4761
)
4862
plot.plot(
4963
size: (16, 6),
50-
axis-style: "scientific",
5164
x-format: none, x-label: none,
5265
x-mode: "log",
5366
x-min: 0.01, x-max: 100, x-tick-step: 1, x-minor-tick-step: 1,
@@ -68,7 +81,6 @@
6881
)
6982
plot.plot(
7083
size: (16, 6),
71-
axis-style: "scientific",
7284
x-mode: "log",
7385
x-min: 0.01, x-max: 100, x-tick-step: 1, x-minor-tick-step: 1,
7486
x-label: [Frequency ($upright(r a d)\/s$)],
@@ -83,35 +95,13 @@
8395
})
8496
})
8597

86-
// Column chart test
87-
#box(stroke: 2pt + red, canvas({
88-
import draw: *
89-
90-
plot.plot(
91-
size: (9, 6),
92-
axis-style: "scientific",
93-
y-mode: "log", y-base: 10,
94-
y-format: "sci",
95-
x-min: -0.5, x-max: 4.5, x-tick-step: 1,
96-
y-min: 0.1, y-max: 10000, y-tick-step: 1, y-minor-tick-step: 1,
97-
x-grid: "both",
98-
y-grid: "both",
99-
{
100-
plot.add-bar(
101-
(1, 10, 100, 1000, 10000).enumerate().map(((x,y))=>{(x,y)}),
102-
bar-width: 0.8,
103-
)
104-
}
105-
)
106-
}))
10798

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

112103
plot.plot(
113104
size: (9, 6),
114-
axis-style: "scientific",
115105
y-mode: "log", y-base: 100,
116106
y-format: "sci",
117107
x-min: -0.5, x-max: 4.5, x-tick-step: 1,
@@ -135,21 +125,21 @@
135125
}
136126
)
137127
}))
128+
*/
138129

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

143134
plot.plot(
144135
size: (9, 6),
145-
axis-style: "scientific",
146-
y-mode: "log", y-base: 10,
147136
y-format: "sci",
148137
x-min: -0.5, x-max: 2.5, x-tick-step: 1,
149138
y-min: 0.1, y-max: 15000, y-tick-step: 1, y-minor-tick-step: 1,
150139
x-grid: "both",
151140
y-grid: "both",
152141
{
142+
plot.log-axis("y")
153143
plot.add-boxwhisker(
154144
(
155145
(x: 0, min: 1, q1: 10, q2: 100, q3: 1000, max: 10000),
@@ -159,4 +149,4 @@
159149
)
160150
}
161151
)
162-
}))
152+
})

tests/plot/equal-axis/test.typ

+4-4
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
x-equal: "y",
1313
b-equal: "a",
1414
{
15-
plot.add-cartesian-axis("a", (0,0), (6,0))
16-
plot.add-cartesian-axis("b", (0,0), (0,3))
15+
plot.lin-axis("a")
16+
plot.lin-axis("b")
1717
plot.add(domain: (0, 2 * calc.pi), t => (calc.cos(t), calc.sin(t)))
1818
plot.add(domain: (0, 2 * calc.pi), t => (calc.cos(t), calc.sin(t)),
1919
axes: ("a", "b"))
@@ -29,8 +29,8 @@
2929
x-equal: "y",
3030
b-equal: "a",
3131
{
32-
plot.add-cartesian-axis("a", (0,0), (3,0))
33-
plot.add-cartesian-axis("b", (0,0), (0,6))
32+
plot.lin-axis("a")
33+
plot.lin-axis("b")
3434
plot.add(domain: (0, 2 * calc.pi), t => (calc.cos(t), calc.sin(t)))
3535
plot.add(domain: (0, 2 * calc.pi), t => (calc.cos(t), calc.sin(t)),
3636
axes: ("a", "b"))

tests/plot/marks/test.typ

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#test-case({
88
import cetz-plot: plot
99

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

1212
plot.plot(
1313
size: (5,5),

0 commit comments

Comments
 (0)