Skip to content

Commit f81538f

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

File tree

2 files changed

+43
-15
lines changed

2 files changed

+43
-15
lines changed

src/core/core.scale.js

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

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)