Skip to content
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

Closed
mbostock opened this issue Sep 7, 2018 · 3 comments
Closed

d3.group, d3.groupsum, etc. #75

mbostock opened this issue Sep 7, 2018 · 3 comments

Comments

@mbostock
Copy link
Member

mbostock commented Sep 7, 2018

Formerly known as multimap, as proposed here:

https://beta.observablehq.com/@mbostock/group

@mbostock
Copy link
Member Author

mbostock commented Oct 20, 2018

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?

@mbostock
Copy link
Member Author

@mbostock
Copy link
Member Author

mbostock commented Oct 20, 2018

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);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant