@@ -640,9 +640,33 @@ module.exports = Element.extend({
640640 var isHorizontal = me . isHorizontal ( ) ;
641641 var optionTicks = me . options . ticks . minor ;
642642 var tickCount = ticks . length ;
643- var labelRotationRadians = helpers . toRadians ( me . labelRotation ) ;
644- var cosRotation = Math . cos ( labelRotationRadians ) ;
645- var longestRotatedLabel = me . longestLabelWidth * cosRotation ;
643+
644+ // Calculate space needed by label in axis direction.
645+ // Idea from https://github.com/chartjs/Chart.js/pull/5252/files#r207742171
646+ var rot = helpers . toRadians ( me . labelRotation ) ;
647+ var cos = Math . abs ( Math . cos ( rot ) ) ;
648+ var sin = Math . abs ( Math . sin ( rot ) ) ;
649+
650+ var padding = optionTicks . autoSkipPadding ;
651+ var w = me . longestLabelWidth + padding || 0 ;
652+
653+ // FIXME: Add support for multiline labels
654+ var tickFont = parseFontOptions ( optionTicks ) ;
655+ var h = tickFont . size * 1.2 + padding ;
656+
657+ // Calculate space needed for 1 tick in axis direction.
658+ var tickSize = isHorizontal
659+ ? ( h * cos > w * sin ? w / cos : h / sin )
660+ : ( h * sin < w * cos ? h / cos : w / sin ) ;
661+
662+ // Total space needed to display all ticks. First and last ticks are drawn as their center at end of axis, so tickCount-1
663+ var ticksLength = tickSize * ( tickCount - 1 ) ;
664+
665+ // Axis length
666+ var axisLength = isHorizontal
667+ ? me . width - ( me . paddingLeft + me . paddingRight )
668+ : me . height - ( me . paddingTop + me . PaddingBottom ) ;
669+
646670 var result = [ ] ;
647671 var i , tick ;
648672
@@ -652,18 +676,16 @@ module.exports = Element.extend({
652676 maxTicks = optionTicks . maxTicksLimit ;
653677 }
654678
655- if ( isHorizontal ) {
656- skipRatio = false ;
679+ skipRatio = false ;
657680
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- }
681+ if ( ticksLength > axisLength ) {
682+ skipRatio = 1 + Math . floor ( ticksLength / axisLength ) ;
683+ }
661684
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- }
685+ // if they defined a max number of optionTicks,
686+ // increase skipRatio until that number is met
687+ if ( maxTicks && tickCount > maxTicks ) {
688+ skipRatio = Math . max ( skipRatio , 1 + Math . floor ( tickCount / maxTicks ) ) ;
667689 }
668690
669691 for ( i = 0 ; i < tickCount ; i ++ ) {
0 commit comments