Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply maxTicksLimit to grid when ticks are hidden #9277

Merged
merged 1 commit into from Jun 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/core/core.scale.js
@@ -1,6 +1,6 @@
import Element from './core.element';
import {_alignPixel, _measureText, renderText, clipArea, unclipArea} from '../helpers/helpers.canvas';
import {callback as call, each, finiteOrDefault, isArray, isFinite, isNullOrUndef, isObject} from '../helpers/helpers.core';
import {callback as call, each, finiteOrDefault, isArray, isFinite, isNullOrUndef, isObject, valueOrDefault} from '../helpers/helpers.core';
import {toDegrees, toRadians, _int16Range, _limitValue, HALF_PI} from '../helpers/helpers.math';
import {_alignStartEnd, _toLeftRightCenter} from '../helpers/helpers.extras';
import {toFont, toPadding, _addGrace} from '../helpers/helpers.options';
Expand Down Expand Up @@ -1072,7 +1072,9 @@ export default class Scale extends Element {
x2 = chartArea.right;
}

for (i = 0; i < ticksLength; ++i) {
const limit = valueOrDefault(options.ticks.maxTicksLimit, ticksLength);
const step = Math.max(1, Math.ceil(ticksLength / limit));
for (i = 0; i < ticksLength; i += step) {
const optsAtIndex = grid.setContext(me.getContext(i));

const lineWidth = optsAtIndex.lineWidth;
Expand Down
37 changes: 37 additions & 0 deletions test/fixtures/scale.category/max-ticks-limit-a.js
@@ -0,0 +1,37 @@
const data = Array.from({length: 42}, (_, i) => i + 1);
const labels = data.map(v => 'tick' + v);

module.exports = {
description: 'https://github.com/chartjs/Chart.js/issues/7302',
config: {
type: 'bar',
data: {
datasets: [{
data
}],
labels
},
options: {
scales: {
x: {
ticks: {
display: false,
maxTicksLimit: 7
},
grid: {
color: 'red'
}
},
y: {display: false}
},
layout: {
padding: {
right: 2
}
}
}
},
options: {
spriteText: true
}
};
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
37 changes: 37 additions & 0 deletions test/fixtures/scale.category/max-ticks-limit-b.js
@@ -0,0 +1,37 @@
const data = Array.from({length: 42}, (_, i) => i + 1);
const labels = data.map(v => 'tick' + v);

module.exports = {
description: 'https://github.com/chartjs/Chart.js/issues/7302',
config: {
type: 'bar',
data: {
datasets: [{
data
}],
labels
},
options: {
scales: {
x: {
ticks: {
display: false,
maxTicksLimit: 6
},
grid: {
color: 'red'
}
},
y: {display: false}
},
layout: {
padding: {
right: 2
}
}
}
},
options: {
spriteText: true
}
};
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.