Skip to content

数据汇总

¥Summarizing data

计算汇总统计数据。

¥Compute summary statistics.

count(iterable, accessor)

js
d3.count(penguins, (d) => d.body_mass_g) // 342

示例 · 源代码 · 返回指定可迭代对象中有效数值的数量(即非空、非 NaN 或非 undefined);接受访问器。

¥Examples · Source · Returns the number of valid number values (i.e., not null, NaN, or undefined) in the specified iterable; accepts an accessor.

min(iterable, accessor)

示例 · 源代码 · 使用自然顺序返回给定可迭代对象的最小值。

¥Examples · Source · Returns the minimum value in the given iterable using natural order.

js
d3.min([3, 2, 1, 1, 6, 2, 4]) // 1

Unlike Math.min, d3.min does not coerce the inputs to numbers;例如,字符串 ["20", "3"] 的最小值是 "20",而数字 [20, 3] 的最小值是 3

¥Unlike Math.min, d3.min does not coerce the inputs to numbers; for example, the minimum of the strings ["20", "3"] is "20", while the minimum of the numbers [20, 3] is 3.

js
d3.min(["bob", "alice", "carol"]) // "alice"
js
d3.min([new Date("2018-01-01"), new Date("2011-03-09")]) // 2011-03-09

与 Math.min 不同,此方法会忽略未定义、null 和 NaN 值,这对于忽略缺失数据非常有用。

¥Also unlike Math.min, this method ignores undefined, null and NaN values, which is useful for ignoring missing data.

js
d3.min([3, 2, 1, NaN, 4]) // 1

可以指定一个可选的访问器函数,其作用类似于在计算最小值之前调用 Array.from。访问器函数反复传递来自给定可迭代对象(通常为 d)的元素和从零开始的索引 (i)。

¥An optional accessor function may be specified, which is similar to calling Array.from before computing the minimum value. The accessor function is repeatedly passed an element from the given iterable (often d) and the zero-based index (i).

js
d3.min(alphabet, (d) => d.frequency) // 0.00074

由于未定义的值会被忽略,你可以使用访问器函数来忽略这些值。例如,获取非 Z 的最不常见字母的频率:

¥Because undefined values are ignored, you can use the accessor function to ignore values. For example, to get the frequency of the least-common letter than is not Z:

js
d3.min(alphabet, (d) => d.letter === "Z" ? NaN : d.frequency) // 0.00095

如果可迭代对象不包含可比较的值,则返回 undefined。

¥If the iterable contains no comparable values, returns undefined.

js
d3.min([]) // undefined
js
d3.min(alphabet, (d) => d.doesnotexist) // undefined

另请参阅 extentleast

¥See also extent and least.

minIndex(iterable, accessor)

源代码 · 与 min 类似,但返回最小值的索引,而不是值本身。

¥Source · Like min, but returns the index of the minimum value rather than the value itself.

js
d3.minIndex([3, 2, 1, 1, 6, 2, 4]) // 2

此方法可根据给定的访问器查找最小元素,类似于 least

¥This method can find the least element according to the given accessor, similar to least:

js
d3.minIndex(alphabet, (d) => d.frequency) // 25
js
alphabet[d3.minIndex(alphabet, (d) => d.frequency)] // {letter: "Z", frequency: 0.00074}

另请参阅 leastIndex

¥See also leastIndex.

max(iterable, accessor)

示例 · 源代码 · 使用自然顺序返回给定可迭代对象中的最大值。

¥Examples · Source · Returns the maximum value in the given iterable using natural order.

js
d3.max([3, 2, 1, 1, 6, 2, 4]) // 6

Unlike Math.max, d3.max does not coerce the inputs to numbers;例如,字符串 ["20", "3"] 的最大值是 "3",而数字 [20, 3] 的最大值是 20

¥Unlike Math.max, d3.max does not coerce the inputs to numbers; for example, the maximum of the strings ["20", "3"] is "3", while the maximum of the numbers [20, 3] is 20.

js
d3.max(["bob", "alice", "carol"]) // "carol"
js
d3.max([new Date("2018-01-01"), new Date("2011-03-09")]) // 2018-01-01

