-
Notifications
You must be signed in to change notification settings - Fork 2
/
timeliney.typ
462 lines (399 loc) · 15.5 KB
/
timeliney.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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
#import "@preview/cetz:0.2.2": canvas, draw, coordinate, util
#let timeline(
body,
spacing: 5pt,
show-grid: false,
grid-style: (stroke: (dash: "dashed", thickness: .5pt, paint: gray)),
tasks-vline: true,
line-style: (stroke: 3pt),
milestone-overhang: 5pt,
milestone-layout: "in-place",
box-milestones: true,
milestone-line-style: (),
offset: 0,
) = style(
styles => {
layout(
size => {
canvas(
debug: false,
length: size.width,
{
import draw: *
let headers = ()
let tasks = ()
let flat_tasks = ()
let milestones = ()
let n_cols = 0
let pt = 1 / size.width.pt()
for line in body {
if line.type == "header" {
headers.push(line.headers)
if line.total > n_cols {
n_cols = line.total
}
} else if line.type == "task" or line.type == "taskgroup" {
tasks.push(line)
} else if line.type == "milestone" {
milestones.push(line)
}
}
// Task titles
group(
{
let i = 0
for task in tasks {
if task.type == "task" {
content(
(rel: (0, 0)),
task.name,
anchor: "north",
name: "task" + str(i),
padding: spacing,
)
anchor(
"task" + str(i) + "-bottom",
(rel: (0, 0), to: "task" + str(i) + ".south", update: true),
)
anchor(
"task" + str(i) + "-top",
(rel: (0, 0), to: "task" + str(i) + ".north-west", update: false),
)
anchor(
"task" + str(i),
(rel: (0, 0), to: "task" + str(i) + ".east", update: false),
)
flat_tasks.push(task)
i += 1
} else if task.type == "taskgroup" {
for t in task.tasks {
content(
(rel: (0, 0)),
t.name,
anchor: "north",
name: "task" + str(i),
padding: spacing,
)
anchor(
"task" + str(i) + "-bottom",
(rel: (0, 0), to: "task" + str(i) + ".south", update: true),
)
anchor(
"task" + str(i) + "-top",
(rel: (0, 0), to: "task" + str(i) + ".north-west", update: false),
)
anchor(
"task" + str(i),
(rel: (0, 0), to: "task" + str(i) + ".east", update: false),
)
flat_tasks.push(t)
i += 1
}
}
}
if milestone-layout == "aligned" {
for (i, milestone) in milestones.enumerate() {
content(
(rel: (0, 0)),
milestone.body,
anchor: "north",
name: "milestone" + str(i),
padding: spacing,
)
anchor(
"milestone" + str(i) + "-bottom",
(rel: (0, 0), to: "milestone" + str(i) + ".south", update: true),
)
anchor(
"milestone" + str(i) + "-right",
(rel: (0, 0), to: "milestone" + str(i) + ".east", update: false),
)
anchor(
"milestone" + str(i) + "-top",
(rel: (0, 0), to: "milestone" + str(i) + ".north", update: false),
)
}
}
},
name: "titles",
)
// Now that we have laid out the task titles, we can render the task group boxes
group(
ctx => {
on-layer(
1,
{
let start_x = coordinate.resolve(ctx, "titles.north-west").at(1).at(0)
let end_x = 1 + start_x
let i = 0
for group in tasks {
if group.type != "taskgroup" {
i += 1
continue
}
let start_i = i
let group_start = none
let group_end = none
for task in group.tasks {
if group_start == none {
let start_y = coordinate.resolve(ctx, "titles.task" + str(i) + "-top").at(1).at(1)
group_start = (start_x, start_y)
}
let end_y = coordinate.resolve(ctx, "titles.task" + str(i) + "-bottom").at(1).at(1)
group_end = (end_x, end_y)
i += 1
}
rect(group_start, group_end, stroke: 1pt)
}
if tasks-vline {
line("titles.north-east", "titles.south-east")
}
if box-milestones and milestone-layout == "aligned" {
let start = none
let end = none
for (i, milestone) in milestones.enumerate() {
if start == none {
let start_y = coordinate.resolve(ctx, "titles.milestone" + str(i) + "-top").at(1).at(1)
start = (start_x, start_y)
}
let end_y = coordinate.resolve(ctx, "titles.milestone" + str(i) + "-bottom").at(1).at(1)
end = (end_x, end_y)
}
rect(start, end, stroke: 1pt)
}
},
)
},
name: "boxes",
)
get-ctx(
ctx => {
let (start_x, start_y, _) = coordinate.resolve(ctx, "titles.north-east").at(1)
let end_x = 1 + coordinate.resolve(ctx, "titles.north-west").at(1).at(0)
let end_y = coordinate.resolve(ctx, "titles.south").at(1).at(1)
group(
{
for (i, header) in headers.rev().enumerate() {
let passed = 0
for group in header {
let group_start = none
let group_end = none
for (name, len) in group.titles {
let start = (
a: (start_x, start_y + 16 * (i + 1) * pt),
b: (end_x, start_y + 16 * (i + 1) * pt),
number: passed / n_cols * 100%,
)
if group_start == none { group_start = start }
let end = (
a: (start_x, start_y + 16 * i * pt),
b: (end_x, start_y + 16 * i * pt),
number: (passed + len) / n_cols * 100%,
)
group_end = end
content(start, end, anchor: "north-west", align(center + horizon, name))
passed += len
}
let group_style = (stroke: 1pt + black)
if "style" in group {
group_style = group.style
}
rect(group_start, group_end, ..group_style)
}
}
},
name: "top-headers",
)
// Draw the lines
for (i, task) in flat_tasks.enumerate() {
let start = "titles.task" + str(i)
let task_start_y = coordinate.resolve(ctx, "titles.task" + str(i)).at(1).at(1)
let (task_top_x, task_top_y, _) = coordinate.resolve(ctx, "titles.task" + str(i) + "-top").at(1)
let task_bottom_y = coordinate.resolve(ctx, "titles.task" + str(i) + "-bottom").at(1).at(1)
let h = task_top_y - task_bottom_y
for gantt_line in task.lines {
let start = (
a: (start_x, task_start_y),
b: (end_x, task_start_y),
number: (gantt_line.from + offset) / n_cols * 100%,
)
let end = (
a: (start_x, task_start_y),
b: (end_x, task_start_y),
number: (gantt_line.to + offset) / n_cols * 100%,
)
let style = line-style
if ("style" in gantt_line) { style = gantt_line.style }
line(start, end, ..style)
}
}
// Grid
if show-grid != false {
let month_width = (end_x - start_x) / n_cols
on-layer(
-1,
{
// Horizontal
if show-grid == true or show-grid == "x" {
for i in range(1, n_cols) {
line(
(start_x + month_width * i, start_y),
(start_x + month_width * i, end_y),
..grid-style,
)
}
}
if show-grid == true or show-grid == "y" {
for (i, task) in flat_tasks.enumerate() {
let task_bottom_y = coordinate.resolve(ctx, "titles.task" + str(i) + "-bottom").at(1).at(1)
line((start_x, task_bottom_y), (end_x, task_bottom_y), ..grid-style)
}
if milestone-layout == "aligned" {
for (i, milestone) in milestones.enumerate() {
let bottom_y = coordinate.resolve(ctx, "titles.milestone" + str(i) + "-bottom").at(1).at(1)
line((start_x, bottom_y), (end_x, bottom_y), ..grid-style)
}
}
}
// Border all around the timeline
rect("titles.north-west", (end_x, end_y), stroke: black + 1pt)
},
)
}
// Milestones
if milestones.len() > 0 {
let draw-milestone(
i,
at: 0,
body: "",
style: milestone-line-style,
overhang: milestone-overhang,
spacing: spacing,
anchor: "north",
type: "milestone",
) = {
if milestone-layout == "in-place" {
let x = (end_x - start_x) * ((at + offset) / n_cols) + start_x
get-ctx(
ctx => {
let pos = (x: x, y: end_y - (spacing + overhang).pt() * pt)
let box_x = x
let (w, h) = util.measure(ctx, body)
if x + w / 2 > end_x {
box_x = end_x - w / 2
}
if i != 0 {
let (prev_end_x, prev_start_y, _) = coordinate.resolve-anchor(ctx, "milestone" + str(i - 1) + ".north-east")
let prev_end_y = coordinate.resolve-anchor(ctx, "milestone" + str(i - 1) + ".south").at(1)
if box_x - w / 2 < prev_end_x and pos.y <= prev_start_y and pos.y + h >= prev_end_y {
pos = (x: x, y: prev_end_y - spacing.pt() * pt * 2)
}
}
line((x, start_y), (rel: (0, overhang.pt() * pt), to: pos), ..style)
on-layer(
1,
{
content((box_x, pos.y), anchor: anchor, body, name: "milestone" + str(i))
},
)
},
)
} else if milestone-layout == "aligned" {
let x = (end_x - start_x) * (at / n_cols) + start_x
let end_y = coordinate.resolve(ctx, "titles.milestone" + str(i) + "-right").at(1).at(1)
line((x, start_y), (x, end_y), (start_x, end_y), ..style)
}
}
on-layer(-0.5, {
if milestone-layout == "aligned" {
set-ctx(ctx => {
ctx.prev.pt = coordinate.resolve(ctx, "titles.south").at(1)
return ctx
})
}
for (i, milestone) in milestones.enumerate() {
draw-milestone(i, ..milestone)
}
})
}
},
)
},
)
},
)
},
)
#let headerline(..args) = {
let groups = args.pos()
let headers = ()
let current_group = ()
let total = 0
let parse_entry(e) = {
if type(e) == array {
return e
} else {
return (e, 1)
}
}
for grp in groups {
if type(grp) == array {
current_group.push(grp)
total += grp.at(1)
} else if type(grp) == dictionary {
if current_group.len() > 0 {
headers.push(current_group)
}
headers.push((titles: grp.group.map(parse_entry)))
total += grp.group.map(n => parse_entry(n).at(1)).sum()
} else {
current_group.push((grp, 1))
total += 1
}
}
if current_group.len() > 0 {
headers.push((titles: current_group))
}
return ((type: "header", headers: headers, total: total),)
}
#let group(..args) = {
return (group: args.pos())
}
#let task(name, style: none, ..lines) = {
let processed_lines = ()
for line in lines.pos() {
if type(line) == dictionary {
processed_lines.push(line)
} else {
let (from, to) = line
if style != none {
processed_lines.push((from: from, to: to, style: style))
} else {
processed_lines.push((from: from, to: to))
}
}
}
((type: "task", name: name, lines: processed_lines),)
}
#let taskgroup(title: none, tasks) = {
let extratask = ()
if title != none {
let min = none
let max = none
for task in tasks {
for l in task.lines {
if min == none or l.from < min {
min = l.from
}
if max == none or l.to > max {
max = l.to
}
}
}
extratask = ((type: "task", name: title, lines: ((from: min, to: max),)),)
}
((type: "taskgroup", tasks: extratask + tasks),)
}
#let milestone(body, at: none, ..options) = {
((type: "milestone", at: at, body: body, ..options.named()),)
}