| 675 | threshold = thresholdSturges; |
| 676 | |
| 677 | function histogram(data) { |
| 678 | if (!Array.isArray(data)) data = Array.from(data); |
| 679 | |
| 680 | var i, |
| 681 | n = data.length, |
| 682 | x, |
| 683 | step, |
| 684 | values = new Array(n); |
| 685 | |
| 686 | for (i = 0; i < n; ++i) { |
| 687 | values[i] = value(data[i], i, data); |
| 688 | } |
| 689 | |
| 690 | var xz = domain(values), |
| 691 | x0 = xz[0], |
| 692 | x1 = xz[1], |
| 693 | tz = threshold(values, x0, x1); |
| 694 | |
| 695 | // Convert number of thresholds into uniform thresholds, and nice the |
| 696 | // default domain accordingly. |
| 697 | if (!Array.isArray(tz)) { |
| 698 | const max = x1, tn = +tz; |
| 699 | if (domain === extent$1) [x0, x1] = nice$1(x0, x1, tn); |
| 700 | tz = ticks(x0, x1, tn); |
| 701 | |
| 702 | // If the domain is aligned with the first tick (which it will by |
| 703 | // default), then we can use quantization rather than bisection to bin |
| 704 | // values, which is substantially faster. |
| 705 | if (tz[0] <= x0) step = tickIncrement(x0, x1, tn); |
| 706 | |
| 707 | // If the last threshold is coincident with the domain’s upper bound, the |
| 708 | // last bin will be zero-width. If the default domain is used, and this |
| 709 | // last threshold is coincident with the maximum input value, we can |
| 710 | // extend the niced upper bound by one tick to ensure uniform bin widths; |
| 711 | // otherwise, we simply remove the last threshold. Note that we don’t |
| 712 | // coerce values or the domain to numbers, and thus must be careful to |
| 713 | // compare order (>=) rather than strict equality (===)! |
| 714 | if (tz[tz.length - 1] >= x1) { |
| 715 | if (max >= x1 && domain === extent$1) { |
| 716 | const step = tickIncrement(x0, x1, tn); |
| 717 | if (isFinite(step)) { |
| 718 | if (step > 0) { |
| 719 | x1 = (Math.floor(x1 / step) + 1) * step; |
| 720 | } else if (step < 0) { |
| 721 | x1 = (Math.ceil(x1 * -step) + 1) / -step; |
| 722 | } |
| 723 | } |
| 724 | } else { |
| 725 | tz.pop(); |
| 726 | } |
| 727 | } |
| 728 | } |
| 729 | |
| 730 | // Remove any thresholds outside the domain. |
| 731 | // Be careful not to mutate an array owned by the user! |
| 732 | var m = tz.length, a = 0, b = m; |
| 733 | while (tz[a] <= x0) ++a; |
| 734 | while (tz[b - 1] > x1) --b; |