Public
Edited
Oct 24, 2023
16 forks
37 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
childColumn = links.columns[0]
Insert cell
parentColumn = links.columns[1]
Insert cell
Insert cell
stratify = d3.stratify()
.id(d => d[childColumn])
.parentId(d => d[parentColumn])
Insert cell
root = stratify(links)
Insert cell
Insert cell
root.data // the original data at the root
Insert cell
root.id // extracted by stratify’s .id() accessor
Insert cell
root.parent // the root has no parent
Insert cell
root.children // children nodes, nodes for which the .parentId() accessor indicated the root node’s id.
Insert cell
root.depth // the root has depth 0, its children depth 1, its grand-children depth 2, etc.
Insert cell
root.height // node.height is the length of the longest branch originating from that node
Insert cell
Insert cell
root.children[0] // "b", the first child of the root node has depth 1, height 1, and its own children…
Insert cell
Insert cell
d3.stratify()([{id: "1", parentId: "2"}])
Insert cell
d3.stratify()([{id: "1", parentId: "2"}, {id: "2", parentId: "1"}])
Insert cell
d3.stratify()([{id: "1", parentId: ""}, {id: "2", parentId: ""}])
Insert cell
Insert cell
disk = d3.stratify().path(d => d)([
"/usr/bin/grep",
"/usr/bin/yes",
"/usr/share/man/man1/grep.1"
])
Insert cell
Tree(disk, {label: d => d.id, width: 780, padding: 2})
Insert cell
Insert cell
d3.csvFormat(root.descendants().map(d => d.data))
Insert cell
Insert cell
root.descendants().map(d => ({
child: d.id,
parent: d.parent ? d.parent.id : ""
}))
Insert cell
Insert cell
function links2dot(links) {
return links
.map(l => [l[links.columns[1]], l[links.columns[0]]])
.filter(([source, target]) => source && target)
.map(([source, target]) => `${source} -> ${target}`)
.join("; ");
}
Insert cell
import {Tree} from "@d3/tree"
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more