Coder Social home page Coder Social logo

d3.group, d3.groupsum, etc. about d3-array HOT 3 CLOSED

d3 avatar d3 commented on May 1, 2024
d3.group, d3.groupsum, etc.

from d3-array.

Comments (3)

mbostock avatar mbostock commented on May 1, 2024 2

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

from d3-array.

mbostock avatar mbostock commented on May 1, 2024

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?

from d3-array.

mbostock avatar mbostock commented on May 1, 2024

Some notes here: https://beta.observablehq.com/@mbostock/nested-groups

from d3-array.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.