数据二等分
🌐 Bisecting data
二分法,或称二进制搜索,可以快速在已排序的数组中找到给定的值。它通常用于查找将新值插入数组以保持排序顺序的位置。
🌐 Bisection, or binary search, quickly finds a given value in a sorted array. It is often used to find the position at which to insert a new value into an array while maintaining sorted order.
bisector(访问器)
🌐 bisector(accessor)
示例 · 来源 · 使用指定的 访问器 函数返回一个新的二分器。
const bisector = d3.bisector((d) => d.Date);如果给定的 accessor 接受两个参数,它将被解释为一个比较函数,用于比较数据中的元素 d 与搜索值 x。如果你希望值按不同于自然顺序的顺序排序,例如按降序而不是升序,请使用比较函数而不是访问器。上述内容等同于:
🌐 If the given accessor takes two arguments, it is interpreted as a comparator function for comparing an element d in the data with a search value x. Use a comparator rather than an accessor if you want values to be sorted in an order different than natural order, such as in descending rather than ascending order. The above is equivalent to:
const bisector = d3.bisector((d, x) => d.Date - x);二分器可以用于二分排序的对象数组(与用于二分原始类型的 bisect 相反)。
🌐 The bisector can be used to bisect sorted arrays of objects (in contrast to bisect, which is for bisecting primitives).
bisector.right(array, x, lo, hi)
d3.bisector((d) => d.Date).right(aapl, new Date("2014-01-02")) // 163像 bisectRight,但使用此分割器的访问器。上面的代码找出 aapl 示例数据集 中紧跟 2014 年 1 月 2 日的行的索引。
🌐 Like bisectRight, but using this bisector’s accessor. The code above finds the index of the row immediately following Jan. 2, 2014 in the aapl sample dataset.
bisector.left(array, x, lo, hi)
d3.bisector((d) => d.Date).left(aapl, new Date("2014-01-02")) // 162像 bisectLeft,但是使用这个二分器的访问器。上面的代码在 aapl 示例数据集 中找到 2014 年 1 月 2 日的行索引。
🌐 Like bisectLeft, but using this bisector’s accessor. The code above finds the index of the row for Jan. 2, 2014 in the aapl sample dataset.
bisector.center(array, x, lo, hi)
d3.bisector((d) => d.Date).center(aapl, new Date("2013-12-31")) // 161返回给定已排序 array 中最接近 x 的值的索引。该方法要求分割器的访问器返回一个定量值,或者分割器的比较器返回一个有符号距离;否则,该方法等同于 bisector.left。参数 lo(包含)和 hi(不包含)可用于指定应考虑的数组子集;默认情况下,使用整个数组。
🌐 Returns the index of the closest value to x in the given sorted array. This expects that the bisector’s accessor returns a quantitative value, or that the bisector’s comparator returns a signed distance; otherwise, this method is equivalent to bisector.left. The arguments lo (inclusive) and hi (exclusive) may be used to specify a subset of the array which should be considered; by default the entire array is used.
bisect(array, x, lo, hi)
d3.bisect(aapl.map((d) => d.Date), new Date("2014-01-02")) // 163bisectRight 的别名。
🌐 Alias for bisectRight.
bisectRight(array, x, lo, hi)
d3.bisectRight(aapl.map((d) => d.Date), new Date("2014-01-02")) // 163类似 bisectLeft,但返回的插入点位于数组中任何与 x 相等的现有条目的后面(右侧)。返回的插入点 i 将 array 分为两半,使得对于右侧的所有 v ∈ array.slice(i, hi),都有 v > x。另请参见 bisector.right。
bisectLeft(array, x, lo, hi)
d3.bisectLeft(aapl.map((d) => d.Date), new Date("2014-01-02")) // 162返回 x 在 array 中的插入点,以保持排序顺序。参数 lo 和 hi 可用于指定应考虑的数组子集;默认情况下使用整个数组。如果 x 已经存在于 array 中,插入点将位于任何现有条目的前面(左侧)。返回值适合作为 array.splice 的第一个参数,假设 array 已经排序。返回的插入点 i 将 array 分成两半,使得右侧 array.slice(i, hi) 中的所有 v>= x。另请参见 bisector.left。
bisectCenter(array, x, lo, hi)
d3.bisectCenter(aapl.map((d) => d.Date), new Date("2013-12-31")) // 161返回给定数字 array 中最接近 x 的值的索引。参数 lo(包含)和 hi(不包含)可用于指定应考虑的数组子集;默认情况下使用整个数组。另请参见 bisector.center。
🌐 Returns the index of the value closest to x in the given array of numbers. The arguments lo (inclusive) and hi (exclusive) may be used to specify a subset of the array which should be considered; by default the entire array is used. See also bisector.center.