Published
Edited
Jun 18, 2021
Importers
9 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
d3[variant]([["🍑", 1], ["🍑", 4], ["🍐", 2]], v => d3.sum(v, d => d[1]), d => d[0])
Insert cell
Insert cell
athletes = [
{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
earnings = d3.rollup(athletes, v => d3.sum(v, d => d.earnings), d => d.nation, d => d.sport)
Insert cell
Insert cell
[...earnings].flatMap(([k1, v1]) => [...v1].map(([k2, v2]) => ({nation: k1, sport: k2, earnings: v2})))
Insert cell
Insert cell
function unroll(rollup, keys, label = "value", p = {}) {
return Array.from(rollup, ([key, value]) =>
value instanceof Map
? unroll(value, keys.slice(1), label, Object.assign({}, { ...p, [keys[0]]: key } ))
: Object.assign({}, { ...p, [keys[0]]: key, [label] : value })
).flat();
}
Insert cell
Insert cell
unroll(earnings, ["nation", "sport"], "earnings")
Insert cell
Insert cell
function rollUnroll(data, reducer, keys, value) {
const rolled = d3.rollup(data, reducer, ...keys.map(k => d => d[k]));
return unroll(rolled, keys, value);
}
Insert cell
rollUnroll(athletes, v => d3.mean(v, d => d.earnings), ["sport"], "earnings")
Insert cell
Insert cell
function groupSum(data, keys, value) {
const rolled = d3.rollup(data, v => d3.sum(v, d => d[value]), ...keys.map(k => d => d[k]));
return unroll(rolled, keys, value);
}
Insert cell
groupSum(athletes, ["sport"], "earnings")
Insert cell
Insert cell
function nest(rollup) {
return Array.from(rollup, ([key, value]) =>
value instanceof Map
? { name: key, children: nest(value) }
: { name: key, value: value }
);
}
Insert cell
data = ({name: "Earnings", children: nest(earnings)})
Insert cell
import { chart } with { data, height } from "@d3/nested-treemap"
Insert cell
chart
Insert cell
Insert cell
d3 = require("d3-array@2.2")
Insert cell
import { radio } from "@jashkenas/inputs"
Insert cell
height = 200
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