@@ -15,8 +15,8 @@ function generateTicks(generationOptions, dataRange) {
15
15
// "nice number" algorithm. See https://stackoverflow.com/questions/8506881/nice-label-algorithm-for-charts-with-minimum-ticks
16
16
// for details.
17
17
18
- var MAX_PRECISION = 1e14 ;
19
- var MIN_SPACING = 2e-15 ;
18
+ // Minimum spacing between ticks
19
+ var MIN_SPACING = 1e-14 ;
20
20
var stepSize = generationOptions . stepSize ;
21
21
var unit = stepSize || 1 ;
22
22
var maxNumSpaces = generationOptions . maxTicks - 1 ;
@@ -25,21 +25,26 @@ function generateTicks(generationOptions, dataRange) {
25
25
var precision = generationOptions . precision ;
26
26
var spacing , factor , niceMin , niceMax , numSpaces ;
27
27
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 ;
31
31
32
32
// spacing is set to a nice number of the dataRange divided by maxNumSpaces.
33
33
// 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
+
36
41
numSpaces = Math . ceil ( rmax / spacing ) - Math . floor ( rmin / spacing ) ;
37
42
if ( numSpaces > maxNumSpaces ) {
38
43
// If the calculated num of spaces exceeds maxNumSpaces, recalculate it
39
44
spacing = helpers . niceNum ( numSpaces * spacing / maxNumSpaces / unit ) * unit ;
40
45
}
41
46
42
- if ( stepSize || helpers . isNullOrUndef ( precision ) ) {
47
+ if ( stepSize || isNullOrUndef ( precision ) ) {
43
48
// If a precision is not specified, calculate factor based on spacing
44
49
factor = Math . pow ( 10 , helpers . decimalPlaces ( spacing ) ) ;
45
50
} else {
@@ -54,10 +59,10 @@ function generateTicks(generationOptions, dataRange) {
54
59
// If min, max and stepSize is set and they make an evenly spaced scale use it.
55
60
if ( stepSize ) {
56
61
// 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 ) ) {
58
63
niceMin = min ;
59
64
}
60
- if ( ! helpers . isNullOrUndef ( max ) && helpers . almostWhole ( max / spacing , spacing / 1000 ) ) {
65
+ if ( ! isNullOrUndef ( max ) && helpers . almostWhole ( max / spacing , spacing / 1000 ) ) {
61
66
niceMax = max ;
62
67
}
63
68
}
@@ -69,13 +74,14 @@ function generateTicks(generationOptions, dataRange) {
69
74
} else {
70
75
numSpaces = Math . ceil ( numSpaces ) ;
71
76
}
77
+
72
78
niceMin = Math . round ( niceMin * factor ) / factor ;
73
79
niceMax = Math . round ( niceMax * factor ) / factor ;
74
- ticks . push ( helpers . isNullOrUndef ( min ) ? niceMin : min ) ;
80
+ ticks . push ( isNullOrUndef ( min ) ? niceMin : min ) ;
75
81
for ( var j = 1 ; j < numSpaces ; ++ j ) {
76
82
ticks . push ( Math . round ( ( niceMin + j * spacing ) * factor ) / factor ) ;
77
83
}
78
- ticks . push ( helpers . isNullOrUndef ( max ) ? niceMax : max ) ;
84
+ ticks . push ( isNullOrUndef ( max ) ? niceMax : max ) ;
79
85
80
86
return ticks ;
81
87
}
0 commit comments