]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Improve calculation for number of ticks that can fit (#6927)
authorBen McCann <322311+benmccann@users.noreply.github.com>
Tue, 7 Jan 2020 12:26:29 +0000 (04:26 -0800)
committerEvert Timberg <evert.timberg+github@gmail.com>
Tue, 7 Jan 2020 12:26:29 +0000 (07:26 -0500)
src/core/core.scale.js
src/scales/scale.time.js
test/specs/scale.time.tests.js

index 0350d3fe9e3af7140a0bb872388c2c80890fc4b1..022e3dd8e53be4b3f23913ca6ef46aa3cdc626a5 100644 (file)
@@ -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];
index ba6ad204901e75d872ba725e4bb173e676a671f1..6d28e496e5fcddb196e3185f6a4e228bacff1f75 100644 (file)
@@ -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;
        }
 }
index 465a7e0f48b3eede24c320bf5eea8c4a89f0502b..53428d9b845bf5b5638010d95a9fa6efd3b8a293 100644 (file)
@@ -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() {