@@ -16,35 +16,41 @@ function generateTicks(generationOptions, dataRange) {
16
16
// for details.
17
17
18
18
var stepSize = generationOptions . stepSize ;
19
+ var unit = 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 , niceRange , niceMin , niceMax , numSpaces ;
22
-
23
- if ( stepSize && stepSize > 0 ) {
24
- spacing = stepSize ;
25
- } else {
26
- niceRange = helpers . niceNum ( dataRange . max - dataRange . min , false ) ;
27
- spacing = helpers . niceNum ( niceRange / ( generationOptions . maxTicks - 1 ) , true ) ;
28
-
29
- precision = generationOptions . precision ;
30
- if ( ! helpers . isNullOrUndef ( precision ) ) {
31
- // If the user specified a precision, round to that number of decimal places
32
- factor = Math . pow ( 10 , precision ) ;
33
- spacing = Math . ceil ( spacing * factor ) / factor ;
34
- }
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 ;
35
33
}
36
- // If a precision is not specified, calculate factor based on spacing
37
- if ( ! factor ) {
34
+
35
+ if ( stepSize || helpers . isNullOrUndef ( precision ) ) {
36
+ // If a precision is not specified, calculate factor based on spacing
38
37
factor = Math . pow ( 10 , helpers . decimalPlaces ( spacing ) ) ;
38
+ } else {
39
+ // If the user specified a precision, round to that number of decimal places
40
+ factor = Math . pow ( 10 , precision ) ;
41
+ spacing = Math . ceil ( spacing * factor ) / factor ;
39
42
}
43
+
40
44
niceMin = Math . floor ( dataRange . min / spacing ) * spacing ;
41
45
niceMax = Math . ceil ( dataRange . max / spacing ) * spacing ;
42
46
43
47
// If min, max and stepSize is set and they make an evenly spaced scale use it.
44
- if ( ! helpers . isNullOrUndef ( min ) && ! helpers . isNullOrUndef ( max ) && stepSize ) {
48
+ if ( stepSize ) {
45
49
// If very close to our whole number, use it.
46
- if ( helpers . almostWhole ( ( max - min ) / stepSize , spacing / 1000 ) ) {
50
+ if ( ! helpers . isNullOrUndef ( min ) && helpers . almostWhole ( min / spacing , spacing / 1000 ) ) {
47
51
niceMin = min ;
52
+ }
53
+ if ( ! helpers . isNullOrUndef ( max ) && helpers . almostWhole ( max / spacing , spacing / 1000 ) ) {
48
54
niceMax = max ;
49
55
}
50
56
}
@@ -146,7 +152,32 @@ module.exports = function(Chart) {
146
152
}
147
153
}
148
154
} ,
149
- getTickLimit : noop ,
155
+
156
+ getTickLimit : function ( ) {
157
+ var me = this ;
158
+ var tickOpts = me . options . ticks ;
159
+ var stepSize = tickOpts . stepSize ;
160
+ var maxTicksLimit = tickOpts . maxTicksLimit ;
161
+ var maxTicks ;
162
+
163
+ if ( stepSize ) {
164
+ maxTicks = Math . ceil ( me . max / stepSize ) - Math . floor ( me . min / stepSize ) + 1 ;
165
+ } else {
166
+ maxTicks = me . _computeTickLimit ( ) ;
167
+ maxTicksLimit = maxTicksLimit || 11 ;
168
+ }
169
+
170
+ if ( maxTicksLimit ) {
171
+ maxTicks = Math . min ( maxTicksLimit , maxTicks ) ;
172
+ }
173
+
174
+ return maxTicks ;
175
+ } ,
176
+
177
+ _computeTickLimit : function ( ) {
178
+ return Number . POSITIVE_INFINITY ;
179
+ } ,
180
+
150
181
handleDirectionalChanges : noop ,
151
182
152
183
buildTicks : function ( ) {
@@ -155,7 +186,7 @@ module.exports = function(Chart) {
155
186
var tickOpts = opts . ticks ;
156
187
157
188
// Figure out what the max number of ticks we can support it is based on the size of
158
- // the axis area. For now, we say that the minimum tick spacing in pixels must be 50
189
+ // the axis area. For now, we say that the minimum tick spacing in pixels must be 40
159
190
// We also limit the maximum number of ticks to 11 which gives a nice 10 squares on
160
191
// the graph. Make sure we always have at least 2 ticks
161
192
var maxTicks = me . getTickLimit ( ) ;
0 commit comments