与 Math.max 不同,此方法会忽略未定义、null 和 NaN 值,这对于忽略缺失数据非常有用。

¥Also unlike Math.max, this method ignores undefined, null and NaN values, which is useful for ignoring missing data.

js
d3.max([3, 2, 1, NaN, 4]) // 4

可以指定一个可选的访问器函数,相当于在计算最大值之前调用 Array.from。访问器函数反复传递来自给定可迭代对象(通常为 d)的元素和从零开始的索引 (i)。

¥An optional accessor function may be specified, which is similar to calling Array.from before computing the maximum value. The accessor function is repeatedly passed an element from the given iterable (often d) and the zero-based index (i).

js
d3.max(alphabet, (d) => d.frequency) // 0.12702

由于未定义的值会被忽略,你可以使用访问器函数来忽略这些值。例如,获取非 E 的最常见字母的频率:

¥Because undefined values are ignored, you can use the accessor function to ignore values. For example, to get the frequency of the most-common letter than is not E:

js
d3.max(alphabet, (d) => d.letter === "E" ? NaN : d.frequency) // 0.09056

如果可迭代对象不包含可比较的值,则返回 undefined。

¥If the iterable contains no comparable values, returns undefined.

js
d3.max([]) // undefined
js
d3.max(alphabet, (d) => d.doesnotexist) // undefined

另请参阅 extentgreatest

¥See also extent and greatest.

maxIndex(iterable, accessor)

源代码 · 与 max 类似,但返回最大值的索引,而不是值本身。

¥Source · Like max, but returns the index of the maximum value rather than the value itself.

js
d3.maxIndex([3, 2, 1, 1, 6, 2, 4]) // 2

此方法可根据给定的访问器查找最大元素,类似于 greatest

¥This method can find the greatest element according to the given accessor, similar to greatest:

js
d3.maxIndex(alphabet, (d) => d.frequency) // 0
js
alphabet[d3.maxIndex(alphabet, (d) => d.frequency)] // {letter: "E", frequency: 0.12702}

另请参阅 greatestIndex

¥See also greatestIndex.

least(iterable, comparator)

示例 · 源代码 · 根据指定的比较器返回指定可迭代对象的最小元素。

¥Examples · Source · Returns the least element of the specified iterable according to the specified comparator.

js
d3.least(alphabet, (a, b) => a.frequency - b.frequency) // {letter: "Z", frequency: 0.00074}
js
d3.least(alphabet, (a, b) => b.frequency - a.frequency) // {letter: "E", frequency: 0.12702}

如果比较器只接受一个参数,则将其解释为访问器,并按自然顺序比较返回的元素。

¥If the comparator takes a single argument, is interpreted as an accessor and the returned elements are compared using natural order.

js
d3.least(alphabet, (d) => d.frequency) // {letter: "Z", frequency: 0.00074}
js
d3.least(alphabet, (d) => -d.frequency) // {letter: "E", frequency: 0.12702}

如果未指定 comparator,则默认为 ascending

¥If comparator is not specified, it defaults to ascending.

js
d3.least(alphabet.map((d) => d.frequency)) // 0.00074

如果给定的可迭代对象不包含可比较元素(即,比较器在将每个元素与自身进行比较时返回 NaN),则返回 undefined。

¥If the given iterable contains no comparable elements (i.e., the comparator returns NaN when comparing each element to itself), returns undefined.

js
d3.least([]) // undefined

此函数与 min 类似,但它允许使用比较器而不是访问器。

¥This function is similar to min, except it allows the use of a comparator rather than an accessor.

leastIndex(iterable, comparator)

源代码 · 根据指定的比较器或访问器,返回指定可迭代对象中最小元素的索引。如果给定的可迭代对象不包含可比较元素(即,比较器在将每个元素与自身进行比较时返回 NaN),则返回 -1。如果未指定 comparator,则默认为 ascending。例如:

¥Source · Returns the index of the least element of the specified iterable according to the specified comparator or accessor. If the given iterable contains no comparable elements (i.e., the comparator returns NaN when comparing each element to itself), returns -1. If comparator is not specified, it defaults to ascending. For example:

