| import {slice} from "./array.js"; |
| import bisect from "./bisect.js"; |
| import constant from "./constant.js"; |
| import extent from "./extent.js"; |
| import identity from "./identity.js"; |
| import nice from "./nice.js"; |
| import ticks, {tickIncrement} from "./ticks.js"; |
| import sturges from "./threshold/sturges.js"; |
|
|
| export default function bin() { |
| var value = identity, |
| domain = extent, |
| threshold = sturges; |
|
|
| function histogram(data) { |
| if (!Array.isArray(data)) data = Array.from(data); |
|
|
| var i, |
| n = data.length, |
| x, |
| step, |
| values = new Array(n); |
|
|
| for (i = 0; i < n; ++i) { |
| values[i] = value(data[i], i, data); |
| } |
|
|
| var xz = domain(values), |
| x0 = xz[0], |
| x1 = xz[1], |
| tz = threshold(values, x0, x1); |
|
|
| |
| |
| if (!Array.isArray(tz)) { |
| const max = x1, tn = +tz; |
| if (domain === extent) [x0, x1] = nice(x0, x1, tn); |
| tz = ticks(x0, x1, tn); |
|
|
| |
| |
| |
| if (tz[0] <= x0) step = tickIncrement(x0, x1, tn); |
|
|
| |
| |
| |
| |
| |
| |
| |
| if (tz[tz.length - 1] >= x1) { |
| if (max >= x1 && domain === extent) { |
| const step = tickIncrement(x0, x1, tn); |
| if (isFinite(step)) { |
| if (step > 0) { |
| x1 = (Math.floor(x1 / step) + 1) * step; |
| } else if (step < 0) { |
| x1 = (Math.ceil(x1 * -step) + 1) / -step; |
| } |
| } |
| } else { |
| tz.pop(); |
| } |
| } |
| } |
|
|
| |
| |
| var m = tz.length, a = 0, b = m; |
| while (tz[a] <= x0) ++a; |
| while (tz[b - 1] > x1) --b; |
| if (a || b < m) tz = tz.slice(a, b), m = b - a; |
|
|
| var bins = new Array(m + 1), |
| bin; |
|
|
| |
| for (i = 0; i <= m; ++i) { |
| bin = bins[i] = []; |
| bin.x0 = i > 0 ? tz[i - 1] : x0; |
| bin.x1 = i < m ? tz[i] : x1; |
| } |
|
|
| |
| if (isFinite(step)) { |
| if (step > 0) { |
| for (i = 0; i < n; ++i) { |
| if ((x = values[i]) != null && x0 <= x && x <= x1) { |
| bins[Math.min(m, Math.floor((x - x0) / step))].push(data[i]); |
| } |
| } |
| } else if (step < 0) { |
| for (i = 0; i < n; ++i) { |
| if ((x = values[i]) != null && x0 <= x && x <= x1) { |
| const j = Math.floor((x0 - x) * step); |
| bins[Math.min(m, j + (tz[j] <= x))].push(data[i]); |
| } |
| } |
| } |
| } else { |
| for (i = 0; i < n; ++i) { |
| if ((x = values[i]) != null && x0 <= x && x <= x1) { |
| bins[bisect(tz, x, 0, m)].push(data[i]); |
| } |
| } |
| } |
|
|
| return bins; |
| } |
|
|
| histogram.value = function(_) { |
| return arguments.length ? (value = typeof _ === "function" ? _ : constant(_), histogram) : value; |
| }; |
|
|
| histogram.domain = function(_) { |
| return arguments.length ? (domain = typeof _ === "function" ? _ : constant([_[0], _[1]]), histogram) : domain; |
| }; |
|
|
| histogram.thresholds = function(_) { |
| return arguments.length ? (threshold = typeof _ === "function" ? _ : constant(Array.isArray(_) ? slice.call(_) : _), histogram) : threshold; |
| }; |
|
|
| return histogram; |
| } |
|
|