]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Fix autoskip logic (#10663)
authorJacco van den Berg <jaccoberg2281@gmail.com>
Tue, 13 Sep 2022 17:33:22 +0000 (19:33 +0200)
committerGitHub <noreply@github.com>
Tue, 13 Sep 2022 17:33:22 +0000 (19:33 +0200)
* fix autoskip logic

* add test

* fix lint erro

* Update variable name

docs/migration/v4-migration.md
src/core/core.scale.autoskip.js
test/specs/core.scale.tests.js

index d065fee04f77122fa151e7a8364ac36217615d32..cce70bc4af26ddad78f37f831a06170ffeec7b00 100644 (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>`.
index 89d0ddfac0a0fb39d9a01c485b8316ecef85e749..1fc7283d652c221871910eaf3815f3730891f23b 100644 (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];
index e5e94926d563ac2f869f5e9801bdcbda215449fa..3b30e5600ea955cb47d7433d77a40e8b784b5b2e 100644 (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: [