]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Linear: Skip ticks that would overlap with min/max (#8569)
authorJukka Kurkela <jukka.kurkela@gmail.com>
Fri, 5 Mar 2021 00:37:46 +0000 (02:37 +0200)
committerGitHub <noreply@github.com>
Fri, 5 Mar 2021 00:37:46 +0000 (19:37 -0500)
src/scales/scale.linearbase.js
test/fixtures/scale.linear/min-max-skip.js [new file with mode: 0644]
test/fixtures/scale.linear/min-max-skip.png [new file with mode: 0644]
test/specs/scale.radialLinear.tests.js

index fe439156850655ae2836cd2442d81a9df00e23ee..6d8f1e1a31cd46ce64a6824db729205e0ef3f06c 100644 (file)
@@ -42,12 +42,14 @@ function generateTicks(generationOptions, dataRange) {
   const unit = stepSize || 1;
   const maxNumSpaces = generationOptions.maxTicks - 1;
   const {min: rmin, max: rmax} = dataRange;
+  const minDefined = !isNullOrUndef(min);
+  const maxDefined = !isNullOrUndef(max);
   let spacing = niceNum((rmax - rmin) / maxNumSpaces / unit) * unit;
   let factor, niceMin, niceMax, numSpaces;
 
   // Beyond MIN_SPACING floating point numbers being to lose precision
   // such that we can't do the math necessary to generate ticks
-  if (spacing < MIN_SPACING && isNullOrUndef(min) && isNullOrUndef(max)) {
+  if (spacing < MIN_SPACING && !minDefined && !maxDefined) {
     return [{value: rmin}, {value: rmax}];
   }
 
@@ -70,7 +72,7 @@ function generateTicks(generationOptions, dataRange) {
   niceMax = Math.ceil(rmax / spacing) * spacing;
 
   // If min, max and stepSize is set and they make an evenly spaced scale use it.
-  if (stepSize && !isNullOrUndef(min) && !isNullOrUndef(max)) {
+  if (stepSize && minDefined && maxDefined) {
     // If very close to our whole number, use it.
     if (almostWhole((max - min) / stepSize, spacing / 1000)) {
       niceMin = min;
@@ -88,11 +90,34 @@ function generateTicks(generationOptions, dataRange) {
 
   niceMin = Math.round(niceMin * factor) / factor;
   niceMax = Math.round(niceMax * factor) / factor;
-  ticks.push({value: isNullOrUndef(min) ? niceMin : min});
-  for (let j = 1; j < numSpaces; ++j) {
+
+  let j = 0;
+  if (minDefined) {
+    ticks.push({value: min});
+    // If the niceMin is smaller than min, skip it
+    if (niceMin < min) {
+      j++;
+    }
+    // If the next nice tick is close to min, skip that too
+    if (almostWhole(Math.round((niceMin + j * spacing) * factor) / factor / min, spacing / 1000)) {
+      j++;
+    }
+  }
+
+  for (; j < numSpaces; ++j) {
     ticks.push({value: Math.round((niceMin + j * spacing) * factor) / factor});
   }
-  ticks.push({value: isNullOrUndef(max) ? niceMax : max});
+
+  if (maxDefined) {
+    // If the previous tick is close to max, replace it with max, else add max
+    if (almostWhole(ticks[ticks.length - 1].value / max, spacing / 1000)) {
+      ticks[ticks.length - 1].value = max;
+    } else {
+      ticks.push({value: max});
+    }
+  } else {
+    ticks.push({value: niceMax});
+  }
 
   return ticks;
 }
diff --git a/test/fixtures/scale.linear/min-max-skip.js b/test/fixtures/scale.linear/min-max-skip.js
new file mode 100644 (file)
index 0000000..03fee6f
--- /dev/null
@@ -0,0 +1,20 @@
+module.exports = {
+  description: 'https://github.com/chartjs/Chart.js/issues/7734',
+  config: {
+    type: 'line',
+    options: {
+      scales: {
+        y: {
+          max: 1225.2,
+          min: 369.5,
+        },
+        x: {
+          display: false
+        }
+      }
+    }
+  },
+  options: {
+    spriteText: true
+  }
+};
diff --git a/test/fixtures/scale.linear/min-max-skip.png b/test/fixtures/scale.linear/min-max-skip.png
new file mode 100644 (file)
index 0000000..9141f8d
Binary files /dev/null and b/test/fixtures/scale.linear/min-max-skip.png differ
index 745e95d1a87818bfea85f7501fcb3dddc0569c90..e70fb9494972df90e405a11e30cf0b497fed515b 100644 (file)
@@ -200,7 +200,7 @@ describe('Test the radial linear scale', function() {
 
     expect(chart.scales.r.min).toBe(-1010);
     expect(chart.scales.r.max).toBe(1010);
-    expect(getLabels(chart.scales.r)).toEqual(['-1,010', '-1,000', '-500', '0', '500', '1,000', '1,010']);
+    expect(getLabels(chart.scales.r)).toEqual(['-1,010', '-500', '0', '500', '1,010']);
   });
 
   it('should forcibly include 0 in the range if the beginAtZero option is used', function() {