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
40 changes: 5 additions & 35 deletions src/linear.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {ticks, tickIncrement} from "d3-array";
import {nice, ticks} from "d3-array";
import continuous, {copy} from "./continuous.js";
import {initRange} from "./init.js";
import tickFormat from "./tickFormat.js";
Expand All @@ -18,40 +18,10 @@ export function linearish(scale) {

scale.nice = function(count) {
if (count == null) count = 10;

var d = domain();
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;
}

while (maxIter-- > 0) {
step = tickIncrement(start, stop, count);
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;
var d = domain(), n = d.length - 1;
if (d[0] <= d[n]) [d[0], d[n]] = nice(d[0], d[n], count);
else [d[n], d[0]] = nice(d[n], d[0], count);
return domain(d);
};

return scale;
Expand Down
9 changes: 9 additions & 0 deletions test/linear-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,15 @@ it("linear.nice(count) nices the domain, extending it to round numbers", () => {
assert.deepStrictEqual(scaleLinear().domain([0, 15]).nice(5).domain(), [0, 20]);
});

it("linear.nice(count) handles small steps precisely", () => {
assert.deepStrictEqual(scaleLinear().domain([1.1, 1.1 + 0.3]).nice().domain(), [1.1, 1.45]);
assert.deepStrictEqual(scaleLinear().domain([-(1.1 + 0.3), -1.1]).nice().domain(), [-1.45, -1.1]);
});

it.skip("linear.nice(count) handles small counts", () => { // See d3-array #299.
assert.deepStrictEqual(scaleLinear().domain([-1, 5]).nice(1).domain(), [-10, 10]);
});

it("linear.nice(count) has no effect on degenerate domains", () => {
assert.deepStrictEqual(scaleLinear().domain([0, 0]).nice(10).domain(), [0, 0]);
assert.deepStrictEqual(scaleLinear().domain([0.5, 0.5]).nice(10).domain(), [0.5, 0.5]);
Expand Down
Loading