Skip to content

Commit

Permalink
Fix last bin.
Browse files Browse the repository at this point in the history
  • Loading branch information
mbostock committed Dec 8, 2020
1 parent 53e1ad0 commit d081c6f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,17 @@ export default function() {
// last bin will be zero-width. If the default domain is used, and this
// last threshold is coincident with the maximum input value, we can
// extend the niced upper bound by one tick to ensure uniform bin widths;
// otherwise, we simply remove the last threshold.
if (tz[tz.length - 1] === x1) {
if (x1 === max && domain === extent) {
// otherwise, we simply remove the last threshold. Note that we don’t
// coerce values or the domain to numbers, and thus must be careful to
// compare order (>=) rather than strict equality (===)!
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;
x1 = (Math.ceil(x1 * -step) + 1) / -step;
}
}
} else {
Expand Down
15 changes: 15 additions & 0 deletions test/bin-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,21 @@ tape("bin()() returns bins whose rightmost bin is not too wide", (test) => {
]);
});

tape("bin(data) coerces values to numbers as expected", (test) => {
const h = d3.bin().thresholds(10);
test.deepEqual(h(["1", "2", "3", "4", "5"]), [
bin(["1"], 1, 1.5),
bin([], 1.5, 2),
bin(["2"], 2, 2.5),
bin([], 2.5, 3),
bin(["3"], 3, 3.5),
bin([], 3.5, 4),
bin(["4"], 4, 4.5),
bin([], 4.5, 5),
bin(["5"], 5, 5.5)
]);
});

function bin(bin, x0, x1) {
bin.x0 = x0;
bin.x1 = x1;
Expand Down

0 comments on commit d081c6f

Please sign in to comment.