]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Show correct decimal places when using count but `min` is not an integer (#9122)
authorEvert Timberg <evert.timberg+github@gmail.com>
Wed, 19 May 2021 22:31:15 +0000 (18:31 -0400)
committerGitHub <noreply@github.com>
Wed, 19 May 2021 22:31:15 +0000 (18:31 -0400)
* Show correct decimal places when using count but `min` is not an integer
* Fix lint issues

src/core/core.ticks.js
src/scales/scale.linearbase.js
test/fixtures/scale.linear/tick-count-min-max-not-int.js [new file with mode: 0644]
test/fixtures/scale.linear/tick-count-min-max-not-int.png [new file with mode: 0644]

index 186be7c0f07e84b8f9137563e65bcd0e0ec9a34f..f054ef66a2057ace1759e50fe5999db079b3f1ea 100644 (file)
@@ -82,7 +82,7 @@ function calculateDelta(tickValue, ticks) {
   let delta = ticks.length > 3 ? ticks[2].value - ticks[1].value : ticks[1].value - ticks[0].value;
 
   // If we have a number like 2.5 as the delta, figure out how many decimal places we need
-  if (Math.abs(delta) > 1 && tickValue !== Math.floor(tickValue)) {
+  if (Math.abs(delta) >= 1 && tickValue !== Math.floor(tickValue)) {
     // not an integer
     delta = tickValue - Math.floor(tickValue);
   }
index f8248668ea7bf940bb028bca01423e3f4aaa31a1..cb299cf43571fcba85f474b55117fff88b3a08e7 100644 (file)
@@ -91,7 +91,11 @@ function generateTicks(generationOptions, dataRange) {
 
   // The spacing will have changed in cases 1, 2, and 3 so the factor cannot be computed
   // until this point
-  factor = Math.pow(10, isNullOrUndef(precision) ? _decimalPlaces(spacing) : precision);
+  const decimalPlaces = Math.max(
+    _decimalPlaces(spacing),
+    _decimalPlaces(niceMin),
+  );
+  factor = Math.pow(10, isNullOrUndef(precision) ? decimalPlaces : precision);
   niceMin = Math.round(niceMin * factor) / factor;
   niceMax = Math.round(niceMax * factor) / factor;
 
diff --git a/test/fixtures/scale.linear/tick-count-min-max-not-int.js b/test/fixtures/scale.linear/tick-count-min-max-not-int.js
new file mode 100644 (file)
index 0000000..a9f6e02
--- /dev/null
@@ -0,0 +1,38 @@
+module.exports = {
+  description: 'https://github.com/chartjs/Chart.js/issues/9078',
+  config: {
+    type: 'bar',
+    data: {
+      datasets: [{
+        data: [
+          {x: 1, y: 3.5},
+          {x: 2, y: 4.7},
+          {x: 3, y: 7.3},
+          {x: 4, y: 6.7}
+        ]
+      }]
+    },
+    options: {
+      scales: {
+        x: {
+          type: 'linear',
+          display: false,
+        },
+        y: {
+          min: 3.5,
+          max: 8.5,
+          ticks: {
+            count: 6,
+          }
+        }
+      }
+    }
+  },
+  options: {
+    spriteText: true,
+    canvas: {
+      width: 256,
+      height: 256
+    }
+  }
+};
diff --git a/test/fixtures/scale.linear/tick-count-min-max-not-int.png b/test/fixtures/scale.linear/tick-count-min-max-not-int.png
new file mode 100644 (file)
index 0000000..0b94172
Binary files /dev/null and b/test/fixtures/scale.linear/tick-count-min-max-not-int.png differ