Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix crash on missing top bin #248

Merged
merged 5 commits into from
Apr 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/bin.js
Original file line number Diff line number Diff line change
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
mbostock marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions test/bin-test.js
Original file line number Diff line number Diff line change
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