js
const array = [{foo: 42}, {foo: 91}];
d3.leastIndex(array, (a, b) => a.foo - b.foo); // 0
d3.leastIndex(array, (a, b) => b.foo - a.foo); // 1
d3.leastIndex(array, (d) => d.foo); // 0

此函数与 minIndex 类似,但它允许使用比较器而不是访问器。

¥This function is similar to minIndex, except it allows the use of a comparator rather than an accessor.

greatest(iterable, comparator)

示例 · 源代码 · 根据指定的比较器或访问器返回指定可迭代对象中的最大元素。如果给定的可迭代对象不包含可比较元素(即,比较器在将每个元素与自身进行比较时返回 NaN),则返回 undefined。如果未指定 comparator,则默认为 ascending。例如:

¥Examples · Source · Returns the greatest element of the specified iterable according to the specified comparator or accessor. If the given iterable contains no comparable elements (i.e., the comparator returns NaN when comparing each element to itself), returns undefined. If comparator is not specified, it defaults to ascending. For example:

js
const array = [{foo: 42}, {foo: 91}];
d3.greatest(array, (a, b) => a.foo - b.foo); // {foo: 91}
d3.greatest(array, (a, b) => b.foo - a.foo); // {foo: 42}
d3.greatest(array, (d) => d.foo); // {foo: 91}

此函数与 max 类似,但它允许使用比较器而不是访问器。

¥This function is similar to max, except it allows the use of a comparator rather than an accessor.

greatestIndex(iterable, comparator)

源代码 · 根据指定的比较器或访问器,返回指定可迭代对象中最大元素的索引。如果给定的可迭代对象不包含可比较元素(即,比较器在将每个元素与自身进行比较时返回 NaN),则返回 -1。如果未指定 comparator,则默认为 ascending。例如:

¥Source · Returns the index of the greatest element of the specified iterable according to the specified comparator or accessor. If the given iterable contains no comparable elements (i.e., the comparator returns NaN when comparing each element to itself), returns -1. If comparator is not specified, it defaults to ascending. For example:

js
const array = [{foo: 42}, {foo: 91}];
d3.greatestIndex(array, (a, b) => a.foo - b.foo); // 1
d3.greatestIndex(array, (a, b) => b.foo - a.foo); // 0
d3.greatestIndex(array, (d) => d.foo); // 1

此函数与 maxIndex 类似,但它允许使用比较器而不是访问器。

¥This function is similar to maxIndex, except it allows the use of a comparator rather than an accessor.

extent(iterable, accessor)

示例 · 源代码 · 使用自然顺序返回给定可迭代对象中的 minimummaximum 值。

¥Examples · Source · Returns the minimum and maximum value in the given iterable using natural order.

js
d3.extent([3, 2, 1, 1, 6, 2, 4]) // [1, 6]

可以指定一个可选的访问器函数,这相当于在计算范围之前调用 Array.from。

¥An optional accessor function may be specified, which is equivalent to calling Array.from before computing the extent.

js
d3.extent(alphabet, (d) => d.frequency) // [0.00074, 0.12702]

如果可迭代对象不包含可比较的值,则返回 [undefined, undefined]。

¥If the iterable contains no comparable values, returns [undefined, undefined].

js
d3.extent(alphabet, (d) => d.doesnotexist) // [undefined, undefined]

mode(iterable, accessor)

示例 · 源代码 · 返回给定可迭代对象的众数,即出现次数最多的值。忽略未定义、null 和 NaN 值。

¥Examples · Source · Returns the mode of the given iterable, i.e. the value which appears the most often. Ignores undefined, null and NaN values.

js
d3.mode([1, 2, 2, 2, 3, 3]) // 2

可以指定一个可选的访问器函数,相当于在计算众数之前调用 Array.from。

¥An optional accessor function may be specified, which is equivalent to calling Array.from before computing the mode.

js
d3.mode(penguins, (d) => d.island) // "Biscoe"

如果相等,则返回第一个相关值。如果可迭代对象不包含可比较的值,则返回 undefined。

¥In case of equality, returns the first of the relevant values. If the iterable contains no comparable values, returns undefined.

sum(iterable, accessor)

示例 · 源代码 · 返回给定可迭代对象的和。忽略未定义、null 和 NaN 值。

¥Examples · Source · Returns the sum of the given iterable of numbers. Ignores undefined, null and NaN values.

