| 1 | import {slice} from "./array.js";
|
|---|
| 2 | import bisect from "./bisect.js";
|
|---|
| 3 | import constant from "./constant.js";
|
|---|
| 4 | import extent from "./extent.js";
|
|---|
| 5 | import identity from "./identity.js";
|
|---|
| 6 | import nice from "./nice.js";
|
|---|
| 7 | import ticks, {tickIncrement} from "./ticks.js";
|
|---|
| 8 | import sturges from "./threshold/sturges.js";
|
|---|
| 9 |
|
|---|
| 10 | export default function bin() {
|
|---|
| 11 | var value = identity,
|
|---|
| 12 | domain = extent,
|
|---|
| 13 | threshold = sturges;
|
|---|
| 14 |
|
|---|
| 15 | function histogram(data) {
|
|---|
| 16 | if (!Array.isArray(data)) data = Array.from(data);
|
|---|
| 17 |
|
|---|
| 18 | var i,
|
|---|
| 19 | n = data.length,
|
|---|
| 20 | x,
|
|---|
| 21 | step,
|
|---|
| 22 | values = new Array(n);
|
|---|
| 23 |
|
|---|
| 24 | for (i = 0; i < n; ++i) {
|
|---|
| 25 | values[i] = value(data[i], i, data);
|
|---|
| 26 | }
|
|---|
| 27 |
|
|---|
| 28 | var xz = domain(values),
|
|---|
| 29 | x0 = xz[0],
|
|---|
| 30 | x1 = xz[1],
|
|---|
| 31 | tz = threshold(values, x0, x1);
|
|---|
| 32 |
|
|---|
| 33 | // Convert number of thresholds into uniform thresholds, and nice the
|
|---|
| 34 | // default domain accordingly.
|
|---|
| 35 | if (!Array.isArray(tz)) {
|
|---|
| 36 | const max = x1, tn = +tz;
|
|---|
| 37 | if (domain === extent) [x0, x1] = nice(x0, x1, tn);
|
|---|
| 38 | tz = ticks(x0, x1, tn);
|
|---|
| 39 |
|
|---|
| 40 | // If the domain is aligned with the first tick (which it will by
|
|---|
| 41 | // default), then we can use quantization rather than bisection to bin
|
|---|
| 42 | // values, which is substantially faster.
|
|---|
| 43 | if (tz[0] <= x0) step = tickIncrement(x0, x1, tn);
|
|---|
| 44 |
|
|---|
| 45 | // If the last threshold is coincident with the domain’s upper bound, the
|
|---|
| 46 | // last bin will be zero-width. If the default domain is used, and this
|
|---|
| 47 | // last threshold is coincident with the maximum input value, we can
|
|---|
| 48 | // extend the niced upper bound by one tick to ensure uniform bin widths;
|
|---|
| 49 | // otherwise, we simply remove the last threshold. Note that we don’t
|
|---|
| 50 | // coerce values or the domain to numbers, and thus must be careful to
|
|---|
| 51 | // compare order (>=) rather than strict equality (===)!
|
|---|
| 52 | if (tz[tz.length - 1] >= x1) {
|
|---|
| 53 | if (max >= x1 && domain === extent) {
|
|---|
| 54 | const step = tickIncrement(x0, x1, tn);
|
|---|
| 55 | if (isFinite(step)) {
|
|---|
| 56 | if (step > 0) {
|
|---|
| 57 | x1 = (Math.floor(x1 / step) + 1) * step;
|
|---|
| 58 | } else if (step < 0) {
|
|---|
| 59 | x1 = (Math.ceil(x1 * -step) + 1) / -step;
|
|---|
| 60 | }
|
|---|
| 61 | }
|
|---|
| 62 | } else {
|
|---|
| 63 | tz.pop();
|
|---|
| 64 | }
|
|---|
| 65 | }
|
|---|
| 66 | }
|
|---|
| 67 |
|
|---|
| 68 | // Remove any thresholds outside the domain.
|
|---|
| 69 | // Be careful not to mutate an array owned by the user!
|
|---|
| 70 | var m = tz.length, a = 0, b = m;
|
|---|
| 71 | while (tz[a] <= x0) ++a;
|
|---|
| 72 | while (tz[b - 1] > x1) --b;
|
|---|
| 73 | if (a || b < m) tz = tz.slice(a, b), m = b - a;
|
|---|
| 74 |
|
|---|
| 75 | var bins = new Array(m + 1),
|
|---|
| 76 | bin;
|
|---|
| 77 |
|
|---|
| 78 | // Initialize bins.
|
|---|
| 79 | for (i = 0; i <= m; ++i) {
|
|---|
| 80 | bin = bins[i] = [];
|
|---|
| 81 | bin.x0 = i > 0 ? tz[i - 1] : x0;
|
|---|
| 82 | bin.x1 = i < m ? tz[i] : x1;
|
|---|
| 83 | }
|
|---|
| 84 |
|
|---|
| 85 | // Assign data to bins by value, ignoring any outside the domain.
|
|---|
| 86 | if (isFinite(step)) {
|
|---|
| 87 | if (step > 0) {
|
|---|
| 88 | for (i = 0; i < n; ++i) {
|
|---|
| 89 | if ((x = values[i]) != null && x0 <= x && x <= x1) {
|
|---|
| 90 | bins[Math.min(m, Math.floor((x - x0) / step))].push(data[i]);
|
|---|
| 91 | }
|
|---|
| 92 | }
|
|---|
| 93 | } else if (step < 0) {
|
|---|
| 94 | for (i = 0; i < n; ++i) {
|
|---|
| 95 | if ((x = values[i]) != null && x0 <= x && x <= x1) {
|
|---|
| 96 | const j = Math.floor((x0 - x) * step);
|
|---|
| 97 | bins[Math.min(m, j + (tz[j] <= x))].push(data[i]); // handle off-by-one due to rounding
|
|---|
| 98 | }
|
|---|
| 99 | }
|
|---|
| 100 | }
|
|---|
| 101 | } else {
|
|---|
| 102 | for (i = 0; i < n; ++i) {
|
|---|
| 103 | if ((x = values[i]) != null && x0 <= x && x <= x1) {
|
|---|
| 104 | bins[bisect(tz, x, 0, m)].push(data[i]);
|
|---|
| 105 | }
|
|---|
| 106 | }
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 | return bins;
|
|---|
| 110 | }
|
|---|
| 111 |
|
|---|
| 112 | histogram.value = function(_) {
|
|---|
| 113 | return arguments.length ? (value = typeof _ === "function" ? _ : constant(_), histogram) : value;
|
|---|
| 114 | };
|
|---|
| 115 |
|
|---|
| 116 | histogram.domain = function(_) {
|
|---|
| 117 | return arguments.length ? (domain = typeof _ === "function" ? _ : constant([_[0], _[1]]), histogram) : domain;
|
|---|
| 118 | };
|
|---|
| 119 |
|
|---|
| 120 | histogram.thresholds = function(_) {
|
|---|
| 121 | return arguments.length ? (threshold = typeof _ === "function" ? _ : constant(Array.isArray(_) ? slice.call(_) : _), histogram) : threshold;
|
|---|
| 122 | };
|
|---|
| 123 |
|
|---|
| 124 | return histogram;
|
|---|
| 125 | }
|
|---|