Skip to content

tree: Node Depth #649

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ package called `cetz-plot`.
## Util
- `apply-transform` now allows passing a function that takes and returns a vector, instead of a transformation matrix.

## Tree
- Support specifying an items depth by setting the first data-value to an integer.

# 0.2.2

## Anchors
Expand Down
12 changes: 10 additions & 2 deletions src/lib/tree.typ
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,20 @@
}
assert(draw-node != none, message: "Node draw callback must be set!")

let build-node(tree, depth: 0, sibling: 0) = {
let build-node(tree-arr, depth: 0, sibling: 0) = {
let children = ()
let content = none

let (e, tree) = if type(tree-arr) == array and tree-arr.len() == 2 and type(tree-arr.at(0)) in (int,float) {
tree-arr
} else {
(1, tree-arr)
}
let depth = depth + e

if type(tree) == array {
children = tree.slice(1).enumerate().map(
((n, c)) => build-node(c, depth: depth + 1, sibling: n)
((n, c)) => build-node(c, depth: depth, sibling: n)
)
content = tree.at(0)
} else {
Expand Down
Binary file modified tests/tree/ref/1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions tests/tree/test.typ
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,9 @@
})
h(.1cm)
}

// Specify branch lengths
#test-case({
cetz.draw.set-style(content: (frame: "rect", padding: .1))
cetz.tree.tree(([A], (1, ([B], (1, [C]), (1, [D]))), (2, [E])))
})
Loading