Public
Edited
Oct 24, 2023
4 forks
59 stars
Also listed in…
d3-hierarchy
Insert cell
Insert cell
data = [
{name: "Floyd Mayweather", sport: "Boxing", nation: "United States", earnings: 285},
{name: "Lionel Messi", sport: "Soccer", nation: "Argentina", earnings: 111},
{name: "Cristiano Ronaldo", sport: "Soccer", nation: "Portugal", earnings: 108},
{name: "Conor McGregor", sport: "MMA", nation: "Ireland", earnings: 99},
{name: "Neymar", sport: "Soccer", nation: "Brazil", earnings: 90},
{name: "LeBron James", sport: "Basketball", nation: "United States", earnings: 85.5},
{name: "Roger Federer", sport: "Tennis", nation: "Switzerland", earnings: 77.2},
{name: "Stephen Curry", sport: "Basketball", nation: "United States", earnings: 76.9},
{name: "Matt Ryan", sport: "Football", nation: "United States", earnings: 67.3},
{name: "Matthew Stafford", sport: "Football", nation: "United States", earnings: 59.5}
]
Insert cell
Insert cell
group = d3.group(data, d => d.nation, d => d.sport)
Insert cell
Insert cell
hierarchy = d3.hierarchy(group)
Insert cell
Insert cell
hierarchy.height
Insert cell
hierarchy.data // root, has the undefined key
Insert cell
hierarchy.children[0].data // nation
Insert cell
hierarchy.children[0].children[0].data // sport
Insert cell
hierarchy.children[0].children[0].children[0].data // athlete
Insert cell
Insert cell
${hierarchy.descendants().map(
d => `${"> ".repeat(d.depth)} ${d.data[0] || d.data["name"] || "root"} / \n`
)}
Insert cell
Insert cell
hierarchy.count().value // count athletes in the hierarchy; equal to the original data.length
Insert cell
sums = hierarchy.copy().sum(d => d.earnings)
Insert cell
sums.value // total earnings, equal to d3.sum(data, d => d.earnings)
Insert cell
sums.find(d => d.data[0] === "United States").value // total earnings of athletes in the US
Insert cell
Insert cell
hierarchy.leaves().map(d => d.data)
Insert cell
Insert cell
Soccer = hierarchy.find(d => d.data[0] === "Soccer")
Insert cell
Federer = hierarchy.find(d => d.data.name === "Roger Federer")
Insert cell
Soccer.path(Federer).map(d => d.data[0] || d.data.name || "^root")
Insert cell
Insert cell
hierarchy.copy().sort((a, b) => d3.ascending(a.data[0], b.data[0])).children.map(d => d.data[0])
Insert cell
Insert cell
rollup = d3.rollup(
data,
v => d3.sum(v, d => d.earnings),
d => d.nation,
d => d.sport
)
Insert cell
R = d3.hierarchy(rollup)
Insert cell
Insert cell
R.leaves().map(d => [d.parent.data[0], ">", ...d.data].join(" "))
Insert cell
Insert cell
{
const names = [];
R.copy().sum(d => d[1]).eachBefore((d, index) =>
names.push(" ".repeat(d.depth) + `${d.depth}: ${d.data[0] || "All"}, ${d.value} (${index})`)
);
return html`<pre style="max-height: 15em; overflow-y: scroll;">${names.join(
"\n"
)}`;
}
Insert cell
Insert cell
{
const names = [];
R.copy().sum(d => d[1]).eachAfter((d, i) =>
names.push(" ".repeat(d.depth) + `${d.depth}: ${d.data[0] || "All"}, ${d.value} (${i})`)
);
return html`<pre style="max-height: 15em; overflow-y: scroll;">${names.join(
"\n"
)}`;
}
Insert cell
Insert cell
Insert cell
d3.median(hierarchy, d => d.data.earnings)
Insert cell
d3.extent(hierarchy, d => d.children && d.children.length)
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