Skip to content

Commit 9799dbd

Browse files
authored
Scale: autoSkip before fit (#8627)
Scale: autoSkip now occurs before fit in the update process
1 parent aae8a06 commit 9799dbd

File tree

10 files changed

+80
-12
lines changed

10 files changed

+80
-12
lines changed

src/core/core.scale.js

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,14 @@ function getTitleHeight(options, fallback) {
192192
return (lines * font.lineHeight) + padding.height;
193193
}
194194

195+
function determineMaxTicks(scale) {
196+
const offset = scale.options.offset;
197+
const tickLength = scale._tickSize();
198+
const maxScale = scale._length / tickLength + (offset ? 0 : 1);
199+
const maxChart = scale._maxLength / tickLength;
200+
return Math.floor(Math.min(maxScale, maxChart));
201+
}
202+
195203
/**
196204
* @param {number[]} arr
197205
*/
@@ -411,6 +419,7 @@ export default class Scale extends Element {
411419
/** @type {object|null} */
412420
this._labelSizes = null;
413421
this._length = 0;
422+
this._maxLength = 0;
414423
this._longestTextCache = {};
415424
/** @type {number} */
416425
this._startPixel = undefined;
@@ -572,7 +581,7 @@ export default class Scale extends Element {
572581
// Absorb the master measurements
573582
me.maxWidth = maxWidth;
574583
me.maxHeight = maxHeight;
575-
me._margins = Object.assign({
584+
me._margins = margins = Object.assign({
576585
left: 0,
577586
right: 0,
578587
top: 0,
@@ -589,6 +598,10 @@ export default class Scale extends Element {
589598
me.setDimensions();
590599
me.afterSetDimensions();
591600

601+
me._maxLength = me.isHorizontal()
602+
? me.width + margins.left + margins.right
603+
: me.height + margins.top + margins.bottom;
604+
592605
// Data min/max
593606
if (!me._dataLimitsCached) {
594607
me.beforeDataLimits();
@@ -620,18 +633,21 @@ export default class Scale extends Element {
620633
me.calculateLabelRotation(); // Preconditions: number of ticks and sizes of largest labels must be calculated beforehand
621634
me.afterCalculateLabelRotation();
622635

623-
me.beforeFit();
624-
me.fit(); // Preconditions: label rotation and label sizes must be calculated beforehand
625-
me.afterFit();
626-
627636
// Auto-skip
628-
me.ticks = tickOpts.display && (tickOpts.autoSkip || tickOpts.source === 'auto') ? me._autoSkip(me.ticks) : me.ticks;
637+
if (tickOpts.display && (tickOpts.autoSkip || tickOpts.source === 'auto')) {
638+
me.ticks = me._autoSkip(me.ticks);
639+
me._labelSizes = null;
640+
}
629641

630642
if (samplingEnabled) {
631643
// Generate labels using all non-skipped ticks
632644
me._convertTicksToLabels(me.ticks);
633645
}
634646

647+
me.beforeFit();
648+
me.fit(); // Preconditions: label rotation and label sizes must be calculated beforehand
649+
me.afterFit();
650+
635651
// IMPORTANT: after this point, we consider that `this.ticks` will NEVER change!
636652

637653
me.afterUpdate();
@@ -1162,8 +1178,8 @@ export default class Scale extends Element {
11621178
*/
11631179
_autoSkip(ticks) {
11641180
const me = this;
1165-
const {offset, ticks: tickOpts} = me.options;
1166-
const ticksLimit = tickOpts.maxTicksLimit || (me._length / me._tickSize() + (offset ? 0 : 1));
1181+
const tickOpts = me.options.ticks;
1182+
const ticksLimit = tickOpts.maxTicksLimit || determineMaxTicks(me);
11671183
const majorIndices = tickOpts.major.enabled ? getMajorIndices(ticks) : [];
11681184
const numMajorIndices = majorIndices.length;
11691185
const first = majorIndices[0];

test/fixtures/core.layouts/refit-vertical-boxes.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
module.exports = {
2+
tolerance: 0.002,
23
config: {
34
type: 'line',
45
data: {
1.64 KB
Loading
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
module.exports = {
2+
description: 'https://github.com/chartjs/Chart.js/issues/3694',
3+
tolerance: 0.002,
4+
config: {
5+
type: 'line',
6+
data: {
7+
labels: [
8+
'Aaron',
9+
'Adam',
10+
'Albert',
11+
'Alex',
12+
'Allan',
13+
'Aman',
14+
'Anthony',
15+
'Autoenrolment',
16+
'Avril',
17+
'Bernard'
18+
],
19+
datasets: [{
20+
backgroundColor: 'rgba(252,233,79,0.5)',
21+
borderColor: 'rgba(252,233,79,1)',
22+
borderWidth: 1,
23+
data: [101,
24+
185,
25+
24,
26+
311,
27+
17,
28+
21,
29+
462,
30+
340,
31+
140,
32+
24
33+
]
34+
}],
35+
},
36+
options: {
37+
scales: {
38+
x: {
39+
backgroundColor: '#eee'
40+
}
41+
}
42+
}
43+
},
44+
options: {
45+
spriteText: true,
46+
canvas: {
47+
width: 185,
48+
height: 185
49+
}
50+
}
51+
};
16.2 KB
Loading
2.8 KB
Loading
2.8 KB
Loading
-64 Bytes
Loading

test/specs/core.scale.tests.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,14 @@ describe('Core.scale', function() {
6464
'January 2019', 'February 2019', 'March 2019', 'April 2019',
6565
'May 2019', 'June 2019', 'July 2019', 'August 2019',
6666
'September 2019', 'October 2019', 'November 2019', 'December 2019',
67-
'January 2020', 'February 2020'
67+
'January 2020', 'February 2020', 'March 2020', 'April 2020'
6868
],
6969
datasets: [{
70-
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]
70+
data: [1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7]
7171
}]
7272
});
7373

74-
expect(lastTick(chart).label).toEqual('January 2020');
74+
expect(lastTick(chart).label).toEqual('March 2020');
7575
});
7676
});
7777

test/specs/scale.time.tests.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1113,7 +1113,7 @@ describe('Time scale tests', function() {
11131113
});
11141114
const scale = chart.scales.x;
11151115
expect(scale.getPixelForDecimal(0)).toBeCloseToPixel(29);
1116-
expect(scale.getPixelForDecimal(1.0)).toBeCloseToPixel(494);
1116+
expect(scale.getPixelForDecimal(1.0)).toBeCloseToPixel(509);
11171117
});
11181118

11191119
['data', 'labels'].forEach(function(source) {

0 commit comments

Comments
 (0)