Skip to content

fix nice for small counts - #299

Open
Fil wants to merge 2 commits into
mainfrom
fil/fix-nice-small-count
Open

Fil wants to merge 2 commits into
mainfrom
fil/fix-nice-small-count

Conversation

@Fil

@Fil Fil commented Nov 9, 2026

Copy link
Copy Markdown
Member

With a small count, nice never converged: nice(-1, 5, 1) returned [-1e308, 1e308], and bin().thresholds(1)([-1.4, 5.6]) returned bins with NaN bounds.

A niced interval spans at least one step. When it crosses zero, it has at least two, since 0 is a tick for every step.

  • nice(-1, 5, 1) → [-10, 10]
  • nice(0, 1, 0.5) → [0, 1] (was [0, 1e308])
  • bin().thresholds(1)([-1.4, 5.6]) → [-10, 0), [0, 10]

Built on #293, which fixed the case crossing zero, simplifying it to also cover counts less than one.

Fixes #292.
Supersedes #293.

@Fil
Fil requested a review from mbostock November 9, 2026 20:14
@Fil Fil mentioned this pull request Nov 9, 2026
Comment thread src/nice.js
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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

d3.nice(start, stop, 1) returns [-1e308, 1e308] when the interval contains zero, so d3.bin gives NaN bin bounds

3 participants