This repository has been archived by the owner on Sep 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
plot.typ
315 lines (268 loc) · 9.38 KB
/
plot.typ
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#import "plot-util.typ": *
#import "plot-mark.typ": *
#import "plot-tics.typ"
#import "plot-line.typ"
#let defaults = (
data-x-axis: "x",
data-y-axis: "y",
colors: (blue, red, green, yellow, black),
)
/* Returns the stroke color if set, or the nth default color */
#let get-plot-color(data, n) = {
if "stroke" in data and not data.stroke in (auto, none) {
return data.stroke
}
return defaults.colors.at(calc.rem(n, defaults.colors.len()))
}
/// Plot a line chart
///
/// Data: Set positional to array or dictionary:
/// - data array Array of data points
/// - mark string Mark type (see plot-mark.typ)
/// - x-axis string X axis to use (x)
/// - y-axis string Y axis to use (y)
/// - stroke stroke Custom stroke
///
/// Tics: Set {x,y,x2,y2}-tics to dictionary:
/// - every number Draw tic every n values
/// - limit number Set tic limit for tics generated using `every`
/// - tics array Place tics at values
/// - mirror bool Mirror tics to opposite side
/// - grid bool Draw tics as grid lines
/// - stroke stroke Tic stroke
///
/// Axis: Set {x,y,x2,y2}-axis to dictionary:
/// - range array Range from low to high (low, high)
///
#let plot(title: none,
x-axis: (:),
y-axis: (:),
x2-axis: (:),
y2-axis: (:),
/* Labels */
x-label: [$x$],
x2-label: [],
y-label: [$y$],
y2-label: [],
width: 8cm,
height: 8cm,
fill: none,
border-stroke: black + .5pt,
/* Padding */
padding: (left: 0em, right: 0em, top: 0em, bottom: 0em),
..data,
) = {
let plots = data.pos().map(v => {
let r = (
x-axis: defaults.data-x-axis,
y-axis: defaults.data-y-axis,
)
if type(v) == "dictionary" {
r = r + v
} else if type(v) == "array" {
r.data = v
}
return r
})
style(st => {
let frame = (x: 0cm, y: 0cm, width: 100%, height: 100%)
let axis-frame = rect-inset(frame, padding)
let data-frame = axis-frame
/* All axes */
let axes = (
x: x-axis, x2: x2-axis,
y: y-axis, y2: y2-axis,
)
/* Calculate unset axis ranges.
* Returns new range as tuple (x-range, y-range)
*/
let autorange-axes(d) = {
let x-axis = p-dict-get(axes, d.x-axis, (:))
let y-axis = p-dict-get(axes, d.y-axis, (:))
let x-range = p-dict-get(x-axis, "range", auto)
let y-range = p-dict-get(y-axis, "range", auto)
if x-range == auto or y-range == auto {
let min-x = none; let max-x = none
let min-y = none; let max-y = none
for pt in d.data {
pt = pt.map(parse-data)
if min-x == none or min-x > pt.at(0) { min-x = pt.at(0) }
if max-x == none or max-x < pt.at(0) { max-x = pt.at(0) }
if min-y == none or min-y > pt.at(1) { min-y = pt.at(1) }
if max-y == none or max-y < pt.at(1) { max-y = pt.at(1) }
}
if x-range == auto {
let x-offset = (max-x - min-x) / 10
if max-x - min-x == 0 { min-x = -.1; max-x = .1 }
x-range = (min-x - x-offset, max-x + x-offset)
}
if y-range == auto {
let y-offset = (max-y - min-y) / 10
if max-y - min-y == 0 { min-y = -.1; max-y = .1 }
y-range = (min-y - y-offset, max-y + y-offset)
}
return (x: x-range, y: y-range)
}
return (x: x-range, y: y-range)
}
/* Compute range */
{
let computed-range = (:)
for sub-data in plots {
let x-axis = sub-data.x-axis
let y-axis = sub-data.y-axis
let ranges = autorange-axes(sub-data)
if not x-axis in computed-range {
computed-range.insert(x-axis, (ranges.x.at(0), ranges.x.at(1)))
} else {
computed-range.at(x-axis).at(0) = (calc.min(computed-range.at(x-axis).at(0), ranges.x.at(0)))
computed-range.at(x-axis).at(1) = (calc.max(computed-range.at(x-axis).at(1), ranges.x.at(1)))
}
if not y-axis in computed-range {
computed-range.insert(y-axis, (ranges.y.at(0), ranges.y.at(1)))
} else {
computed-range.at(y-axis).at(0) = (calc.min(computed-range.at(y-axis).at(0), ranges.y.at(0)))
computed-range.at(y-axis).at(1) = (calc.max(computed-range.at(y-axis).at(1), ranges.y.at(1)))
}
}
for (name, r) in computed-range {
axes.at(name).range = r
}
}
/* All tics */
let tics = (
x: (side: "bottom", angle: 270deg, tics: (), grid: false, mirror: true, every: 1),
y: (side: "left", angle: 0deg, tics: (), grid: false, mirror: true, every: 1),
x2: (side: "top", angle: 90deg, tics: (), grid: false, mirror: true),
y2: (side: "right", angle: 180deg, tics: (), grid: false, mirror: true),
)
/* Compute tic positions */
for (name, t) in tics {
if t != none {
let key = name + "-tics"
if key in data.named() {
tics.at(name) += data.named().at(key)
t = tics.at(name) // t seems to be a copy!
}
let length = if (t.side == "left" or t.side == "right") {
axis-frame.height
} else {
axis-frame.width
}
let axis = axes.at(name)
if "range" in axis {
tics.at(name).tics = plot-tics.tic-list(axes.at(name), t, length)
} else {
tics.at(name).tics = ()
}
}
}
let content = box(width: 100%, height: 100%, fill: fill, {
/* Plot point array */
let stroke-data(data, stroke, n) = {
let x-range = axes.at(data.x-axis).range
let y-range = axes.at(data.y-axis).range
let x-delta = x-range.at(1) - x-range.at(0)
let y-delta = y-range.at(1) - y-range.at(0)
let x-off = x-range.at(0)
let y-off = y-range.at(0)
if x-delta == 0 { x-delta = 1 }
if y-delta == 0 { y-delta = 1 }
let norm-data = data.data.map(pt => {
return ((pt.at(0) - x-off) / x-delta,
(pt.at(1) - y-off) / y-delta)
})
plot-line.render(norm-data, stroke)
}
let mark-data(data, mark, n) = {
let mark-size = p-dict-get(data, "mark-size", .5em)
let mark-stroke = p-dict-get(data, "mark-stroke", auto)
if mark-stroke == auto {
mark-stroke = get-plot-color(data, n) + .5pt
}
let mark-fill = p-dict-get(data, "mark-fill", auto)
if mark-fill == auto {
mark-fill = get-plot-color(data, n)
}
let x-range = axes.at(data.x-axis).range
let y-range = axes.at(data.y-axis).range
let x-off = x-range.at(0)
let y-off = y-range.at(0)
for p in data.data {
let delta-x = x-range.at(1) - x-range.at(0)
let delta-y = y-range.at(1) - y-range.at(0)
let x = (p.at(0) - x-off) / delta-x * 100%
let y = 100% - (p.at(1) - y-off) / delta-y * 100%
/* Skip out of range points */
if x < 0% or x > 100% or y < 0% or y > 100% { continue }
place(dx: x - mark-size/2,
dy: y - mark-size/2,
box(width: mark-size, height: mark-size, {
plot-mark(mark, stroke: mark-stroke, fill: mark-fill)
}))
}
}
/* Render axes */
place(dx: axis-frame.x, dy: axis-frame.y, {
box(width: axis-frame.width, height: axis-frame.height, {
plot-tics.render-marks(tics, axis-frame)
/* Render border */
place(dx: 0cm, dy: 0cm, {
rect(width: axis-frame.width, height: axis-frame.height,
stroke: border-stroke)
})
})
})
/* Plot graph(s) */
place(dx: data-frame.x, dy: data-frame.y, {
box(width: data-frame.width, height: data-frame.height, clip: false, {
let n = 0
for sub-plot in plots {
let stroke = p-dict-get(sub-plot, "stroke", auto)
if stroke == auto {
stroke = get-plot-color(sub-plot, n) + .5pt
}
if stroke != none {
stroke-data(sub-plot, stroke, n)
}
let mark = p-dict-get(sub-plot, "mark", none)
if mark != none {
mark-data(sub-plot, mark, n)
}
n += 1
}
})
})
})
let x-tic-labels = plot-tics.render-labels(tics.x.tics, bottom,
data-frame.width)
let x2-tic-labels = plot-tics.render-labels(tics.x2.tics, top,
data-frame.width)
let y-tic-labels = plot-tics.render-labels(tics.y.tics, right,
data-frame.height)
let y2-tic-labels = plot-tics.render-labels(tics.y2.tics, left,
data-frame.height)
block(breakable: false,
grid(columns: (auto, auto, width, auto, auto),
rows: (auto, auto, auto, height, auto, auto),
gutter: .5em,
/* Title */
[], [], align(center, title), [], [],
/* X2 Label */
[], [], align(center, x2-label), [], [],
/* X2 Tics */
[], [], x2-tic-labels, [], [],
/* Y Label */
align(center + horizon, rotate-bbox(y-label, -90deg)),
y-tic-labels,
content,
y2-tic-labels,
/* Y2 Label */
align(center + horizon, rotate-bbox(y2-label, -90deg)),
/* X Tics */
[], [], x-tic-labels, [], [],
/* X Label */
[], [], align(center, x-label), [], [])
)
})
}