From: Ben McCann <322311+benmccann@users.noreply.github.com> Date: Tue, 7 Jan 2020 12:26:29 +0000 (-0800) Subject: Improve calculation for number of ticks that can fit (#6927) X-Git-Tag: v3.0.0-alpha~149 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3b55de73facd9a46cfb70bf4968c732c90cc1bb1;p=thirdparty%2FChart.js.git Improve calculation for number of ticks that can fit (#6927) --- diff --git a/src/core/core.scale.js b/src/core/core.scale.js index 0350d3fe9..022e3dd8e 100644 --- a/src/core/core.scale.js +++ b/src/core/core.scale.js @@ -884,7 +884,7 @@ class Scale extends Element { const me = this; const tickOpts = me.options.ticks; const axisLength = me._length; - const ticksLimit = tickOpts.maxTicksLimit || axisLength / me._tickSize() + 1; + const ticksLimit = tickOpts.maxTicksLimit || axisLength / me._tickSize(); const majorIndices = tickOpts.major.enabled ? getMajorIndices(ticks) : []; const numMajorIndices = majorIndices.length; const first = majorIndices[0]; diff --git a/src/scales/scale.time.js b/src/scales/scale.time.js index ba6ad2049..6d28e496e 100644 --- a/src/scales/scale.time.js +++ b/src/scales/scale.time.js @@ -755,12 +755,9 @@ class TimeScale extends Scale { const format = displayFormats[timeOpts.unit] || displayFormats.millisecond; const exampleLabel = me._tickFormatFunction(exampleTime, 0, ticksFromTimestamps(me, [exampleTime], me._majorUnit), format); const size = me._getLabelSize(exampleLabel); - let capacity = Math.floor(me.isHorizontal() ? me.width / size.w : me.height / size.h); - - if (me.options.offset) { - capacity--; - } - + // subtract 1 - if offset then there's one less label than tick + // if not offset then one half label padding is added to each end leaving room for one less label + const capacity = Math.floor(me.isHorizontal() ? me.width / size.w : me.height / size.h) - 1; return capacity > 0 ? capacity : 1; } } diff --git a/test/specs/scale.time.tests.js b/test/specs/scale.time.tests.js index 465a7e0f4..53428d9b8 100644 --- a/test/specs/scale.time.tests.js +++ b/test/specs/scale.time.tests.js @@ -1397,8 +1397,8 @@ describe('Time scale tests', function() { this.chart = window.acquireChart({ type: 'line', data: { - labels: ['2017', '2019', '2020', '2025', '2042'], - datasets: [{data: [0, 1, 2, 3, 4, 5]}] + labels: ['2017', '2019', '2020', '2025'], + datasets: [{data: [0, 1, 2, 3, 4]}] }, options: { scales: { @@ -1421,7 +1421,7 @@ describe('Time scale tests', function() { var scale = this.chart.scales.x; expect(scale.getPixelForValue(moment('2017').valueOf())).toBeCloseToPixel(scale.left); - expect(scale.getPixelForValue(moment('2042').valueOf())).toBeCloseToPixel(scale.left + scale.width); + expect(scale.getPixelForValue(moment('2025').valueOf())).toBeCloseToPixel(scale.left + scale.width); }); it ('should add offset from the edges if offset is true', function() { @@ -1437,7 +1437,7 @@ describe('Time scale tests', function() { var lastTickInterval = scale.getPixelForTick(numTicks - 1) - scale.getPixelForTick(numTicks - 2); expect(scale.getPixelForValue(moment('2017').valueOf())).toBeCloseToPixel(scale.left + firstTickInterval / 2); - expect(scale.getPixelForValue(moment('2042').valueOf())).toBeCloseToPixel(scale.left + scale.width - lastTickInterval / 2); + expect(scale.getPixelForValue(moment('2025').valueOf())).toBeCloseToPixel(scale.left + scale.width - lastTickInterval / 2); }); it ('should not add offset if min and max extend the labels range', function() {