js
d3.sum([1, 2, 2, 2, NaN, 3, null]) // 10

可以指定一个可选的访问器函数,相当于在计算总和之前调用 Array.from。

¥An optional accessor function may be specified, which is equivalent to calling Array.from before computing the sum.

js
d3.sum(penguins, (d) => d.body_mass_g) // 1437000

如果可迭代对象不包含数字,则返回 0。另请参阅 fsum

¥If the iterable contains no numbers, returns 0. See also fsum.

mean(iterable, accessor)

示例 · 源代码 · 返回给定可迭代对象的平均值。忽略未定义、null 和 NaN 值。

¥Examples · Source · Returns the mean of the given iterable of numbers. Ignores undefined, null and NaN values.

js
d3.mean([1, 2, 2, 2, NaN, 3, null]) // 2

可以指定一个可选的访问器函数,相当于在计算平均值之前调用 Array.from。

¥An optional accessor function may be specified, which is equivalent to calling Array.from before computing the mean.

js
d3.mean(penguins, (d) => d.body_mass_g) // 4201.754385964912

如果可迭代对象不包含数字,则返回 undefined。

¥If the iterable contains no numbers, returns undefined.

median(iterable, accessor)

示例 · 源代码 · 使用 R-7 方法 返回给定可迭代对象的中位数。忽略未定义、null 和 NaN 值。

¥Examples · Source · Returns the median of the given iterable of numbers using the R-7 method. Ignores undefined, null and NaN values.

js
d3.median([1, 2, 2, 2, NaN, 3, null]) // 2

可以指定一个可选的访问器函数,相当于在计算中位数之前调用 Array.from。

¥An optional accessor function may be specified, which is equivalent to calling Array.from before computing the median.

js
d3.median(penguins, (d) => d.body_mass_g) // 4050

如果可迭代对象不包含数字,则返回 undefined。

¥If the iterable contains no numbers, returns undefined.

medianIndex(array, accessor)

源代码 · 与 median 类似,但返回中位数左侧元素的索引。

¥Source · Like median, but returns the index of the element to the left of the median.

js
d3.medianIndex([1, 2, 2, 2, NaN, 3, null]) // 2

cumsum(iterable, accessor)

示例 · 源代码 · 返回给定可迭代对象数字的累计和,以相同长度的 Float64Array 形式返回。

¥Examples · Source · Returns the cumulative sum of the given iterable of numbers, as a Float64Array of the same length.

js
d3.cumsum([1, 1, 2, 3, 5]) // [1, 2, 4, 7, 12]

可以指定一个可选的访问器函数,这相当于在计算累积和之前调用 Array.from。

¥An optional accessor function may be specified, which is equivalent to calling Array.from before computing the cumulative sum.

js
d3.cumsum(penguins, (d) => d.body_mass_g) // [3750, 7550, 10800, 10800, …]

此方法忽略未定义值和 NaN 值;这对于忽略缺失数据很有用。如果可迭代对象不包含数字,则返回零。另请参阅 fcumsum

¥This method ignores undefined and NaN values; this is useful for ignoring missing data. If the iterable contains no numbers, returns zeros. See also fcumsum.

quantile(iterable, p, accessor)

示例 · 源代码 · 返回给定可迭代对象的 p 分位数,其中 p 为 [0, 1] 范围内的数值。例如,可以使用 p = 0.5、p = 0.25 处的第一个四分位数和 p = 0.75 处的第三个四分位数来计算中位数。此特定实现使用 R-7 方法,它是 R 编程语言和 Excel 的默认设置。

¥Examples · Source · Returns the p-quantile of the given iterable of numbers, where p is a number in the range [0, 1]. For example, the median can be computed using p = 0.5, the first quartile at p = 0.25, and the third quartile at p = 0.75. This particular implementation uses the R-7 method, which is the default for the R programming language and Excel.

js
const numbers = [0, 10, 30];
d3.quantile(numbers, 0); // 0
d3.quantile(numbers, 0.5); // 10
d3.quantile(numbers, 1); // 30
d3.quantile(numbers, 0.25); // 5
d3.quantile(numbers, 0.75); // 20
d3.quantile(numbers, 0.1); // 2

