数据分组
¥Grouping data
对离散值进行分组。
¥Group discrete values.
group(iterable, ...keys)
示例 · 源代码 · 将指定的可迭代值分组为一个 InternMap 数组,该数组的元素为键和值。例如,按物种字段对 企鹅样本数据集 进行分组:
¥Examples · Source · Groups the specified iterable of values into an InternMap from key to array of value. For example, to group the penguins sample dataset by species field:
const species = d3.group(penguins, (d) => d.species);
要获取物种字段为阿德利企鹅的元素:
¥To get the elements whose species field is Adelie:
species.get("Adelie") // Array(152)
如果指定了多个键,则返回一个嵌套的 InternMap。例如:
¥If more than one key is specified, a nested InternMap is returned. For example:
const speciesSex = d3.group(penguins, (d) => d.species, (d) => d.sex)
要获取物种为阿德利企鹅且性别为雌性的企鹅数量:
¥To get the penguins whose species is Adelie and whose sex is FEMALE:
speciesSex.get("Adelie").get("FEMALE") // Array(73)
元素按每个键的第一个实例的顺序返回。
¥Elements are returned in the order of the first instance of each key.
groups(iterable, ...keys)
const species = d3.groups(penguins, (d) => d.species); // [["Adelie", Array(152)], …]
相当于 group,但返回的是 [key, value] 条目的数组,而不是 map。如果指定了多个键,则每个值都将是一个包含 [key, value] 元素的嵌套数组。元素按每个键的第一个实例的顺序返回。
¥Equivalent to group, but returns an array of [key, value] entries instead of a map. If more than one key is specified, each value will be a nested array of [key, value] entries. Elements are returned in the order of the first instance of each key.
rollup(iterable, reduce, ...keys)
示例 · 源代码 · 将指定的可迭代对象按键分组并缩减为 InternMap 类型,并将键值从键值缩减为值。例如,按物种字段对 企鹅样本数据集 进行分组和计数:
¥Examples · Source · Groups and reduces the specified iterable of values into an InternMap from key to reduced value. For example, to group and count the penguins sample dataset by species field:
const speciesCount = d3.rollup(penguins, (D) => D.length, (d) => d.species);
要获取物种为阿德利企鹅的数量:
¥To get the count of penguins whose species is Adelie:
speciesCount.get("Adelie") // 152
如果指定了多个键,则返回一个嵌套的 InternMap。例如:
¥If more than one key is specified, a nested InternMap is returned. For example:
const speciesSexCount = d3.rollup(penguins, (D) => D.length, (d) => d.species, (d) => d.sex);
要获取物种为阿德利企鹅且性别为雌性的企鹅数量:
¥To get the count of penguins whose species is Adelie and whose sex is FEMALE:
speciesSexCount.get("Adelie").get("FEMALE") // 73
元素按每个键的第一个实例的顺序返回。
¥Elements are returned in the order of the first instance of each key.
rollups(iterable, reduce, ...keys)
const speciesCounts = d3.rollups(penguins, (D) => D.length, (d) => d.species); // [["Adelie", 152], …]
相当于 rollup,但返回的是 [key, value] 条目的数组,而不是 map。如果指定了多个键,则每个值都将是一个包含 [key, value] 元素的嵌套数组。元素按每个键的第一个实例的顺序返回。
¥Equivalent to rollup, but returns an array of [key, value] entries instead of a map. If more than one key is specified, each value will be a nested array of [key, value] entries. Elements are returned in the order of the first instance of each key.
index(iterable, ...keys)
Uses rollup with a reducer that extracts the first element from each group, and throws an error if the group has more than one element.例如,按日期对 aapl 相同数据集 进行索引:
¥Uses rollup with a reducer that extracts the first element from each group, and throws an error if the group has more than one element. For example, to index the aapl same dataset by date:
const aaplDate = d3.index(aapl, (d) => d.Date);
然后,你可以快速按日期检索值:
¥You can then quickly retrieve a value by date:
aaplDate.get(new Date("2013-12-31")).Close // 80.145714
元素按输入顺序返回。
¥Elements are returned in input order.
indexes(iterable, ...keys)
类似 index,但返回一个包含 [键,值] 条目的数组,而不是 Map。这可能没有任何用处,但为了与 groups 和 rollups 对称而包含。
¥Like index, but returns an array of [key, value] entries instead of a map. This probably isn’t useful for anything, but is included for symmetry with groups and rollups.
flatGroup(iterable, ...keys)
示例 · 源代码 · 等同于 group,但返回的是 [key0, key1, …, values] 的扁平数组,而不是嵌套的 Map;适用于迭代所有组。
¥Examples · Source · Equivalent to group, but returns a flat array of [key0, key1, …, values] instead of nested maps; useful for iterating over all groups.
flatRollup(iterable, reduce, ...keys)
示例 · 源代码 · 等同于 rollup,但返回一个 [key0, key1, …, value] 的扁平数组,而不是嵌套映射;适用于迭代所有组。
¥Examples · Source · Equivalent to rollup, but returns a flat array of [key0, key1, …, value] instead of nested maps; useful for iterating over all groups.
groupSort(iterable, comparator, key)
示例 · 源代码 · 根据指定的键函数对指定的可迭代对象进行分组,根据指定的比较器对分组进行排序,然后按排序后的键值数组返回。例如,按体重中位数升序排列 企鹅样本数据集 的物种:
¥Examples · Source · Groups the specified iterable of elements according to the specified key function, sorts the groups according to the specified comparator, and then returns an array of keys in sorted order. For example, to order the species of the penguins sample dataset by ascending median body mass:
d3.groupSort(penguins, (D) => d3.median(D, (d) => d.body_mass_g), (d) => d.species) // ["Adelie", "Chinstrap", "Gentoo"]
对于降序,请对组值取反:
¥For descending order, negate the group value:
d3.groupSort(penguins, (D) => -d3.median(D, (d) => d.body_mass_g), (d) => d.species) // ["Gentoo", "Adelie", "Chinstrap"]
如果传递的是比较器而不是访问器(即,如果第二个参数是一个只接受两个参数的函数),则会要求比较两个组 a 和 b。如果 a 应该在 b 之前,则返回负值;如果 a 应该在 b 之后,则返回正值;如果是部分排序,则返回零。
¥If a comparator is passed instead of an accessor (i.e., if the second argument is a function that takes exactly two arguments), it will be asked to compare two groups a and b and should return a negative value if a should be before b, a positive value if a should be after b, or zero for a partial ordering.