@@ -192,18 +192,18 @@ function calculateSpacing(majorIndices, ticks, axisLength, ticksLimit) {
192192 return Math . max ( spacing , 1 ) ;
193193}
194194
195- function getMajorIndices ( ticks ) {
195+ function getMajorIndices ( meta ) {
196196 const result = [ ] ;
197197 let i , ilen ;
198- for ( i = 0 , ilen = ticks . length ; i < ilen ; i ++ ) {
199- if ( ticks [ i ] . major ) {
198+ for ( i = 0 , ilen = meta . length ; i < ilen ; i ++ ) {
199+ if ( meta [ i ] && meta [ i ] . major ) {
200200 result . push ( i ) ;
201201 }
202202 }
203203 return result ;
204204}
205205
206- function skipMajors ( ticks , newTicks , majorIndices , spacing ) {
206+ function skipMajors ( ticks , meta , newTicks , newMeta , majorIndices , spacing ) {
207207 let count = 0 ;
208208 let next = majorIndices [ 0 ] ;
209209 let i ;
@@ -212,13 +212,14 @@ function skipMajors(ticks, newTicks, majorIndices, spacing) {
212212 for ( i = 0 ; i < ticks . length ; i ++ ) {
213213 if ( i === next ) {
214214 newTicks . push ( ticks [ i ] ) ;
215+ newMeta . push ( meta [ i ] ) ;
215216 count ++ ;
216217 next = majorIndices [ count * spacing ] ;
217218 }
218219 }
219220}
220221
221- function skip ( ticks , newTicks , spacing , majorStart , majorEnd ) {
222+ function skip ( ticks , meta , newTicks , newMeta , spacing , majorStart , majorEnd ) {
222223 const start = valueOrDefault ( majorStart , 0 ) ;
223224 const end = Math . min ( valueOrDefault ( majorEnd , ticks . length ) , ticks . length ) ;
224225 let count = 0 ;
@@ -240,6 +241,7 @@ function skip(ticks, newTicks, spacing, majorStart, majorEnd) {
240241 for ( i = Math . max ( start , 0 ) ; i < end ; i ++ ) {
241242 if ( i === next ) {
242243 newTicks . push ( ticks [ i ] ) ;
244+ newMeta . push ( meta [ i ] ) ;
243245 count ++ ;
244246 next = Math . round ( start + count * spacing ) ;
245247 }
@@ -373,7 +375,7 @@ class Scale extends Element {
373375 const me = this ;
374376 const tickOpts = me . options . ticks ;
375377 const sampleSize = tickOpts . sampleSize ;
376- let samplingEnabled ;
378+ const autoSkipEnabled = tickOpts . autoSkip || tickOpts . source === 'auto' ;
377379
378380 // Update Lifecycle - Probably don't want to ever extend or overwrite this function ;)
379381 me . beforeUpdate ( ) ;
@@ -390,6 +392,7 @@ class Scale extends Element {
390392 } , margins ) ;
391393
392394 me . ticks = null ;
395+ me . _tickMeta = [ ] ;
393396 me . _labelSizes = null ;
394397 me . _maxLabelLines = 0 ;
395398 me . _longestTextCache = me . _longestTextCache || { } ;
@@ -415,8 +418,8 @@ class Scale extends Element {
415418
416419 // Compute tick rotation and fit using a sampled subset of labels
417420 // We generally don't need to compute the size of every single label for determining scale size
418- samplingEnabled = sampleSize < me . ticks . length ;
419- me . _convertTicksToLabels ( samplingEnabled ? sample ( me . ticks , sampleSize ) : me . ticks ) ;
421+ const samplingEnabled = sampleSize < me . ticks . length ;
422+ me . labels = me . _convertTicksToLabels ( samplingEnabled ? sample ( me . ticks , sampleSize ) : me . ticks ) ;
420423
421424 // _configure is called twice, once here, once from core.controller.updateLayout.
422425 // Here we haven't been positioned yet, but dimensions are correct.
@@ -434,11 +437,13 @@ class Scale extends Element {
434437 me . afterFit ( ) ;
435438
436439 // Auto-skip
437- me . ticks = tickOpts . display && ( tickOpts . autoSkip || tickOpts . source === 'auto' ) ? me . _autoSkip ( me . ticks ) : me . ticks ;
440+ if ( tickOpts . display && autoSkipEnabled ) {
441+ me . _autoSkip ( ) ;
442+ }
438443
439- if ( samplingEnabled ) {
444+ if ( tickOpts . display && ( samplingEnabled || autoSkipEnabled ) ) {
440445 // Generate labels using all non-skipped ticks
441- me . _convertTicksToLabels ( me . ticks ) ;
446+ me . labels = me . _convertTicksToLabels ( me . ticks ) ;
442447 }
443448
444449 // IMPORTANT: after this point, we consider that `this.ticks` will NEVER change!
@@ -528,14 +533,15 @@ class Scale extends Element {
528533 /**
529534 * Convert ticks to label strings
530535 */
531- generateTickLabels ( ticks ) {
536+ convertTicksToLabels ( ticks ) {
532537 const me = this ;
533538 const tickOpts = me . options . ticks ;
534- let i , ilen , tick ;
539+ const labels = [ ] ;
540+ let i , ilen ;
535541 for ( i = 0 , ilen = ticks . length ; i < ilen ; i ++ ) {
536- tick = ticks [ i ] ;
537- tick . label = helpers . callback ( tickOpts . callback , [ tick . value , i , ticks ] , me ) ;
542+ labels [ i ] = helpers . callback ( tickOpts . callback , [ ticks [ i ] , i , ticks ] , me ) ;
538543 }
544+ return labels ;
539545 }
540546 afterTickToLabelConversion ( ) {
541547 helpers . callback ( this . options . afterTickToLabelConversion , [ this ] ) ;
@@ -729,9 +735,11 @@ class Scale extends Element {
729735
730736 me . beforeTickToLabelConversion ( ) ;
731737
732- me . generateTickLabels ( ticks ) ;
738+ const labels = me . convertTicksToLabels ( ticks ) ;
733739
734740 me . afterTickToLabelConversion ( ) ;
741+
742+ return labels ;
735743 }
736744
737745 /**
@@ -762,16 +770,17 @@ class Scale extends Element {
762770 const widths = [ ] ;
763771 const heights = [ ] ;
764772 const offsets = [ ] ;
765- let ticks = me . ticks ;
766- if ( sampleSize < ticks . length ) {
767- ticks = sample ( ticks , sampleSize ) ;
773+ let labels = me . labels ;
774+ if ( sampleSize < labels . length ) {
775+ labels = sample ( labels , sampleSize ) ;
768776 }
769- const length = ticks . length ;
770- let i , j , jlen , label , tickFont , fontString , cache , lineHeight , width , height , nestedLabel , widest , highest ;
777+ const length = labels . length ;
778+ let i , j , jlen , label , meta , tickFont , fontString , cache , lineHeight , width , height , nestedLabel , widest , highest ;
771779
772780 for ( i = 0 ; i < length ; ++ i ) {
773- label = ticks [ i ] . label ;
774- tickFont = ticks [ i ] . major ? tickFonts . major : tickFonts . minor ;
781+ label = labels [ i ] ;
782+ meta = me . _tickMeta [ i ] ;
783+ tickFont = meta && meta . major ? tickFonts . major : tickFonts . minor ;
775784 ctx . font = fontString = tickFont . string ;
776785 cache = caches [ fontString ] = caches [ fontString ] || { data : { } , gc : [ ] } ;
777786 lineHeight = tickFont . lineHeight ;
@@ -894,37 +903,46 @@ class Scale extends Element {
894903 * Returns a subset of ticks to be plotted to avoid overlapping labels.
895904 * @private
896905 */
897- _autoSkip ( ticks ) {
906+ _autoSkip ( ) {
898907 const me = this ;
899908 const tickOpts = me . options . ticks ;
909+ const ticks = me . ticks ;
910+ const meta = me . _tickMeta ;
900911 const axisLength = me . _length ;
901912 const ticksLimit = tickOpts . maxTicksLimit || axisLength / me . _tickSize ( ) ;
902- const majorIndices = tickOpts . major . enabled ? getMajorIndices ( ticks ) : [ ] ;
913+ const majorIndices = tickOpts . major . enabled ? getMajorIndices ( meta ) : [ ] ;
903914 const numMajorIndices = majorIndices . length ;
904915 const first = majorIndices [ 0 ] ;
905916 const last = majorIndices [ numMajorIndices - 1 ] ;
906917 const newTicks = [ ] ;
918+ const newMeta = [ ] ;
907919
908920 // If there are too many major ticks to display them all
909921 if ( numMajorIndices > ticksLimit ) {
910- skipMajors ( ticks , newTicks , majorIndices , numMajorIndices / ticksLimit ) ;
911- return newTicks ;
922+ skipMajors ( ticks , meta , newTicks , newMeta , majorIndices , numMajorIndices / ticksLimit ) ;
923+ me . _tickMeta = newMeta ;
924+ me . ticks = newTicks ;
925+ return ;
912926 }
913927
914928 const spacing = calculateSpacing ( majorIndices , ticks , axisLength , ticksLimit ) ;
915929
916930 if ( numMajorIndices > 0 ) {
917931 let i , ilen ;
918932 const avgMajorSpacing = numMajorIndices > 1 ? Math . round ( ( last - first ) / ( numMajorIndices - 1 ) ) : null ;
919- skip ( ticks , newTicks , spacing , helpers . isNullOrUndef ( avgMajorSpacing ) ? 0 : first - avgMajorSpacing , first ) ;
933+ skip ( ticks , meta , newTicks , newMeta , spacing , helpers . isNullOrUndef ( avgMajorSpacing ) ? 0 : first - avgMajorSpacing , first ) ;
920934 for ( i = 0 , ilen = numMajorIndices - 1 ; i < ilen ; i ++ ) {
921- skip ( ticks , newTicks , spacing , majorIndices [ i ] , majorIndices [ i + 1 ] ) ;
935+ skip ( ticks , meta , newTicks , newMeta , spacing , majorIndices [ i ] , majorIndices [ i + 1 ] ) ;
922936 }
923- skip ( ticks , newTicks , spacing , last , helpers . isNullOrUndef ( avgMajorSpacing ) ? ticks . length : last + avgMajorSpacing ) ;
924- return newTicks ;
937+ skip ( ticks , meta , newTicks , newMeta , spacing , last , helpers . isNullOrUndef ( avgMajorSpacing ) ? ticks . length : last + avgMajorSpacing ) ;
938+ me . _tickMeta = newMeta ;
939+ me . ticks = newTicks ;
940+ return ;
925941 }
926- skip ( ticks , newTicks , spacing ) ;
927- return newTicks ;
942+ skip ( ticks , meta , newTicks , newMeta , spacing ) ;
943+ me . _tickMeta = newMeta ;
944+ me . ticks = newTicks ;
945+ return ;
928946 }
929947
930948 /**
@@ -946,8 +964,8 @@ class Scale extends Element {
946964
947965 // Calculate space needed for 1 tick in axis direction.
948966 return me . isHorizontal ( )
949- ? h * cos > w * sin ? w / cos : h / sin
950- : h * sin < w * cos ? h / cos : w / sin ;
967+ ? ( h * cos > w * sin ? w / cos : h / sin )
968+ : ( h * sin < w * cos ? h / cos : w / sin ) ;
951969 }
952970
953971 /**
@@ -1109,7 +1127,7 @@ class Scale extends Element {
11091127 const tl = getTickMarkLength ( options . gridLines ) ;
11101128 const rotation = - helpers . math . toRadians ( me . labelRotation ) ;
11111129 const items = [ ] ;
1112- let i , ilen , tick , label , x , y , textAlign , pixel , font , lineHeight , lineCount , textOffset ;
1130+ let i , ilen , meta , label , x , y , textAlign , pixel , font , lineHeight , lineCount , textOffset ;
11131131
11141132 if ( position === 'top' ) {
11151133 y = me . bottom - tl - tickPadding ;
@@ -1144,11 +1162,11 @@ class Scale extends Element {
11441162 }
11451163
11461164 for ( i = 0 , ilen = ticks . length ; i < ilen ; ++ i ) {
1147- tick = ticks [ i ] ;
1148- label = tick . label ;
1165+ meta = me . _tickMeta [ i ] ;
1166+ label = me . labels [ i ] ;
11491167
11501168 pixel = me . getPixelForTick ( i ) + optionTicks . labelOffset ;
1151- font = tick . major ? fonts . major : fonts . minor ;
1169+ font = meta && meta . major ? fonts . major : fonts . minor ;
11521170 lineHeight = font . lineHeight ;
11531171 lineCount = isArray ( label ) ? label . length : 1 ;
11541172
0 commit comments