New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
d3.group, d3.groupsum, etc. #75
Comments
I think the path forward here is to deprecate d3-collection entirely and adopt ES collections (Map, Set). The proposed d3.group works well for single-level grouping, but it would be nice to have something analogous to d3.nest for multi-level grouping. Here’s an example two-level nested group: new Map(Array.from(
group(data, d => d.name),
([name, values]) => [name, group(values, d => d.date)]
)) And here’s an example with multi-part keys using the bleeding edge array.flatMap: new Map(Array.from(group(data, d => d.name)).flatMap(([name, data]) => {
return Array.from(group(data, d => d.date), ([date, data]) => {
return [[name, date], data];
});
})) Perhaps there’s a d3.nest(data, ...keys) function? |
Some notes here: https://beta.observablehq.com/@mbostock/nested-groups |
These seem to work well: function nest(values, ...keys) {
return (function regroup(values, i) {
if (i >= keys.length) return values;
const map = group(values, keys[i]);
return new Map(Array.from(map, ([k, v]) => [k, regroup(v, i + 1)]));
})(values, 0);
} function rollup(values, reduce, ...keys) {
return (function regroup(values, i) {
if (i >= keys.length) return reduce(values);
const map = group(values, keys[i]);
return new Map(Array.from(map, ([k, v]) => [k, regroup(v, i + 1)]));
})(values, 0);
} |
Formerly known as multimap, as proposed here:
https://beta.observablehq.com/@mbostock/group
The text was updated successfully, but these errors were encountered: