@@ -192,6 +192,14 @@ function getTitleHeight(options, fallback) {
192192 return ( lines * font . lineHeight ) + padding . height ;
193193}
194194
195+ function determineMaxTicks ( scale ) {
196+ const offset = scale . options . offset ;
197+ const tickLength = scale . _tickSize ( ) ;
198+ const maxScale = scale . _length / tickLength + ( offset ? 0 : 1 ) ;
199+ const maxChart = scale . _maxLength / tickLength ;
200+ return Math . floor ( Math . min ( maxScale , maxChart ) ) ;
201+ }
202+
195203/**
196204 * @param {number[] } arr
197205 */
@@ -411,6 +419,7 @@ export default class Scale extends Element {
411419 /** @type {object|null } */
412420 this . _labelSizes = null ;
413421 this . _length = 0 ;
422+ this . _maxLength = 0 ;
414423 this . _longestTextCache = { } ;
415424 /** @type {number } */
416425 this . _startPixel = undefined ;
@@ -572,7 +581,7 @@ export default class Scale extends Element {
572581 // Absorb the master measurements
573582 me . maxWidth = maxWidth ;
574583 me . maxHeight = maxHeight ;
575- me . _margins = Object . assign ( {
584+ me . _margins = margins = Object . assign ( {
576585 left : 0 ,
577586 right : 0 ,
578587 top : 0 ,
@@ -589,6 +598,10 @@ export default class Scale extends Element {
589598 me . setDimensions ( ) ;
590599 me . afterSetDimensions ( ) ;
591600
601+ me . _maxLength = me . isHorizontal ( )
602+ ? me . width + margins . left + margins . right
603+ : me . height + margins . top + margins . bottom ;
604+
592605 // Data min/max
593606 if ( ! me . _dataLimitsCached ) {
594607 me . beforeDataLimits ( ) ;
@@ -620,18 +633,21 @@ export default class Scale extends Element {
620633 me . calculateLabelRotation ( ) ; // Preconditions: number of ticks and sizes of largest labels must be calculated beforehand
621634 me . afterCalculateLabelRotation ( ) ;
622635
623- me . beforeFit ( ) ;
624- me . fit ( ) ; // Preconditions: label rotation and label sizes must be calculated beforehand
625- me . afterFit ( ) ;
626-
627636 // Auto-skip
628- me . ticks = tickOpts . display && ( tickOpts . autoSkip || tickOpts . source === 'auto' ) ? me . _autoSkip ( me . ticks ) : me . ticks ;
637+ if ( tickOpts . display && ( tickOpts . autoSkip || tickOpts . source === 'auto' ) ) {
638+ me . ticks = me . _autoSkip ( me . ticks ) ;
639+ me . _labelSizes = null ;
640+ }
629641
630642 if ( samplingEnabled ) {
631643 // Generate labels using all non-skipped ticks
632644 me . _convertTicksToLabels ( me . ticks ) ;
633645 }
634646
647+ me . beforeFit ( ) ;
648+ me . fit ( ) ; // Preconditions: label rotation and label sizes must be calculated beforehand
649+ me . afterFit ( ) ;
650+
635651 // IMPORTANT: after this point, we consider that `this.ticks` will NEVER change!
636652
637653 me . afterUpdate ( ) ;
@@ -1162,8 +1178,8 @@ export default class Scale extends Element {
11621178 */
11631179 _autoSkip ( ticks ) {
11641180 const me = this ;
1165- const { offset , ticks : tickOpts } = me . options ;
1166- const ticksLimit = tickOpts . maxTicksLimit || ( me . _length / me . _tickSize ( ) + ( offset ? 0 : 1 ) ) ;
1181+ const tickOpts = me . options . ticks ;
1182+ const ticksLimit = tickOpts . maxTicksLimit || determineMaxTicks ( me ) ;
11671183 const majorIndices = tickOpts . major . enabled ? getMajorIndices ( ticks ) : [ ] ;
11681184 const numMajorIndices = majorIndices . length ;
11691185 const first = majorIndices [ 0 ] ;
0 commit comments