Published
Edited
Feb 18, 2021
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
groupedData = d3.group(data, v=>v.date, v=>v.group);
Insert cell
stackKeys = Array.from(new Set(data.map(d => d[secondaryKey])).values())
Insert cell
tableData = {
// How we want to aggregate the data
const reducer = v => d3.sum(v, d => d.amount);

return Array.from(groupedData.entries())
.map(g => {
const obj = {};
obj["date"] = g[0];
for (let col of stackKeys) {
const vals = g[1].get(col);
obj[col] = !vals ? 0 : reducer(Array.from(vals.values()));
}
return obj;
})
.sort((a, b) => a["date"] - b["date"]);
}
Insert cell
d3.stack().keys(stackKeys)(tableData)
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more