数据汇总
🌐 Summarizing data
计算汇总统计数据。
🌐 Compute summary statistics.
count(iterable, accessor)
d3.count(penguins, (d) => d.body_mass_g) // 342示例 · 来源 · 返回指定可迭代对象中有效数字值(即,非 null、NaN 或 undefined)的数量;接受一个访问器。
min(可迭代对象, 访问器)
🌐 min(iterable, accessor)
示例 · 来源 · 使用自然顺序返回给定可迭代对象中的最小值。
d3.min([3, 2, 1, 1, 6, 2, 4]) // 1与 Math.min 不同,d3.min 不会将输入强制转换为数字;例如,字符串 ["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.
d3.min(["bob", "alice", "carol"]) // "alice"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.
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).
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:
d3.min(alphabet, (d) => d.letter === "Z" ? NaN : d.frequency) // 0.00095如果可迭代对象不包含可比较的值,则返回 undefined。
🌐 If the iterable contains no comparable values, returns undefined.
d3.min([]) // undefinedd3.min(alphabet, (d) => d.doesnotexist) // undefinedminIndex(可迭代对象, 访问器)
🌐 minIndex(iterable, accessor)
来源 · 类似于 min,但返回的是最小值的索引,而不是值本身。
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:
d3.minIndex(alphabet, (d) => d.frequency) // 25alphabet[d3.minIndex(alphabet, (d) => d.frequency)] // {letter: "Z", frequency: 0.00074}另请参见 leastIndex。
🌐 See also leastIndex.
max(可迭代对象, 访问器)
🌐 max(iterable, accessor)
示例 · 来源 · 使用自然顺序返回给定可迭代对象中的最大值。
d3.max([3, 2, 1, 1, 6, 2, 4]) // 6与 Math.max 不同,d3.max 不会将输入强制转换为数字;例如,字符串 ["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.
d3.max(["bob", "alice", "carol"]) // "carol"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.
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).
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:
d3.max(alphabet, (d) => d.letter === "E" ? NaN : d.frequency) // 0.09056如果可迭代对象不包含可比较的值,则返回 undefined。
🌐 If the iterable contains no comparable values, returns undefined.
d3.max([]) // undefinedd3.max(alphabet, (d) => d.doesnotexist) // undefined🌐 See also extent and greatest.
maxIndex(可迭代对象, 访问器)
🌐 maxIndex(iterable, accessor)
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:
d3.maxIndex(alphabet, (d) => d.frequency) // 0alphabet[d3.maxIndex(alphabet, (d) => d.frequency)] // {letter: "E", frequency: 0.12702}另请参见 greatestIndex。
🌐 See also greatestIndex.
least(可迭代对象, 比较器)
🌐 least(iterable, comparator)
示例 · 来源 · 返回根据指定比较器指定的可迭代对象中的最小元素。
d3.least(alphabet, (a, b) => a.frequency - b.frequency) // {letter: "Z", frequency: 0.00074}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.
d3.least(alphabet, (d) => d.frequency) // {letter: "Z", frequency: 0.00074}d3.least(alphabet, (d) => -d.frequency) // {letter: "E", frequency: 0.12702}如果未指定 comparator,则默认为 升序。
🌐 If comparator is not specified, it defaults to ascending.
d3.least(alphabet.map((d) => d.frequency)) // 0.00074如果给定的可迭代对象不包含可比较的元素(即比较器在将每个元素与其自身比较时返回 NaN),则返回未定义。
🌐 If the given iterable contains no comparable elements (i.e., the comparator returns NaN when comparing each element to itself), returns undefined.
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。如果未指定比较器,默认为升序。例如:
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(可迭代对象, 比较器)
🌐 greatest(iterable, comparator)
示例 · 来源 · 根据指定的 比较器 或 访问器 返回指定 可迭代对象 的最大元素。如果给定的 可迭代对象 不包含可比较的元素(即,比较器在将每个元素与自身比较时返回 NaN),则返回 undefined。如果未指定 比较器,则默认使用 升序。例如:
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。如果未指定比较器,则默认使用升序。例如:
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(可迭代对象, 访问器)
🌐 extent(iterable, accessor)
示例 · 来源 · 使用自然顺序返回给定可迭代对象中的最小和最大值。
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.
d3.extent(alphabet, (d) => d.frequency) // [0.00074, 0.12702]如果可迭代对象不包含可比较的值,则返回 [undefined, undefined]。
🌐 If the iterable contains no comparable values, returns [undefined, undefined].
d3.extent(alphabet, (d) => d.doesnotexist) // [undefined, undefined]mode(iterable, accessor)
示例 · 来源 · 返回给定可迭代对象的众数,即出现次数最多的值。忽略未定义、null和NaN值。
d3.mode([1, 2, 2, 2, 3, 3]) // 2可以指定一个可选的 accessor 函数,这相当于在计算众数之前调用 Array.from。
🌐 An optional accessor function may be specified, which is equivalent to calling Array.from before computing the mode.
d3.mode(penguins, (d) => d.island) // "Biscoe"在相等的情况下,返回相关值中的第一个。如果可迭代对象不包含可比较的值,则返回未定义。
🌐 In case of equality, returns the first of the relevant values. If the iterable contains no comparable values, returns undefined.
sum(可迭代对象, 访问器)
🌐 sum(iterable, accessor)
示例 · 来源 · 返回给定可迭代数字的和。忽略未定义、null和NaN值。
d3.sum([1, 2, 2, 2, NaN, 3, null]) // 10可以指定一个可选的 accessor 函数,这相当于在计算总和之前调用 Array.from。
🌐 An optional accessor function may be specified, which is equivalent to calling Array.from before computing the sum.
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 值。
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.
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 值。
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.
d3.median(penguins, (d) => d.body_mass_g) // 4050如果可迭代对象不包含数字,则返回 undefined。
🌐 If the iterable contains no numbers, returns undefined.
medianIndex(数组, 访问器)
🌐 medianIndex(array, accessor)
d3.medianIndex([1, 2, 2, 2, NaN, 3, null]) // 2cumsum(可迭代对象, 访问器)
🌐 cumsum(iterable, accessor)
示例 · 来源 · 返回给定数字可迭代对象的累积和,作为长度相同的 Float64Array。
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.
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)
示例 · 来源 · 返回给定数字 iterable 的 p 分位数,其中 p 是范围在 [0, 1] 之间的数字。例如,中位数可以使用 p = 0.5 计算,第一四分位数为 p = 0.25,第三四分位数为 p = 0.75。此特定实现使用 R-7 方法,这是 R 编程语言和 Excel 的默认方法。
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(数组, p, 访问器)
🌐 quantileIndex(array, p, accessor)
来源 · 类似于quantile,但返回p左侧的索引。
quantileSorted(数组, p, 访问器)
🌐 quantileSorted(array, p, accessor)
示例 · 来源 · 类似于 quantile,但期望输入是一个已排序的数组值。与 quantile 不同,访问器仅在计算分位数所需的元素上调用。
rank(iterable, comparator)
示例 · 来源 · 返回一个数组,其中包含 可迭代对象 中每个值的排名,即在对可迭代对象排序后该值的零基索引。空值会被排到末尾并被排名为 NaN。可以指定可选的 比较函数 或 访问器 函数;后者相当于在计算排名前调用 array.map。如果未指定 比较函数,默认为 升序。平局(相等的值)都得到相同的排名,定义为该值第一次出现时的排名。
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(可迭代对象, 访问器)
🌐 variance(iterable, accessor)
示例 · 来源 · 使用 Welford 算法 返回给定数字 可迭代对象 的 总体方差无偏估计。如果可迭代对象中的数字少于两个,则返回未定义值。可以指定一个可选的 访问器 函数,这相当于在计算方差之前调用 Array.from。此方法会忽略 undefined 和 NaN 值;这对于忽略缺失数据非常有用。
deviation(可迭代对象, 访问器)
🌐 deviation(iterable, accessor)
示例 · 来源 · 返回给定数字 可迭代对象 的标准差,定义为 偏差修复方差 的平方根。如果可迭代对象中的数字少于两个,则返回未定义。可以指定可选的 访问函数,这相当于在计算标准差之前调用 Array.from。此方法会忽略 undefined 和 NaN 值;这对于忽略缺失数据非常有用。
every(可迭代对象, 测试)
🌐 every(iterable, test)
来源 · 如果给定的 test 函数对给定 iterable 中的每个值都返回 true,则返回 true。该方法会在 test 返回非真值或所有值被迭代完时立即返回。等同于 array.every:
d3.every(new Set([1, 3, 5, 7]), x => x & 1) // truesome(可迭代对象, 测试)
🌐 some(iterable, test)
来源 · 如果给定的 test 函数对给定 iterable 中的任意值返回 true,则返回 true。此方法会在 test 返回真值或所有值被迭代完毕时立即返回。等同于 array.some:
d3.some(new Set([0, 2, 3, 4]), x => x & 1) // true