]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Fix: tick spacing when min=0 | niceMin or max=0 (#8811)
authorJukka Kurkela <jukka.kurkela@gmail.com>
Sun, 4 Apr 2021 11:03:48 +0000 (14:03 +0300)
committerJukka Kurkela <jukka.kurkela@gmail.com>
Sun, 4 Apr 2021 11:50:24 +0000 (14:50 +0300)
src/scales/scale.linearbase.js
test/fixtures/scale.linear/issue-8806.js [new file with mode: 0644]
test/fixtures/scale.linear/issue-8806.png [new file with mode: 0644]

index a215c7571c6dd11cdf60f69a31c180a51e24fce9..3a671d269cdcf9bd92270c06ceadd45b62c49adc 100644 (file)
@@ -98,12 +98,12 @@ function generateTicks(generationOptions, dataRange) {
   let j = 0;
   if (minDefined) {
     ticks.push({value: min});
-    // If the niceMin is smaller than min, skip it
-    if (niceMin < min) {
+    // If the niceMin is smaller or equal to 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)) {
+    if (almostEquals(Math.round((niceMin + j * spacing) * factor) / factor, min, spacing / 10)) {
       j++;
     }
   }
@@ -114,7 +114,7 @@ function generateTicks(generationOptions, dataRange) {
 
   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)) {
+    if (almostEquals(ticks[ticks.length - 1].value, max, spacing / 10)) {
       ticks[ticks.length - 1].value = max;
     } else {
       ticks.push({value: max});
diff --git a/test/fixtures/scale.linear/issue-8806.js b/test/fixtures/scale.linear/issue-8806.js
new file mode 100644 (file)
index 0000000..ec53498
--- /dev/null
@@ -0,0 +1,26 @@
+module.exports = {
+  description: 'https://github.com/chartjs/Chart.js/issues/8806',
+  config: {
+    type: 'bar',
+    data: {
+      labels: ['0', '1', '2', '3', '4', '5', '6'],
+      datasets: [{
+        label: '# of Votes',
+        data: [32, 46, 28, 21, 20, 13, 27]
+      }]
+    },
+    options: {
+      scales: {
+        x: {display: false},
+        y: {ticks: {maxTicksLimit: 4}, min: 0}
+      }
+    }
+  },
+  options: {
+    spriteText: true,
+    canvas: {
+      width: 256,
+      height: 256
+    }
+  }
+};
diff --git a/test/fixtures/scale.linear/issue-8806.png b/test/fixtures/scale.linear/issue-8806.png
new file mode 100644 (file)
index 0000000..9db9d33
Binary files /dev/null and b/test/fixtures/scale.linear/issue-8806.png differ