-
Notifications
You must be signed in to change notification settings - Fork 286
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
Linear nice(n) domain does not always match ticks(n). #9
Comments
Right. The problem is that the tick step is calculated based on the domain, and then the domain is extended, which can change the tick step (for the same number of ticks). An iterative solution is one possibility, but I need to think about it a bit more… |
Any thoughts on this? |
It should be fixed. |
I think computing the tick step twice should be sufficient, so I’ve done that. |
Is this fix going to be back-ported to the old mbostock/d3 version at all? Should I open an issue there? |
I suppose I could, but the more time I spend on back-porting, the longer the release of D3 4.0 is delayed. And there are lots of significant improvements in D3 4.0 that are not possible to back-port (hence the major release). Also here the workaround is trivial, since you can just call nice twice: d3.scale.linear().domain([-0.1, 51.1]).nice(8).nice(8).domain() // [-10, 60] Yet by that argument the fix is also trivial, and I could have fixed it in about the same time I spent writing this response. But I felt obligated to make my point about my larger goal of releasing 4.0. |
It happens to like this in D3 V4.0? I expect:
|
@chenkun24 The behavior looks correct to me: the resulting domain after calling scale.nice(count) is consistent with scale.ticks(count): var x = d3.scaleLinear().domain([0, 5]).nice(6);
x.domain(); // [0, 5]
x.ticks(6); // [0, 1, 2, 3, 4, 5]
var x = d3.scaleLinear().domain([0, 5.1]).nice(6);
x.domain(); // [0, 6]
x.ticks(6); // [0, 1, 2, 3, 4, 5, 6]
var x = d3.scaleLinear().domain([0, 5.1]).nice(7);
x.domain(); // [0, 6]
x.ticks(7); // [0, 1, 2, 3, 4, 5, 6] |
Is there any way to extend the domain and tick exactly as I want?
|
If you mean have scale.ticks(count) return exactly count ticks, no; the count is only a hint. I’m sure it’d be possible for you to implement your own variant of scale.nice that achieved the desired result, but that’s not how d3-scale is designed. |
A comment by @moshevds from d3/d3#2580:
Compare this sentence from https://github.com/mbostock/d3/wiki/Quantitative-Scales#linear_nice:
With this result from the current implementation:
In most cases I do get the result I expected, for example:
I looked at
d3_scale_linearTickRange
. The problem apparently stems from the step-size heuristic and its dependency on the values thatnice
is changing. I don't feel comfortable proposing a completely different heuristic, perhaps you would consider a patch to calculate the heuristic twice?For comparison:
The text was updated successfully, but these errors were encountered: