Public
Edited
Oct 24, 2023
6 forks
Importers
31 stars
Insert cell
Insert cell
Insert cell
data = FileAttachment("flare-2.json").json()
Insert cell
root = d3.hierarchy(data)
Insert cell
Insert cell
Insert cell
counted = root.copy().count()
Insert cell
counted.value // the total number of leaves
Insert cell
counted.children[0].value // the number of leaves in the first subpackage
Insert cell
Insert cell
Insert cell
summed = root.copy().sum(d => d.value)
Insert cell
summed.value // the aggregate file size in bytes
Insert cell
summed.children[0].value // the aggregate size of the first subpackage
Insert cell
Insert cell
root.copy().sum(d => 1).value // the total number of nodes
Insert cell
root.copy().sum(d => d.name.length).value // the total name length?
Insert cell
Insert cell
Insert cell
Insert cell
sorted = root.copy()
.sum(d => d.value)
.sort((a, b) => d3.descending(a.value, b.value))
Insert cell
sorted.children.map(d => [d.data.name, d.value])
Insert cell
Insert cell
root.copy()
.sum(d => d.value)
.sort((a, b) => d3.descending(a.height, b.height) || d3.descending(a.value, b.value))
Insert cell
Insert cell
root.copy()
.sum(d => d.value)
.sort((a, b) => d3.descending(a.height, b.height) || d3.ascending(a.data.name, b.data.name))
Insert cell
Insert cell
Insert cell
Insert cell
{
const names = [];
root.each(d => names.push(" ".repeat(d.depth) + `${d.depth}: ${d.data.name}`));
return html`<pre style="max-height: 10em; overflow-y: scroll;">${names.join("\n")}`;
}
Insert cell
Insert cell
{
const names = [];
root.eachBefore(d => names.push(" ".repeat(d.depth) + `${d.depth}: ${d.data.name}`));
return html`<pre style="max-height: 10em; overflow-y: scroll;">${names.join("\n")}`;
}
Insert cell
Insert cell
{
const names = [];
root.eachAfter(d => names.push(" ".repeat(d.depth) + `${d.depth}: ${d.data.name}`));
return html`<pre style="max-height: 10em; overflow-y: scroll;">${names.join("\n")}`;
}
Insert cell
Insert cell
chaos = d3.stratify()(d3.csvParse(await FileAttachment("chaos.csv").text()))
Insert cell
{ let i = 0; return digraph(chaos.copy().each(d => d.label = `${d.data.id} = ${i++}`)); }
Insert cell
{ let i = 0; return digraph(chaos.copy().eachBefore(d => d.label = `${d.data.id} = ${i++}`)); }
Insert cell
{ let i = 0; return digraph(chaos.copy().eachAfter(d => d.label = `${d.data.id} = ${i++}`)); }
Insert cell
Insert cell
Insert cell
dot = require("@observablehq/graphviz@0.2")
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];
edge [fontname="var(--sans-serif)" fontsize=12];
${hierarchy.descendants().map(node => `"${id.get(node)}" [label="${node.label}"]`).join("; ")}
${hierarchy.links().map(({source, target}) => `"${id.get(source)}" -> "${id.get(target)}"`).join("; ")}
}`;
}
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