Skip to content

Commit 8dfcf1c

Browse files
authored
Use maxTicksLimit option to calculate the labels size on ticks (#11069)
* Use maxTicksLimit option to calculate the labels size on ticks * apply review
1 parent 1db46ef commit 8dfcf1c

File tree

3 files changed

+43
-4
lines changed

3 files changed

+43
-4
lines changed

src/core/core.scale.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {autoSkip} from './core.scale.autoskip.js';
88

99
const reverseAlign = (align) => align === 'left' ? 'right' : align === 'right' ? 'left' : align;
1010
const offsetFromEdge = (scale, edge, offset) => edge === 'top' || edge === 'left' ? scale[edge] + offset : scale[edge] - offset;
11+
const getTicksLimit = (ticksLength, maxTicksLimit) => Math.min(maxTicksLimit || ticksLength, ticksLength);
1112

1213
/**
1314
* @typedef { import('./core.controller.js').default } Chart
@@ -585,7 +586,7 @@ export default class Scale extends Element {
585586
calculateLabelRotation() {
586587
const options = this.options;
587588
const tickOpts = options.ticks;
588-
const numTicks = this.ticks.length;
589+
const numTicks = getTicksLimit(this.ticks.length, options.ticks.maxTicksLimit);
589590
const minRotation = tickOpts.minRotation || 0;
590591
const maxRotation = tickOpts.maxRotation;
591592
let labelRotation = minRotation;
@@ -803,7 +804,7 @@ export default class Scale extends Element {
803804
ticks = sample(ticks, sampleSize);
804805
}
805806

806-
this._labelSizes = labelSizes = this._computeLabelSizes(ticks, ticks.length);
807+
this._labelSizes = labelSizes = this._computeLabelSizes(ticks, ticks.length, this.options.ticks.maxTicksLimit);
807808
}
808809

809810
return labelSizes;
@@ -815,15 +816,16 @@ export default class Scale extends Element {
815816
* @return {{ first: object, last: object, widest: object, highest: object, widths: Array, heights: array }}
816817
* @private
817818
*/
818-
_computeLabelSizes(ticks, length) {
819+
_computeLabelSizes(ticks, length, maxTicksLimit) {
819820
const {ctx, _longestTextCache: caches} = this;
820821
const widths = [];
821822
const heights = [];
823+
const increment = Math.floor(length / getTicksLimit(length, maxTicksLimit));
822824
let widestLabelSize = 0;
823825
let highestLabelSize = 0;
824826
let i, j, jlen, label, tickFont, fontString, cache, lineHeight, width, height, nestedLabel;
825827

826-
for (i = 0; i < length; ++i) {
828+
for (i = 0; i < length; i += increment) {
827829
label = ticks[i].label;
828830
tickFont = this._resolveTickFontOptions(i);
829831
ctx.font = fontString = tickFont.string;
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
const data = Array.from({length: 42}, (_, i) => i + 1);
2+
const labels = data.map(v => 'tick' + v);
3+
4+
module.exports = {
5+
description: 'https://github.com/chartjs/Chart.js/issues/10856',
6+
config: {
7+
type: 'bar',
8+
data: {
9+
datasets: [{
10+
data
11+
}],
12+
labels
13+
},
14+
options: {
15+
scales: {
16+
x: {
17+
ticks: {
18+
display: true,
19+
maxTicksLimit: 6
20+
},
21+
grid: {
22+
color: 'red'
23+
}
24+
},
25+
y: {display: false}
26+
},
27+
layout: {
28+
padding: {
29+
right: 2
30+
}
31+
}
32+
}
33+
},
34+
options: {
35+
spriteText: true
36+
}
37+
};
17.6 KB
Loading

0 commit comments

Comments
 (0)