Fix autoskip logic (#10663)

* fix autoskip logic

* add test

* fix lint erro

* Update variable name
This commit is contained in:
Jacco van den Berg 2022-09-13 19:33:22 +02:00 committed by GitHub
parent 1c9e536de2
commit cc65c2bac2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 35 additions and 1 deletions

View File

@ -28,6 +28,7 @@ A number of changes were made to the configuration options passed to the `Chart`
* If the tooltip callback returns `undefined`, then the default callback will be used.
* `maintainAspectRatio` respects container height.
* Time and timeseries scales use `ticks.stepSize` instead of `time.stepSize`, which has been removed.
* `maxTickslimit` wont be used for the ticks in `autoSkip` if the determined max ticks is less then the `maxTicksLimit`.
#### Type changes
* The order of the `ChartMeta` parameters have been changed from `<Element, DatasetElement, Type>` to `<Type, Element, DatasetElement>`.

View File

@ -16,7 +16,8 @@ import {_factorize} from '../helpers/helpers.math';
*/
export function autoSkip(scale, ticks) {
const tickOpts = scale.options.ticks;
const ticksLimit = tickOpts.maxTicksLimit || determineMaxTicks(scale);
const determinedMaxTicks = determineMaxTicks(scale);
const ticksLimit = Math.min(tickOpts.maxTicksLimit || determinedMaxTicks, determinedMaxTicks);
const majorIndices = tickOpts.major.enabled ? getMajorIndices(ticks) : [];
const numMajorIndices = majorIndices.length;
const first = majorIndices[0];

View File

@ -34,12 +34,44 @@ describe('Core.scale', function() {
});
}
function getChartBigData(maxTicksLimit) {
return window.acquireChart({
type: 'line',
data: {
labels: new Array(300).fill('red'),
datasets: [{
data: new Array(300).fill(5),
}]
},
options: {
scales: {
x: {
ticks: {
autoSkip: true,
maxTicksLimit
}
}
}
}
});
}
function lastTick(chart) {
var xAxis = chart.scales.x;
var ticks = xAxis.getTicks();
return ticks[ticks.length - 1];
}
it('should use autoSkip amount of ticks when maxTicksLimit is set to a larger number as autoSkip calculation', function() {
var chart = getChartBigData(300);
expect(chart.scales.x.ticks.length).toEqual(20);
});
it('should use maxTicksLimit amount of ticks when maxTicksLimit is set to a smaller number as autoSkip calculation', function() {
var chart = getChartBigData(3);
expect(chart.scales.x.ticks.length).toEqual(3);
});
it('should display the last tick if it fits evenly with other ticks', function() {
var chart = getChart({
labels: [