Published
Edited
Jul 15, 2020
16 stars
Insert cell
Insert cell
data = [
{name: "Jan", value: 15},
{name: "Feb", value: 7},
{name: "Mar", value: 3},
{name: "Apr", value: 10},
{name: "May", value: 15},
{name: "May", value: 9},
{name: "Jun", value: 15},
{name: "Jul", value: 0},
{name: "Aug", value: 0},
{name: "Sep", value: 0},
{name: "Oct", value: 0},
{name: "Nov", value: 0},
{name: "Dec", value: 0}
]
Insert cell
Insert cell
d3.group(data, d => d.name)
Insert cell
Insert cell
d3.rollup(data, g => d3.sum(g, d => d.value), d => d.name)
Insert cell
Insert cell
d3.rollup(data, g => g.reduce((p, v) => (p.value += v.value, p), {...g[0], value: 0}), d => d.name)
Insert cell
Insert cell
d3.rollup(
data,
group => {
const reduce = {...group[0], value: 0};
for (const {value} of group) {
reduce.value += value;
}
return reduce;
},
d => d.name
)
Insert cell
Insert cell
Array.from(d3.rollup(data, g => g.reduce((p, v) => (p.value += v.value, p), {...g[0], value: 0}), d => d.name).values())
Insert cell
Insert cell
Array.from(d3.group(data, d => d.name), ([name, group]) => ({name, value: d3.sum(group, d => d.value)}))
Insert cell
Insert cell
d3.groups(data, d => d.name).map(([name, group]) => ({name, value: d3.sum(group, d => d.value)}))
Insert cell
Insert cell
d3 = require("d3-array@2")
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