Skip to content

Commit c6d57de

Browse files
committed
autoSkip: properly calculate space needed by tick label
1 parent 52b9793 commit c6d57de

File tree

2 files changed

+41
-15
lines changed

2 files changed

+41
-15
lines changed

src/core/core.scale.js

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -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++) {

test/specs/core.scale.tests.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,14 @@ describe('Core.scale', function() {
6363
labels: [
6464
'January 2018', 'February 2018', 'March 2018', 'April 2018',
6565
'May 2018', 'June 2018', 'July 2018', 'August 2018',
66-
'September 2018', 'October 2018', 'November 2018', 'December 2018'
66+
'September 2018', 'October 2018', 'November 2018', 'December 2018',
67+
'January 2019', 'February 2019', 'March 2019', 'April 2019',
68+
'May 2019', 'June 2019', 'July 2019', 'August 2019',
69+
'September 2019', 'October 2019', 'November 2019', 'December 2019',
70+
'January 2020', 'February 2020'
6771
],
6872
datasets: [{
69-
data: [12, 19, 3, 5, 2, 3, 7, 8, 9, 10, 11, 12]
73+
data: [12, 19, 3, 5, 2, 3, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
7074
}]
7175
});
7276

0 commit comments

Comments
 (0)