可以指定一个可选的访问器函数,相当于在计算分位数之前调用 array.map

¥An optional accessor function may be specified, which is equivalent to calling array.map before computing the quantile.

quantileIndex(array, p, accessor)

源代码 · 与分位数类似,但返回 p 左侧的索引。

¥Source · Similar to quantile, but returns the index to the left of p.

quantileSorted(array, p, accessor)

示例 · 源代码 · 与分位数类似,但要求输入为已排序的值数组。与分位数相比,访问器仅在计算分位数所需的元素上调用。

¥Examples · Source · Similar to quantile, but expects the input to be a sorted array of values. In contrast with quantile, the accessor is only called on the elements needed to compute the quantile.

rank(iterable, comparator)

示例 · 源代码 · 返回一个数组,其中包含可迭代对象中每个值的秩,即可迭代对象排序后,该值的从零开始的索引。空值排在末尾,并排名为 NaN。可以指定一个可选的比较器或访问器函数;后者相当于在计算等级之前调用 array.map。如果未指定 comparator,则默认为 ascending。所有等值线(等价值)都具有相同的等级,该等级定义为第一次找到该值的时间。

¥Examples · Source · Returns an array with the rank of each value in the iterable, i.e. the zero-based index of the value when the iterable is sorted. Nullish values are sorted to the end and ranked NaN. An optional comparator or accessor function may be specified; the latter is equivalent to calling array.map before computing the ranks. If comparator is not specified, it defaults to ascending. Ties (equivalent values) all get the same rank, defined as the first time the value is found.

js
d3.rank([{x: 1}, {}, {x: 2}, {x: 0}], d => d.x); // [1, NaN, 2, 0]
d3.rank(["b", "c", "b", "a"]); // [1, 3, 1, 0]
d3.rank([1, 2, 3], d3.descending); // [2, 1, 0]

variance(iterable, accessor)

示例 · 源代码 · 使用 韦尔福德算法 返回给定数字可迭代对象的 总体方差的无偏估计器。如果可迭代对象包含的数字少于两个,则返回 undefined。可以指定一个可选的访问器函数,相当于在计算方差之前调用 Array.from。此方法忽略未定义值和 NaN 值;这对于忽略缺失数据很有用。

¥Examples · Source · Returns an unbiased estimator of the population variance of the given iterable of numbers using Welford’s algorithm. If the iterable has fewer than two numbers, returns undefined. An optional accessor function may be specified, which is equivalent to calling Array.from before computing the variance. This method ignores undefined and NaN values; this is useful for ignoring missing data.

deviation(iterable, accessor)

示例 · 源代码 · 返回给定可迭代对象的标准差,定义为 偏差校正方差 的平方根。如果可迭代对象包含的数字少于两个,则返回 undefined。可以指定一个可选的访问器函数,相当于在计算标准差之前调用 Array.from。此方法忽略未定义值和 NaN 值;这对于忽略缺失数据很有用。

¥Examples · Source · Returns the standard deviation, defined as the square root of the bias-corrected variance, of the given iterable of numbers. If the iterable has fewer than two numbers, returns undefined. An optional accessor function may be specified, which is equivalent to calling Array.from before computing the standard deviation. This method ignores undefined and NaN values; this is useful for ignoring missing data.

every(iterable, test)

源代码 · 如果给定的测试函数对给定可迭代对象中的每个值都返回 true,则返回 true。此方法在 test 返回非真值或所有值迭代完毕后立即返回。等同于 array.every

¥Source · Returns true if the given test function returns true for every value in the given iterable. This method returns as soon as test returns a non-truthy value or all values are iterated over. Equivalent to array.every:

js
d3.every(new Set([1, 3, 5, 7]), x => x & 1) // true

some(iterable, test)

源代码 · 如果给定的测试函数对给定可迭代对象中的任何值都返回 true,则返回 true。此方法在 test 返回真值或所有值迭代完毕后立即返回。等同于 array.some

¥Source · Returns true if the given test function returns true for any value in the given iterable. This method returns as soon as test returns a truthy value or all values are iterated over. Equivalent to array.some:

js
d3.some(new Set([0, 2, 3, 4]), x => x & 1) // true