@r_masseater/tako 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -42,6 +42,24 @@ bun add @r_masseater/tako
42
42
  - `nested(root: (kind: ..., body: [...], children: (...)))` draws boxes inside boxes.
43
43
  - `fork(before: (...), decide: (...), yes: (...), no: (...), yes-label: [...], no-label: [...])` splits a flow at a condition into two branches.
44
44
 
45
+ `isotype(rows: ((label: [...], value: 30), ...), icon: "person", per: 10, unit: "...")` repeats one icon per `per` units and writes the exact value at the end of the row.
46
+ `quadrant(x-axis: (...), y-axis: (...), points: ((x: 0.2, y: 0.8, label: [...], kind: ..., hero: true), ...))` places points on two crossed axes.
47
+ `annotated(picture: ..., notes: ((side: "left", x: 0.1, y: 0.2, name: [...], note: [...]), ...))` draws leader lines from a picture to names on its sides.
48
+ `sysmap(nodes: ((id: ..., kind: ..., body: [...]), ...), links: ((from: ..., to: ..., label: [...], condition: true), ...))` lays nodes out in layers from the links and curves the arrows around nodes in between.
49
+
50
+ ## Charts
51
+
52
+ The chart parts wrap [primaviz](https://typst.app/universe/package/primaviz), which Typst downloads on the first render, so import them separately.
53
+
54
+ ```typst
55
+ #import "/.tako/charts.typ": *
56
+ ```
57
+
58
+ - `trend(labels: (...), series: ((name: ..., values: (...)),))` draws a line.
59
+ - `stacked(labels: (...), series: (...))` stacks bars in shades of one hue and names each part inside it.
60
+ - `funnel(stages: (([name], 1000), ...))` narrows by stage.
61
+ - `sankey(nodes: ((name: ..., kind: ...), ...), flows: ((from: "a", to: "b", value: 3), ...))` draws flows between nodes by name.
62
+
45
63
  ## Render
46
64
 
47
65
  ```sh
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@r_masseater/tako",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "description": "Draws infographics from a few lines of Typst: parts for the six kinds of element, flows, a hero number and bars, laid out automatically in light and dark.",
5
5
  "keywords": [
6
6
  "diagram",
@@ -1,16 +1,10 @@
1
1
  ---
2
2
  name: draw-infographic
3
3
  description: >
4
- Use when an infographic, a flow with a hero number, or a bar chart with a limit line is wanted
5
- as a figure: write a few lines of Typst with the tako parts and tako render lays it out and
6
- writes light and dark SVG or PDF.
7
- metadata:
8
- type: core
9
- library: "@r_masseater/tako"
10
- library_version: 0.1.0
11
- sources:
12
- - typ/**
13
- - README.md
4
+ Use when an infographic is wanted as a figure: a flow with a hero number, a fork, a cycle, a tree,
5
+ nested boxes, a timeline, a system map, bars, a line, stacked bars, a funnel, a sankey, an isotype row,
6
+ a two-axis map or an annotated picture. Write a few lines of Typst with the tako parts and tako render
7
+ lays it out and writes light and dark SVG or PDF.
14
8
  ---
15
9
 
16
10
  # draw-infographic
@@ -45,7 +39,9 @@ Give each node the kind that matches what happens to it: `person` acts, `system`
45
39
 
46
40
  `bars(rows: (...), limit: (...))` draws bars in the amount color and a dashed limit line in the condition color.
47
41
 
48
- Pick the part by what the figure shows: `flow` for steps, `fork` for a split at a condition, `cycle` for a repeat, `tree` for parent and child, `nested` for inside and outside, `timeline` for dates, `bars` for a size against a limit. The README lists their arguments.
42
+ Pick the part by what the figure shows: `flow` for steps, `fork` for a split at a condition, `cycle` for a repeat, `tree` for parent and child, `nested` for inside and outside, `timeline` for dates, `sysmap` for who hands what to whom, `bars` for a size against a limit, `isotype` for a count, `quadrant` for a position on two axes, `annotated` for the parts of a picture. The README lists their arguments.
43
+
44
+ The chart parts `trend`, `stacked`, `funnel` and `sankey` come from `#import "/.tako/charts.typ": *`. Typst downloads primaviz for them on the first render, so import that file only for a figure that needs a chart.
49
45
 
50
46
  ## Render
51
47
 
@@ -0,0 +1,30 @@
1
+ #import "theme.typ": tokens, kinds
2
+
3
+ #let annotated(picture: none, notes: (), gap: 56pt, note-width: 220pt) = context {
4
+ let m = measure(picture)
5
+ let left-notes = notes.filter(n => n.side == "left")
6
+ let right-notes = notes.filter(n => n.side == "right")
7
+ let lead = if left-notes.len() == 0 { 0pt } else { note-width + gap }
8
+ let tail = if right-notes.len() == 0 { 0pt } else { note-width + gap }
9
+ let stroke = 2pt + tokens.sub
10
+ box(width: lead + m.width + tail, height: m.height, {
11
+ place(top + left, dx: lead, picture)
12
+ for (side, group) in (("left", left-notes), ("right", right-notes)) {
13
+ for n in group {
14
+ let tx = lead + n.x * m.width
15
+ let ty = n.y * m.height
16
+ let nx = if side == "left" { lead - gap } else { lead + m.width + gap }
17
+ place(top + left, curve(stroke: stroke, curve.move((nx, ty)), curve.quad(((nx + tx) / 2, ty), (tx, ty))))
18
+ place(top + left, dx: tx - 5pt, dy: ty - 5pt, circle(radius: 5pt, fill: tokens.sub, stroke: none))
19
+ let text-box = box(width: note-width, {
20
+ align(if side == "left" { right } else { left }, text(size: 18pt, weight: "bold", fill: tokens.ink, n.name))
21
+ if n.at("note", default: none) != none {
22
+ align(if side == "left" { right } else { left }, text(size: 15pt, fill: tokens.sub, n.note))
23
+ }
24
+ })
25
+ let size = measure(text-box)
26
+ place(top + left, dx: if side == "left" { lead - gap - note-width - 8pt } else { nx + 8pt }, dy: ty - size.height / 2, text-box)
27
+ }
28
+ }
29
+ })
30
+ }
package/typ/charts.typ ADDED
@@ -0,0 +1,65 @@
1
+ #import "@preview/primaviz:0.12.1" as viz
2
+ #import "theme.typ": tokens, kinds
3
+
4
+ #let base-theme = (
5
+ text-color: tokens.ink,
6
+ text-color-light: tokens.sub,
7
+ text-color-inverse: tokens.bg,
8
+ background: tokens.bg,
9
+ axis-stroke: 1pt + tokens.line,
10
+ grid-stroke: 1pt + tokens.line,
11
+ marker-stroke: 2pt + tokens.bg,
12
+ base-size: 15pt,
13
+ base-gap: 10pt,
14
+ )
15
+
16
+ #let with-palette(colors) = (..base-theme, palette: colors)
17
+
18
+ #let shades(color, count) = range(count).map(i => color.lighten(i * 55% / calc.max(count - 1, 1)))
19
+
20
+ #let trend(labels: (), series: (), kind: "amount", width: 520pt, height: 280pt) = {
21
+ let stroke = kinds.at(kind).stroke
22
+ let colors = if series.len() == 1 { (stroke,) } else { (stroke, kinds.system.stroke, kinds.data.stroke, kinds.person.stroke) }
23
+ let theme = with-palette(colors)
24
+ if series.len() == 1 {
25
+ viz.line-chart((labels: labels, values: series.at(0).values), width: width, height: height, theme: theme, show-values: true)
26
+ } else {
27
+ viz.multi-line-chart((labels: labels, series: series), width: width, height: height, theme: theme)
28
+ }
29
+ }
30
+
31
+ #let stacked(labels: (), series: (), kind: "amount", width: 520pt, height: 300pt) = {
32
+ let colors = shades(kinds.at(kind).stroke, series.len())
33
+ let notes = series.enumerate().map(((i, s)) => {
34
+ let below = series.slice(0, i).map(p => p.values.at(0)).sum(default: 0)
35
+ (type: "content", x: 0, y: below + s.values.at(0) / 2, body: text(size: 14pt, fill: tokens.bg, weight: "bold", s.name), anchor: "center")
36
+ })
37
+ viz.stacked-bar-chart(
38
+ (labels: labels, series: series),
39
+ width: width,
40
+ height: height,
41
+ show-legend: false,
42
+ annotations: notes,
43
+ theme: with-palette(colors),
44
+ )
45
+ }
46
+
47
+ #let funnel(stages: (), kind: "amount", width: 520pt, height: 300pt) = viz.funnel-chart(
48
+ stages,
49
+ width: width,
50
+ height: height,
51
+ theme: with-palette((kinds.at(kind).stroke,)),
52
+ )
53
+
54
+ #let sankey(nodes: (), flows: (), width: 560pt, height: 320pt) = {
55
+ let names = nodes.map(n => if type(n) == str { n } else { n.name })
56
+ let colors = nodes.map(n => if type(n) == str { kinds.system.stroke } else { kinds.at(n.at("kind", default: "system")).stroke })
57
+ let at = name => names.position(n => n == name)
58
+ viz.sankey-chart(
59
+ (nodes: names, flows: flows.map(f => (from: at(f.from), to: at(f.to), value: f.value))),
60
+ width: width,
61
+ height: height,
62
+ show-values: true,
63
+ theme: with-palette(colors),
64
+ )
65
+ }
@@ -0,0 +1,27 @@
1
+ #import "theme.typ": tokens, kinds
2
+ #import "node.typ": icon-of
3
+
4
+ #let isotype(rows: (), icon: "person", kind: "amount", per: 1, group: 5, unit: "", size: 30pt) = context {
5
+ let color = kinds.at(kind).stroke
6
+ let labels = rows.map(r => measure(text(size: 17pt, r.label)).width)
7
+ let label-width = calc.max(..labels) + 16pt
8
+ let row-height = size + 22pt
9
+ let count-of = r => int(calc.round(r.value / per))
10
+ let widest = calc.max(..rows.map(count-of))
11
+ let pitch = size + 6pt
12
+ let groups = calc.ceil(widest / group)
13
+ let span = widest * pitch + (groups - 1) * 14pt
14
+ box(width: label-width + span + 110pt, height: row-height * rows.len(), {
15
+ for (i, r) in rows.enumerate() {
16
+ let y = row-height * i
17
+ place(top + left, dy: y + size / 2 - 12pt, box(width: label-width - 12pt, align(right, text(size: 17pt, fill: tokens.ink, r.label))))
18
+ let n = count-of(r)
19
+ for k in range(n) {
20
+ let gap = calc.quo(k, group) * 14pt
21
+ place(top + left, dx: label-width + k * pitch + gap, dy: y, icon-of(icon, color, size))
22
+ }
23
+ let end = label-width + n * pitch + calc.quo(calc.max(n - 1, 0), group) * 14pt
24
+ place(top + left, dx: end + 10pt, dy: y + size / 2 - 14pt, text(size: 20pt, weight: "bold", fill: tokens.ink, str(r.value) + unit))
25
+ }
26
+ })
27
+ }
@@ -0,0 +1,33 @@
1
+ #import "theme.typ": tokens, kinds
2
+ #import "node.typ": link
3
+
4
+ #let quadrant(points: (), x-axis: (), y-axis: (), size: 420pt) = context {
5
+ let pad = 120pt
6
+ let plot = size
7
+ let dot = 11pt
8
+ box(width: plot + pad * 2, height: plot + 80pt, {
9
+ let ox = pad
10
+ let oy = 20pt
11
+ place(top + left, curve(stroke: 2pt + tokens.line, curve.move((ox, oy + plot / 2)), curve.line((ox + plot, oy + plot / 2))))
12
+ place(top + left, curve(stroke: 2pt + tokens.line, curve.move((ox + plot / 2, oy)), curve.line((ox + plot / 2, oy + plot))))
13
+ place(top + left, dx: ox, dy: oy + plot + 12pt, box(width: plot, {
14
+ place(left, text(size: 16pt, fill: tokens.sub, x-axis.low))
15
+ place(right, text(size: 16pt, fill: tokens.sub, x-axis.high))
16
+ align(center, text(size: 17pt, weight: "bold", fill: tokens.ink, x-axis.name))
17
+ }))
18
+ place(top + left, dx: 0pt, dy: oy, box(width: pad - 14pt, height: plot, {
19
+ place(top + right, text(size: 16pt, fill: tokens.sub, y-axis.high))
20
+ place(bottom + right, text(size: 16pt, fill: tokens.sub, y-axis.low))
21
+ place(horizon + right, text(size: 17pt, weight: "bold", fill: tokens.ink, y-axis.name))
22
+ }))
23
+ for p in points {
24
+ let spec = kinds.at(p.at("kind", default: "system"))
25
+ let big = p.at("hero", default: false)
26
+ let d = if big { dot * 1.7 } else { dot }
27
+ let cx = ox + p.x * plot
28
+ let cy = oy + (1 - p.y) * plot
29
+ place(top + left, dx: cx - d / 2, dy: cy - d / 2, circle(radius: d / 2, fill: spec.fill, stroke: 2.5pt + spec.stroke))
30
+ place(top + left, dx: cx + d / 2 + 8pt, dy: cy - 12pt, text(size: if big { 20pt } else { 17pt }, weight: if big { "bold" } else { "regular" }, fill: tokens.ink, p.label))
31
+ }
32
+ })
33
+ }
package/typ/sysmap.typ ADDED
@@ -0,0 +1,91 @@
1
+ #import "theme.typ": tokens, kinds
2
+ #import "shapes.typ": shape, outer-width
3
+ #import "node.typ": *
4
+
5
+ #let layers-of(nodes, links) = {
6
+ let layer = (:)
7
+ for n in nodes {
8
+ layer.insert(n.id, 0)
9
+ }
10
+ for _ in nodes {
11
+ for l in links {
12
+ let next = layer.at(l.from) + 1
13
+ if next > layer.at(l.to) and next < nodes.len() {
14
+ layer.insert(l.to, next)
15
+ }
16
+ }
17
+ }
18
+ layer
19
+ }
20
+
21
+ #let sysmap(nodes: (), links: (), gap-x: 110pt, gap-y: 40pt) = context {
22
+ let layer = layers-of(nodes, links)
23
+ let by-id = (:)
24
+ for n in nodes {
25
+ by-id.insert(n.id, n)
26
+ }
27
+ let sized = uniform(nodes)
28
+ let h = sized.height
29
+ let width-of = (:)
30
+ for (i, n) in nodes.enumerate() {
31
+ width-of.insert(n.id, sized.widths.at(i))
32
+ }
33
+ let depth = calc.max(..layer.values()) + 1
34
+ let center = (:)
35
+ let col-widths = ()
36
+ let x = 0pt
37
+ let tallest = 0pt
38
+ let rows-of = range(depth).map(d => nodes.filter(n => layer.at(n.id) == d))
39
+ for rows in rows-of {
40
+ tallest = calc.max(tallest, rows.len() * (h + gap-y) - gap-y)
41
+ }
42
+ for (d, members) in rows-of.enumerate() {
43
+ let ranked = members.enumerate().map(((i, n)) => {
44
+ let heads = links.filter(l => l.to == n.id and center.keys().contains(l.from)).map(l => center.at(l.from).at(1))
45
+ let key = if heads.len() == 0 { i * 1pt } else { heads.sum() / heads.len() }
46
+ (key: key, node: n)
47
+ }).sorted(key: r => r.key / 1pt).map(r => r.node)
48
+ let w = calc.max(..ranked.map(n => width-of.at(n.id)))
49
+ let column = ranked.len() * (h + gap-y) - gap-y
50
+ let top = (tallest - column) / 2
51
+ for (i, n) in ranked.enumerate() {
52
+ center.insert(n.id, (x + w / 2, top + i * (h + gap-y) + h / 2))
53
+ }
54
+ x += w + gap-x
55
+ }
56
+ let total = x - gap-x
57
+ box(width: total, height: tallest, {
58
+ for n in nodes {
59
+ let c = center.at(n.id)
60
+ at-center(c, width-of.at(n.id), h, build(n, h, width-of.at(n.id)))
61
+ }
62
+ for l in links {
63
+ let a = center.at(l.from)
64
+ let b = center.at(l.to)
65
+ let from-box = (width-of.at(l.from), h)
66
+ let to-box = (width-of.at(l.to), h)
67
+ let start = edge-point(a, ..from-box, b)
68
+ let end = edge-point(b, ..to-box, a)
69
+ let dx = (end.at(0) - start.at(0)) / 1pt
70
+ let dy = (end.at(1) - start.at(1)) / 1pt
71
+ let length = calc.sqrt(dx * dx + dy * dy)
72
+ let middle = ((a.at(1) + b.at(1)) / 2)
73
+ let between = nodes.filter(n => layer.at(n.id) > calc.min(layer.at(l.from), layer.at(l.to)) and layer.at(n.id) < calc.max(layer.at(l.from), layer.at(l.to)))
74
+ let blocked = between.filter(n => calc.abs((center.at(n.id).at(1) - middle) / 1pt) < h / 1pt)
75
+ let bend = if blocked.len() == 0 { calc.min(length * 0.14, 30) * 1pt } else {
76
+ let below = blocked.map(n => center.at(n.id).at(1)).sum() / blocked.len() > middle
77
+ let sign = if (b.at(0) > a.at(0)) == below { 1 } else { -1 }
78
+ sign * h * 1.5
79
+ }
80
+ let conditional = l.at("condition", default: false)
81
+ link(
82
+ start,
83
+ end,
84
+ color: if conditional { kinds.condition.stroke } else { tokens.sub },
85
+ dashed: conditional,
86
+ bend: bend,
87
+ label: l.at("label", default: none),
88
+ )
89
+ }
90
+ })
91
+ }
package/typ/tako.typ CHANGED
@@ -69,3 +69,7 @@
69
69
  #import "timeline.typ": timeline
70
70
  #import "nested.typ": nested
71
71
  #import "fork.typ": fork
72
+ #import "sysmap.typ": sysmap
73
+ #import "isotype.typ": isotype
74
+ #import "quadrant.typ": quadrant
75
+ #import "annotated.typ": annotated