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