Public
Edited
Apr 24, 2024
Fork of d3.stratify
1 fork
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 // los datos originales en la raíz
Insert cell
root.id // extraído por el descriptor de acceso .id () de Stratify
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
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
dot = require("@observablehq/graphviz")
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
d3 = require("d3-hierarchy@2", "d3-dsv@2")
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