d3-collection
https://github.com/d3/d3-collection
Table of Contents
Objects
d3.keys(object)
d3.values(object)
d3.entries(object)
Nests
Think of it like the GROUP BY operator in SQL, except you can have multiple levels of grouping, and the resulting output is a tree rather than a flat table.
d3.nest()
- Creates a new nest operator.
- The set of keys is initially empty.
nest.key(key)
- Registers a new key function.
- The
key
function will be invoked for each element in the input array and must return a string identifier to assign the element to its group. - Each time a
key
is registered, it is pushed onto the end of the internal array of keys, and the nest operator applies an additional level of nesting.
nest.sortKeys(comparator)
- Sorts key values for the current key using the specified comparator function, such as
d3.ascending
ord3.descending
.
Data:
var entries = d3.nest()
.key(function(d) { return d.year; }).sortKeys(d3.ascending)
.key(function(d) { return d.variety; }).sortKeys(d3.descending)
.entries(yields);
Result:
[{key: "1931", values: [
{key: "Manchuria", values: [
{yield: 27.00, variety: "Manchuria", year: 1931, site: "University Farm"},
{yield: 48.87, variety: "Manchuria", year: 1931, site: "Waseca"},
{yield: 27.43, variety: "Manchuria", year: 1931, site: "Morris"}, ...]},
{key: "Glabron", values: [
{yield: 43.07, variety: "Glabron", year: 1931, site: "University Farm"},
{yield: 55.20, variety: "Glabron", year: 1931, site: "Waseca"}, ...]}, ...]},
{key: "1932", values: ...}]
nest.sortValues(comparator)
nest.rollup(function)
- Applied on each group of leaf elements, which means that it takes the array of values.
nest.map(array)
- Applies the nest operator to the specified array, returning a nested map.
nest.object(array)
- Applies the nest operator to the specified array, returning a nested object.
nest.entries(array)
- Applies the nest operator to the specified array, returning an array of key-values entries.
Sort by rollup keys or values howto
Sort by rollup keys
Sort by rollup values