Skip to content
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
50 changes: 24 additions & 26 deletions src/linear.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,38 +19,36 @@ export function linearish(scale) {
scale.nice = function(count) {
if (count == null) count = 10;

var d = domain(),
i0 = 0,
i1 = d.length - 1,
start = d[i0],
stop = d[i1],
step;
var d = domain();
Comment thread
domoritz marked this conversation as resolved.
var i0 = 0;
var i1 = d.length - 1;
var start = d[i0];
var stop = d[i1];
var prestep;
var step;
var maxIter = 10;

if (stop < start) {
step = start, start = stop, stop = step;
step = i0, i0 = i1, i1 = step;
}

step = tickIncrement(start, stop, count);

if (step > 0) {
start = Math.floor(start / step) * step;
stop = Math.ceil(stop / step) * step;

while (maxIter-- > 0) {
step = tickIncrement(start, stop, count);
} else if (step < 0) {
start = Math.ceil(start * step) / step;
stop = Math.floor(stop * step) / step;
step = tickIncrement(start, stop, count);
}

if (step > 0) {
d[i0] = Math.floor(start / step) * step;
d[i1] = Math.ceil(stop / step) * step;
domain(d);
} else if (step < 0) {
d[i0] = Math.ceil(start * step) / step;
d[i1] = Math.floor(stop * step) / step;
domain(d);
if (step === prestep) {
d[i0] = start
d[i1] = stop
return domain(d);
} else if (step > 0) {
start = Math.floor(start / step) * step;
stop = Math.ceil(stop / step) * step;
} else if (step < 0) {
start = Math.ceil(start * step) / step;
stop = Math.floor(stop * step) / step;
} else {
break;
}
prestep = step;
}

return scale;
Expand Down
30 changes: 30 additions & 0 deletions test/linear-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,36 @@ tape("linear.ticks(count) returns the expected ticks for a polylinear domain", f
test.end();
});

tape("linear.ticks(X) spans linear.nice(X).domain()", function(test) {
function check(domain, count) {
var s = scale.scaleLinear().domain(domain).nice(count);
var ticks = s.ticks(count);
test.deepEqual([ticks[0], ticks[ticks.length - 1]], s.domain());
}

check([1, 9], 2);
check([1, 9], 3);
check([1, 9], 4);

check([8, 9], 2);
check([8, 9], 3);
check([8, 9], 4);

check([1, 21], 2);
check([2, 21], 2);
check([3, 21], 2);
check([4, 21], 2);
check([5, 21], 2);
check([6, 21], 2);
check([7, 21], 2);
check([8, 21], 2);
check([9, 21], 2);
check([10, 21], 2);
check([11, 21], 2);

test.end();
})

tape("linear.ticks(count) returns the empty array if count is not a positive integer", function(test) {
var s = scale.scaleLinear();
test.deepEqual(s.ticks(NaN), []);
Expand Down