]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
minimum bar length setting keeps bar base in view (#10157)
authorEvert Timberg <evert.timberg@gmail.com>
Sun, 13 Feb 2022 20:27:46 +0000 (15:27 -0500)
committerGitHub <noreply@github.com>
Sun, 13 Feb 2022 20:27:46 +0000 (15:27 -0500)
src/controllers/controller.bar.js
test/fixtures/controller.bar/minBarLength/horizontal-neg.png
test/fixtures/controller.bar/minBarLength/horizontal-pos.png
test/fixtures/controller.bar/minBarLength/horizontal-stacked.js [new file with mode: 0644]
test/fixtures/controller.bar/minBarLength/horizontal-stacked.png [new file with mode: 0644]
test/fixtures/controller.bar/minBarLength/vertical-neg.png
test/fixtures/controller.bar/minBarLength/vertical-pos.png
test/fixtures/controller.bar/minBarLength/vertical-stacked.js [new file with mode: 0644]
test/fixtures/controller.bar/minBarLength/vertical-stacked.png [new file with mode: 0644]

index 2d7629a308e6d7d33475f87bad7b96e67c73e758..e2b45bde3fd9dd0c29f23713ae818040ab502fc6 100644 (file)
@@ -543,6 +543,11 @@ export default class BarController extends DatasetController {
       if (value === actualBase) {
         base -= size / 2;
       }
+      const startPixel = vScale.getPixelForDecimal(0);
+      const endPixel = vScale.getPixelForDecimal(1);
+      const min = Math.min(startPixel, endPixel);
+      const max = Math.max(startPixel, endPixel);
+      base = Math.max(Math.min(base, max), min);
       head = base + size;
     }
 
index f012c82b6fb47245553eacbac130dde0164c41df..16c6cabd522e65a235ed6a4a87476caa432d8609 100644 (file)
Binary files a/test/fixtures/controller.bar/minBarLength/horizontal-neg.png and b/test/fixtures/controller.bar/minBarLength/horizontal-neg.png differ
index 0f89b8676ed783cde282ee71db128883460cae18..8d8b4724e9f469a43dbfb56deea6c4d73c8703f6 100644 (file)
Binary files a/test/fixtures/controller.bar/minBarLength/horizontal-pos.png and b/test/fixtures/controller.bar/minBarLength/horizontal-pos.png differ
diff --git a/test/fixtures/controller.bar/minBarLength/horizontal-stacked.js b/test/fixtures/controller.bar/minBarLength/horizontal-stacked.js
new file mode 100644 (file)
index 0000000..8452ed6
--- /dev/null
@@ -0,0 +1,43 @@
+module.exports = {
+  config: {
+    type: 'bar',
+    data: {
+      labels: [0, 1, 2, 3, 4],
+      datasets: [{
+        data: [0, 0.01, 30],
+        backgroundColor: '#00ff00',
+        borderColor: '#000',
+        borderWidth: 4,
+        minBarLength: 20,
+        xAxisID: 'x2',
+      }]
+    },
+    options: {
+      indexAxis: 'y',
+      scales: {
+        x: {
+          stack: 'demo',
+          ticks: {
+            display: false
+          }
+        },
+        x2: {
+          type: 'linear',
+          position: 'bottom',
+          stack: 'demo',
+          stackWeight: 1,
+          ticks: {
+            display: false
+          }
+        },
+        y: {display: false},
+      }
+    }
+  },
+  options: {
+    canvas: {
+      height: 512,
+      width: 512
+    }
+  }
+};
diff --git a/test/fixtures/controller.bar/minBarLength/horizontal-stacked.png b/test/fixtures/controller.bar/minBarLength/horizontal-stacked.png
new file mode 100644 (file)
index 0000000..87da748
Binary files /dev/null and b/test/fixtures/controller.bar/minBarLength/horizontal-stacked.png differ
index 4d75f1cb238c9d2679a0e8bb59392c67a4a01b19..debb97ee7d197d81fe378eaabff2e02a9af99e5c 100644 (file)
Binary files a/test/fixtures/controller.bar/minBarLength/vertical-neg.png and b/test/fixtures/controller.bar/minBarLength/vertical-neg.png differ
index 5561b4f3b3de56ec49cae89a65934241c294d661..9a7b49e9cd92564ec56204a8a5872e3549976280 100644 (file)
Binary files a/test/fixtures/controller.bar/minBarLength/vertical-pos.png and b/test/fixtures/controller.bar/minBarLength/vertical-pos.png differ
diff --git a/test/fixtures/controller.bar/minBarLength/vertical-stacked.js b/test/fixtures/controller.bar/minBarLength/vertical-stacked.js
new file mode 100644 (file)
index 0000000..e09f12c
--- /dev/null
@@ -0,0 +1,42 @@
+module.exports = {
+  config: {
+    type: 'bar',
+    data: {
+      labels: [0, 1, 2, 3, 4],
+      datasets: [{
+        data: [0, 0.01, 30],
+        backgroundColor: '#00ff00',
+        borderColor: '#000',
+        borderWidth: 4,
+        minBarLength: 20,
+        yAxisID: 'y2',
+      }]
+    },
+    options: {
+      scales: {
+        x: {display: false},
+        y: {
+          stack: 'demo',
+          ticks: {
+            display: false
+          }
+        },
+        y2: {
+          type: 'linear',
+          position: 'left',
+          stack: 'demo',
+          stackWeight: 1,
+          ticks: {
+            display: false
+          }
+        }
+      }
+    }
+  },
+  options: {
+    canvas: {
+      height: 512,
+      width: 512
+    }
+  }
+};
diff --git a/test/fixtures/controller.bar/minBarLength/vertical-stacked.png b/test/fixtures/controller.bar/minBarLength/vertical-stacked.png
new file mode 100644 (file)
index 0000000..ecef74b
Binary files /dev/null and b/test/fixtures/controller.bar/minBarLength/vertical-stacked.png differ