Public
Edited
Oct 24, 2023
10 forks
23 stars
Insert cell
Insert cell
Insert cell
graph = {
let current = 0,
N = chaos.descendants().length;
const view = d3.create("div").style("user-select", "none");

function draw(node) {
view
.html("")
.node()
.appendChild(
digraph(
chaos.copy()[order]((d, i) => {
d.label = `${d.data.id} ${i === 0 ? "*" : ""}`;
d.style = i === current ? "filled" : "";
return d;
})
)
);
}

const increment = () => draw((current = ++current % N));
view.on("click", increment);
d3.select(document.body).on("keyup", increment);

draw();

return view.node();
}
Insert cell
function digraph(hierarchy) {
const id = new Map(hierarchy.descendants().map((node, i) => [node, i]));
return dot`digraph {
rankdir = TB;
node [fontname="var(--sans-serif)" fontsize=12];
${hierarchy
.descendants()
.map(
node =>
`"${id.get(node)}" [label="${node.label}"] [style="${node.style}"]`
)
.join("; ")}
${hierarchy
.links()
.map(({ source, target }) => `"${id.get(source)}" -> "${id.get(target)}"`)
.join("; ")}
}`;
}
Insert cell
dot = require("@observablehq/graphviz@0.2")
Insert cell
import {chaos} from "@d3/visiting-a-d3-hierarchy"
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