From cc65c2bac28c8779b7d96f69470347fbfd6e2e83 Mon Sep 17 00:00:00 2001 From: Jacco van den Berg Date: Tue, 13 Sep 2022 19:33:22 +0200 Subject: [PATCH] Fix autoskip logic (#10663) * fix autoskip logic * add test * fix lint erro * Update variable name --- docs/migration/v4-migration.md | 1 + src/core/core.scale.autoskip.js | 3 ++- test/specs/core.scale.tests.js | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/docs/migration/v4-migration.md b/docs/migration/v4-migration.md index d065fee04..cce70bc4a 100644 --- a/docs/migration/v4-migration.md +++ b/docs/migration/v4-migration.md @@ -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 `` to ``. diff --git a/src/core/core.scale.autoskip.js b/src/core/core.scale.autoskip.js index 89d0ddfac..1fc7283d6 100644 --- a/src/core/core.scale.autoskip.js +++ b/src/core/core.scale.autoskip.js @@ -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]; diff --git a/test/specs/core.scale.tests.js b/test/specs/core.scale.tests.js index e5e94926d..3b30e5600 100644 --- a/test/specs/core.scale.tests.js +++ b/test/specs/core.scale.tests.js @@ -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: [ -- 2.47.3