Skip to content
Open
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
1 change: 1 addition & 0 deletions src/nice.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {tickIncrement} from "./ticks.js";

export default function nice(start, stop, count) {
if (count > 0) count = Math.max(count, start < 0 && stop > 0 ? 2 : 1);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (count > 0) count = Math.max(count, start < 0 && stop > 0 ? 2 : 1);
if (count > 0) count = Math.max(count, start <= 0 || stop >= 0 ? 2 : 1);

Is it not something more lax like this? I guess you don’t need 2 in the [0, 0] case but that doesn’t really matter anyway.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think start <= 0 || stop >= 0 is always true? Did you mean start <= 0 && stop >= 0?

let prestep;
while (true) {
const step = tickIncrement(start, stop, count);
Expand Down
10 changes: 10 additions & 0 deletions test/bin-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,16 @@ it("bin.thresholds(number) sets the approximate number of bin thresholds", () =>
]);
});

it("bin() keeps finite bounds when one threshold spans zero", () => {
const bins = bin().thresholds(1)([-1.4, 5.6]);
assert.strictEqual(bins.flat().length, 2);
for (const {x0, x1} of bins) {
assert(Number.isFinite(x0));
assert(Number.isFinite(x1));
}
assert.deepStrictEqual(bins, [box([-1.4], -10, 0), box([5.6], 0, 10)]);
});

it("bin.thresholds(array) sets the bin thresholds", () => {
const h = bin().thresholds([10, 20]);
assert.deepStrictEqual(h([0, 0, 0, 10, 30, 30]), [
Expand Down
12 changes: 12 additions & 0 deletions test/nice-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,15 @@ it("nice(start, stop, count) handles large steps precisely", () => {
assert.deepStrictEqual(nice(0.929999999999999e10, 1.07e10, 5000), [0.92998e10, 1.07e10]);
assert.deepStrictEqual(nice(-1.07e10, -0.929999999999999e10, 5000), [-1.07e10, -0.92998e10]);
});

it("nice(start, stop, count) keeps intervals finite for small counts", () => {
for (const count of [0.5, 1, 1.5]) {
for (const [start, stop] of [[-1, 5], [-0.1, 0.2], [-5, 1], [-1, 1]]) {
const [a, b] = nice(start, stop, count);
assert(a <= start && b >= stop);
assert(b - a < 100 * (stop - start));
}
}
assert.deepStrictEqual(nice(-1, 5, 1), [-10, 10]);
assert.deepStrictEqual(nice(0, 1, 0.5), [0, 1]);
});
Loading