@@ -438,6 +438,9 @@ module.exports = Element.extend({
438438 var lineSpace = tickFont . size * 0.5 ;
439439 var tickPadding = me . options . ticks . padding ;
440440
441+ // Store max number of lines used in labels for _autoSkip
442+ me . maxLabelLines = tallestLabelHeightInLines ;
443+
441444 if ( isHorizontal ) {
442445 // A horizontal axis is more constrained by the height.
443446 me . longestLabelWidth = largestTextWidth ;
@@ -640,9 +643,32 @@ module.exports = Element.extend({
640643 var isHorizontal = me . isHorizontal ( ) ;
641644 var optionTicks = me . options . ticks . minor ;
642645 var tickCount = ticks . length ;
643- var labelRotationRadians = helpers . toRadians ( me . labelRotation ) ;
644- var cosRotation = Math . cos ( labelRotationRadians ) ;
645- var longestRotatedLabel = me . longestLabelWidth * cosRotation ;
646+
647+ // Calculate space needed by label in axis direction.
648+ // Idea from https://github.com/chartjs/Chart.js/pull/5252/files#r207742171
649+ var rot = helpers . toRadians ( me . labelRotation ) ;
650+ var cos = Math . abs ( Math . cos ( rot ) ) ;
651+ var sin = Math . abs ( Math . sin ( rot ) ) ;
652+
653+ var padding = optionTicks . autoSkipPadding ;
654+ var w = me . longestLabelWidth + padding || 0 ;
655+
656+ var tickFont = parseFontOptions ( optionTicks ) ;
657+ var h = me . maxLabelLines * tickFont . size * 1.2 + padding ;
658+
659+ // Calculate space needed for 1 tick in axis direction.
660+ var tickSize = isHorizontal
661+ ? ( h * cos > w * sin ? w / cos : h / sin )
662+ : ( h * sin < w * cos ? h / cos : w / sin ) ;
663+
664+ // Total space needed to display all ticks. First and last ticks are drawn as their center at end of axis, so tickCount-1
665+ var ticksLength = tickSize * ( tickCount - 1 ) ;
666+
667+ // Axis length
668+ var axisLength = isHorizontal
669+ ? me . width - ( me . paddingLeft + me . paddingRight )
670+ : me . height - ( me . paddingTop + me . PaddingBottom ) ;
671+
646672 var result = [ ] ;
647673 var i , tick ;
648674
@@ -652,18 +678,16 @@ module.exports = Element.extend({
652678 maxTicks = optionTicks . maxTicksLimit ;
653679 }
654680
655- if ( isHorizontal ) {
656- skipRatio = false ;
681+ skipRatio = false ;
657682
658- if ( ( longestRotatedLabel + optionTicks . autoSkipPadding ) * tickCount > ( me . width - ( me . paddingLeft + me . paddingRight ) ) ) {
659- skipRatio = 1 + Math . floor ( ( ( longestRotatedLabel + optionTicks . autoSkipPadding ) * tickCount ) / ( me . width - ( me . paddingLeft + me . paddingRight ) ) ) ;
660- }
683+ if ( ticksLength > axisLength ) {
684+ skipRatio = 1 + Math . floor ( ticksLength / axisLength ) ;
685+ }
661686
662- // if they defined a max number of optionTicks,
663- // increase skipRatio until that number is met
664- if ( maxTicks && tickCount > maxTicks ) {
665- skipRatio = Math . max ( skipRatio , Math . floor ( tickCount / maxTicks ) ) ;
666- }
687+ // if they defined a max number of optionTicks,
688+ // increase skipRatio until that number is met
689+ if ( maxTicks && tickCount > maxTicks ) {
690+ skipRatio = Math . max ( skipRatio , 1 + Math . floor ( tickCount / maxTicks ) ) ;
667691 }
668692
669693 for ( i = 0 ; i < tickCount ; i ++ ) {
0 commit comments