Skip to content

Commit

Permalink
fix crash on missing top bin (#248)
Browse files Browse the repository at this point in the history
* fix https://observablehq.com/@weiglemc/demonstrating-issue-with-d3-bins-in-d3v7-4-1-update

the issue is that we have removed the last bin, but then want to assign the largest value to it because floor(x1) === x1

Co-authored-by: Mike Bostock <mbostock@gmail.com>
  • Loading branch information
Fil and mbostock committed Apr 7, 2022
1 parent 364b3f3 commit 68fb588
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/bin.js
Expand Up @@ -85,14 +85,14 @@ export default function bin() {
if (step > 0) {
for (i = 0; i < n; ++i) {
if ((x = values[i]) != null && x0 <= x && x <= x1) {
bins[Math.floor((x - x0) / step)].push(data[i]);
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[j + (tz[j] <= x)].push(data[i]); // handle off-by-one due to rounding
bins[Math.min(m, j + (tz[j] <= x))].push(data[i]); // handle off-by-one due to rounding
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions test/bin-test.js
Expand Up @@ -235,6 +235,12 @@ it("bin(data) assigns floating point values to the correct bins", () => {
}
});

it("bin(data) assigns integer values to the correct bins", () => {
assert.deepStrictEqual(bin().domain([4, 5])([5]), [box([5], 4, 5)]);
const eights = [8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8];
assert.deepStrictEqual(bin().domain([3, 8])(eights), [box([], 3, 4), box([], 4, 5), box([], 5, 6), box([], 6, 7), box(eights, 7, 8)]);
});

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

0 comments on commit 68fb588

Please sign in to comment.