Skip to content

Commit 9b69423

Browse files
committed
spacing < MIN_SPACING
1 parent ef2ed17 commit 9b69423

File tree

1 file changed

+18
-12
lines changed

1 file changed

+18
-12
lines changed

src/scales/scale.linearbase.js

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ function generateTicks(generationOptions, dataRange) {
1515
// "nice number" algorithm. See https://stackoverflow.com/questions/8506881/nice-label-algorithm-for-charts-with-minimum-ticks
1616
// for details.
1717

18-
var MAX_PRECISION = 1e14;
19-
var MIN_SPACING = 2e-15;
18+
// Minimum spacing between ticks
19+
var MIN_SPACING = 1e-14;
2020
var stepSize = generationOptions.stepSize;
2121
var unit = stepSize || 1;
2222
var maxNumSpaces = generationOptions.maxTicks - 1;
@@ -25,21 +25,26 @@ function generateTicks(generationOptions, dataRange) {
2525
var precision = generationOptions.precision;
2626
var spacing, factor, niceMin, niceMax, numSpaces;
2727

28-
// sanitize dataRangee to MAX_PRECISION
29-
var rmin = Math.floor(dataRange.min * MAX_PRECISION) / MAX_PRECISION;
30-
var rmax = Math.ceil(dataRange.max * MAX_PRECISION) / MAX_PRECISION;
28+
var rmin = dataRange.min;
29+
var rmax = dataRange.max;
30+
var isNullOrUndef = helpers.isNullOrUndef;
3131

3232
// spacing is set to a nice number of the dataRange divided by maxNumSpaces.
3333
// stepSize is used as a minimum unit if it is specified.
34-
// use MIN_SPACING as lower bound to avoid uneven steps with tiny numbers
35-
spacing = Math.max(helpers.niceNum((rmax - rmin) / maxNumSpaces / unit) * unit, MIN_SPACING);
34+
spacing = helpers.niceNum((rmax - rmin) / maxNumSpaces / unit) * unit;
35+
36+
// In case of really small numbers and min / max are undefined, default to rmin / rmax
37+
if (spacing < MIN_SPACING && isNullOrUndef(min) && isNullOrUndef(max)) {
38+
return [rmin, rmax];
39+
}
40+
3641
numSpaces = Math.ceil(rmax / spacing) - Math.floor(rmin / spacing);
3742
if (numSpaces > maxNumSpaces) {
3843
// If the calculated num of spaces exceeds maxNumSpaces, recalculate it
3944
spacing = helpers.niceNum(numSpaces * spacing / maxNumSpaces / unit) * unit;
4045
}
4146

42-
if (stepSize || helpers.isNullOrUndef(precision)) {
47+
if (stepSize || isNullOrUndef(precision)) {
4348
// If a precision is not specified, calculate factor based on spacing
4449
factor = Math.pow(10, helpers.decimalPlaces(spacing));
4550
} else {
@@ -54,10 +59,10 @@ function generateTicks(generationOptions, dataRange) {
5459
// If min, max and stepSize is set and they make an evenly spaced scale use it.
5560
if (stepSize) {
5661
// If very close to our whole number, use it.
57-
if (!helpers.isNullOrUndef(min) && helpers.almostWhole(min / spacing, spacing / 1000)) {
62+
if (!isNullOrUndef(min) && helpers.almostWhole(min / spacing, spacing / 1000)) {
5863
niceMin = min;
5964
}
60-
if (!helpers.isNullOrUndef(max) && helpers.almostWhole(max / spacing, spacing / 1000)) {
65+
if (!isNullOrUndef(max) && helpers.almostWhole(max / spacing, spacing / 1000)) {
6166
niceMax = max;
6267
}
6368
}
@@ -69,13 +74,14 @@ function generateTicks(generationOptions, dataRange) {
6974
} else {
7075
numSpaces = Math.ceil(numSpaces);
7176
}
77+
7278
niceMin = Math.round(niceMin * factor) / factor;
7379
niceMax = Math.round(niceMax * factor) / factor;
74-
ticks.push(helpers.isNullOrUndef(min) ? niceMin : min);
80+
ticks.push(isNullOrUndef(min) ? niceMin : min);
7581
for (var j = 1; j < numSpaces; ++j) {
7682
ticks.push(Math.round((niceMin + j * spacing) * factor) / factor);
7783
}
78-
ticks.push(helpers.isNullOrUndef(max) ? niceMax : max);
84+
ticks.push(isNullOrUndef(max) ? niceMax : max);
7985

8086
return ticks;
8187
}

0 commit comments

Comments
 (0)