@@ -16,52 +16,41 @@ function generateTicks(generationOptions, dataRange) {
1616 // for details.
1717
1818 var stepSize = generationOptions . stepSize ;
19+ var unit = stepSize > 0 ? stepSize : 1 ;
20+ var maxNumSpaces = generationOptions . maxTicks - 1 ;
1921 var min = generationOptions . min ;
2022 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 ;
5233 }
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
5541 factor = Math . pow ( 10 , helpers . decimalPlaces ( spacing ) ) ;
5642 }
43+
5744 niceMin = Math . floor ( dataRange . min / spacing ) * spacing ;
5845 niceMax = Math . ceil ( dataRange . max / spacing ) * spacing ;
5946
6047 // 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 ) {
6249 // 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 ) ) {
6451 niceMin = min ;
52+ }
53+ if ( ! helpers . isNullOrUndef ( max ) && helpers . almostWhole ( max / spacing , spacing / 1000 ) ) {
6554 niceMax = max ;
6655 }
6756 }
@@ -180,7 +169,6 @@ module.exports = function(Chart) {
180169
181170 var numericGeneratorOptions = {
182171 maxTicks : maxTicks ,
183- maxTicksLimit : tickOpts . maxTicksLimit ,
184172 min : tickOpts . min ,
185173 max : tickOpts . max ,
186174 precision : tickOpts . precision ,
0 commit comments