@@ -16,52 +16,41 @@ function generateTicks(generationOptions, dataRange) {
16
16
// for details.
17
17
18
18
var stepSize = generationOptions . stepSize ;
19
+ var unit = stepSize > 0 ? stepSize : 1 ;
20
+ var maxNumSpaces = generationOptions . maxTicks - 1 ;
19
21
var min = generationOptions . min ;
20
22
var max = generationOptions . max ;
21
- var spacing , precision , factor , niceMin , niceMax , numSpaces , maxNumSpaces ;
22
-
23
- if ( stepSize && stepSize > 0 ) {
24
- spacing = stepSize ;
25
- if ( generationOptions . maxTicksLimit ) {
26
- maxNumSpaces = generationOptions . maxTicksLimit - 1 ;
27
- // spacing is set to stepSize multiplied by a nice number of
28
- // Math.ceil((max - min) / maxNumSpaces / stepSize) = num of steps that should be grouped
29
- spacing *= helpers . niceNum ( Math . ceil ( ( dataRange . max - dataRange . min ) / maxNumSpaces / stepSize ) ) ;
30
- numSpaces = Math . ceil ( dataRange . max / spacing ) - Math . floor ( dataRange . min / spacing ) ;
31
- if ( numSpaces > maxNumSpaces ) {
32
- // If the calculated num of spaces exceeds maxNumSpaces, recalculate it
33
- spacing = helpers . niceNum ( Math . ceil ( numSpaces * spacing / maxNumSpaces / stepSize ) ) * stepSize ;
34
- }
35
- }
36
- } else {
37
- maxNumSpaces = generationOptions . maxTicks - 1 ;
38
- // spacing is set to a nice number of (max - min) / maxNumSpaces
39
- spacing = helpers . niceNum ( ( dataRange . max - dataRange . min ) / maxNumSpaces ) ;
40
- numSpaces = Math . ceil ( dataRange . max / spacing ) - Math . floor ( dataRange . min / spacing ) ;
41
- if ( numSpaces > maxNumSpaces ) {
42
- // If the calculated num of spaces exceeds maxNumSpaces, recalculate it
43
- spacing = helpers . niceNum ( numSpaces * spacing / maxNumSpaces ) ;
44
- }
45
-
46
- precision = generationOptions . precision ;
47
- if ( ! helpers . isNullOrUndef ( precision ) ) {
48
- // If the user specified a precision, round to that number of decimal places
49
- factor = Math . pow ( 10 , precision ) ;
50
- spacing = Math . ceil ( spacing * factor ) / factor ;
51
- }
23
+ var precision = generationOptions . precision ;
24
+ var spacing , factor , niceMin , niceMax , numSpaces ;
25
+
26
+ // spacing is set to a nice number of the dataRange divided by maxNumSpaces.
27
+ // stepSize is used as a minimum unit if it is specified.
28
+ spacing = helpers . niceNum ( ( dataRange . max - dataRange . min ) / maxNumSpaces / unit ) * unit ;
29
+ numSpaces = Math . ceil ( dataRange . max / spacing ) - Math . floor ( dataRange . min / spacing ) ;
30
+ if ( numSpaces > maxNumSpaces ) {
31
+ // If the calculated num of spaces exceeds maxNumSpaces, recalculate it
32
+ spacing = helpers . niceNum ( numSpaces * spacing / maxNumSpaces / unit ) * unit ;
52
33
}
53
- // If a precision is not specified, calculate factor based on spacing
54
- if ( ! factor ) {
34
+
35
+ if ( ! ( stepSize > 0 ) && ! helpers . isNullOrUndef ( precision ) ) {
36
+ // If the user specified a precision, round to that number of decimal places
37
+ factor = Math . pow ( 10 , precision ) ;
38
+ spacing = Math . ceil ( spacing * factor ) / factor ;
39
+ } else {
40
+ // If a precision is not specified, calculate factor based on spacing
55
41
factor = Math . pow ( 10 , helpers . decimalPlaces ( spacing ) ) ;
56
42
}
43
+
57
44
niceMin = Math . floor ( dataRange . min / spacing ) * spacing ;
58
45
niceMax = Math . ceil ( dataRange . max / spacing ) * spacing ;
59
46
60
47
// If min, max and stepSize is set and they make an evenly spaced scale use it.
61
- if ( ! helpers . isNullOrUndef ( min ) && ! helpers . isNullOrUndef ( max ) && stepSize ) {
48
+ if ( stepSize > 0 ) {
62
49
// If very close to our whole number, use it.
63
- if ( helpers . almostWhole ( ( max - min ) / stepSize , spacing / 1000 ) ) {
50
+ if ( ! helpers . isNullOrUndef ( min ) && helpers . almostWhole ( min / spacing , spacing / 1000 ) ) {
64
51
niceMin = min ;
52
+ }
53
+ if ( ! helpers . isNullOrUndef ( max ) && helpers . almostWhole ( max / spacing , spacing / 1000 ) ) {
65
54
niceMax = max ;
66
55
}
67
56
}
@@ -180,7 +169,6 @@ module.exports = function(Chart) {
180
169
181
170
var numericGeneratorOptions = {
182
171
maxTicks : maxTicks ,
183
- maxTicksLimit : tickOpts . maxTicksLimit ,
184
172
min : tickOpts . min ,
185
173
max : tickOpts . max ,
186
174
precision : tickOpts . precision ,
0 commit comments