@@ -15,54 +15,55 @@ function generateTicks(generationOptions, dataRange) {
15
15
// "nice number" algorithm. See http://stackoverflow.com/questions/8506881/nice-label-algorithm-for-charts-with-minimum-ticks
16
16
// for details.
17
17
18
- var factor ;
19
- var precision ;
20
- var spacing ;
18
+ var stepSize = generationOptions . stepSize ;
19
+ var min = generationOptions . min ;
20
+ var max = generationOptions . max ;
21
+ var spacing , precision , factor , niceRange , niceMin , niceMax , numSpaces ;
21
22
22
- if ( generationOptions . stepSize && generationOptions . stepSize > 0 ) {
23
- spacing = generationOptions . stepSize ;
23
+ if ( stepSize && stepSize > 0 ) {
24
+ spacing = stepSize ;
24
25
} else {
25
- var niceRange = helpers . niceNum ( dataRange . max - dataRange . min , false ) ;
26
+ niceRange = helpers . niceNum ( dataRange . max - dataRange . min , false ) ;
26
27
spacing = helpers . niceNum ( niceRange / ( generationOptions . maxTicks - 1 ) , true ) ;
27
28
28
29
precision = generationOptions . precision ;
29
- if ( precision !== undefined ) {
30
+ if ( ! helpers . isNullOrUndef ( precision ) ) {
30
31
// If the user specified a precision, round to that number of decimal places
31
32
factor = Math . pow ( 10 , precision ) ;
32
33
spacing = Math . ceil ( spacing * factor ) / factor ;
33
34
}
34
35
}
35
- var niceMin = Math . floor ( dataRange . min / spacing ) * spacing ;
36
- var niceMax = Math . ceil ( dataRange . max / spacing ) * spacing ;
36
+ // If a precision is not specified, calculate factor based on spacing
37
+ if ( ! factor ) {
38
+ factor = Math . pow ( 10 , helpers . decimalPlaces ( spacing ) ) ;
39
+ }
40
+ niceMin = Math . floor ( dataRange . min / spacing ) * spacing ;
41
+ niceMax = Math . ceil ( dataRange . max / spacing ) * spacing ;
37
42
38
43
// If min, max and stepSize is set and they make an evenly spaced scale use it.
39
- if ( ! helpers . isNullOrUndef ( generationOptions . min ) && ! helpers . isNullOrUndef ( generationOptions . max ) && generationOptions . stepSize ) {
44
+ if ( ! helpers . isNullOrUndef ( min ) && ! helpers . isNullOrUndef ( max ) && stepSize ) {
40
45
// If very close to our whole number, use it.
41
- if ( helpers . almostWhole ( ( generationOptions . max - generationOptions . min ) / generationOptions . stepSize , spacing / 1000 ) ) {
42
- niceMin = generationOptions . min ;
43
- niceMax = generationOptions . max ;
46
+ if ( helpers . almostWhole ( ( max - min ) / stepSize , spacing / 1000 ) ) {
47
+ niceMin = min ;
48
+ niceMax = max ;
44
49
}
45
50
}
46
51
47
- var numSpaces = ( niceMax - niceMin ) / spacing ;
52
+ numSpaces = ( niceMax - niceMin ) / spacing ;
48
53
// If very close to our rounded value, use it.
49
54
if ( helpers . almostEquals ( numSpaces , Math . round ( numSpaces ) , spacing / 1000 ) ) {
50
55
numSpaces = Math . round ( numSpaces ) ;
51
56
} else {
52
57
numSpaces = Math . ceil ( numSpaces ) ;
53
58
}
54
59
55
- precision = 1 ;
56
- if ( spacing < 1 ) {
57
- precision = Math . pow ( 10 , 1 - Math . floor ( helpers . log10 ( spacing ) ) ) ;
58
- niceMin = Math . round ( niceMin * precision ) / precision ;
59
- niceMax = Math . round ( niceMax * precision ) / precision ;
60
- }
61
- ticks . push ( generationOptions . min !== undefined ? generationOptions . min : niceMin ) ;
60
+ niceMin = Math . round ( niceMin * factor ) / factor ;
61
+ niceMax = Math . round ( niceMax * factor ) / factor ;
62
+ ticks . push ( helpers . isNullOrUndef ( min ) ? niceMin : min ) ;
62
63
for ( var j = 1 ; j < numSpaces ; ++ j ) {
63
- ticks . push ( Math . round ( ( niceMin + j * spacing ) * precision ) / precision ) ;
64
+ ticks . push ( Math . round ( ( niceMin + j * spacing ) * factor ) / factor ) ;
64
65
}
65
- ticks . push ( generationOptions . max !== undefined ? generationOptions . max : niceMax ) ;
66
+ ticks . push ( helpers . isNullOrUndef ( max ) ? niceMax : max ) ;
66
67
67
68
return ticks ;
68
69
}
0 commit comments