]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Fix bar direction when minBarLength is applied (#9400)
authorJukka Kurkela <jukka.kurkela@gmail.com>
Tue, 13 Jul 2021 12:02:14 +0000 (15:02 +0300)
committerGitHub <noreply@github.com>
Tue, 13 Jul 2021 12:02:14 +0000 (08:02 -0400)
13 files changed:
src/controllers/controller.bar.js
test/fixtures/controller.bar/minBarLength/horizontal-neg.js
test/fixtures/controller.bar/minBarLength/horizontal-neg.png
test/fixtures/controller.bar/minBarLength/horizontal-pos.js
test/fixtures/controller.bar/minBarLength/horizontal-pos.png
test/fixtures/controller.bar/minBarLength/horizontal.js
test/fixtures/controller.bar/minBarLength/horizontal.png
test/fixtures/controller.bar/minBarLength/vertical-neg.js
test/fixtures/controller.bar/minBarLength/vertical-neg.png
test/fixtures/controller.bar/minBarLength/vertical-pos.js
test/fixtures/controller.bar/minBarLength/vertical-pos.png
test/fixtures/controller.bar/minBarLength/vertical.js
test/fixtures/controller.bar/minBarLength/vertical.png

index 9a568cdeb9914337ee3d57d189f6d75f395141fb..1ab4f2217fb8417758c76d824409639392e41913 100644 (file)
@@ -170,6 +170,13 @@ function isFloatBar(custom) {
   return custom && custom.barStart !== undefined && custom.barEnd !== undefined;
 }
 
+function barSign(size, vScale, actualBase) {
+  if (size !== 0) {
+    return sign(size);
+  }
+  return (vScale.isHorizontal() ? 1 : -1) * (vScale.min >= actualBase ? 1 : -1);
+}
+
 export default class BarController extends DatasetController {
 
   /**
@@ -426,8 +433,8 @@ export default class BarController extends DatasetController {
         */
   _calculateBarValuePixels(index) {
     const me = this;
-    const {vScale, _stacked} = me._cachedMeta;
-    const {base: baseValue, minBarLength} = me.options;
+    const {_cachedMeta: {vScale, _stacked}, options: {base: baseValue, minBarLength}} = me;
+    const actualBase = baseValue || 0;
     const parsed = me.getParsed(index);
     const custom = parsed._custom;
     const floating = isFloatBar(custom);
@@ -454,7 +461,7 @@ export default class BarController extends DatasetController {
     const startValue = !isNullOrUndef(baseValue) && !floating ? baseValue : start;
     let base = vScale.getPixelForValue(startValue);
 
-    if (this.chart.getDataVisibility(index)) {
+    if (me.chart.getDataVisibility(index)) {
       head = vScale.getPixelForValue(start + length);
     } else {
       // When not visible, no height
@@ -463,24 +470,18 @@ export default class BarController extends DatasetController {
 
     size = head - base;
 
-    if (minBarLength !== undefined && Math.abs(size) < minBarLength) {
-      size = size < 0 ? -minBarLength : minBarLength;
-      if (value === 0) {
+    if (Math.abs(size) < minBarLength) {
+      size = barSign(size, vScale, actualBase) * minBarLength;
+      if (value === actualBase) {
         base -= size / 2;
       }
       head = base + size;
     }
 
-    const actualBase = baseValue || 0;
     if (base === vScale.getPixelForValue(actualBase)) {
-      const halfGrid = vScale.getLineWidthForValue(actualBase) / 2;
-      if (size > 0) {
-        base += halfGrid;
-        size -= halfGrid;
-      } else if (size < 0) {
-        base -= halfGrid;
-        size += halfGrid;
-      }
+      const halfGrid = sign(size) * vScale.getLineWidthForValue(actualBase) / 2;
+      base += halfGrid;
+      size -= halfGrid;
     }
 
     return {
index 85e6dba68e5c2e549e8c802cd1d177b6047cc52c..c625aa64e4fbf84434f3cea8c4b921ae10181d57 100644 (file)
@@ -7,7 +7,8 @@ module.exports = {
         {
           data: [0, -0.01, -30],
           backgroundColor: '#00ff00',
-          borderWidth: 0,
+          borderColor: '#000',
+          borderWidth: 4,
           minBarLength: 20
         }
       ]
index 4b8335a436d370750d94727724b65d41332995fc..f012c82b6fb47245553eacbac130dde0164c41df 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 d12c91cf9d70412237db17e1c35d65c633717481..397e54adc165aa6d2929432757521254bae241d6 100644 (file)
@@ -7,7 +7,8 @@ module.exports = {
         {
           data: [0, 0.01, 30],
           backgroundColor: '#00ff00',
-          borderWidth: 0,
+          borderColor: '#000',
+          borderWidth: 4,
           minBarLength: 20
         }
       ]
index b19acd76753cc57c029b46993024c39cc4be440a..0f89b8676ed783cde282ee71db128883460cae18 100644 (file)
Binary files a/test/fixtures/controller.bar/minBarLength/horizontal-pos.png and b/test/fixtures/controller.bar/minBarLength/horizontal-pos.png differ
index ac320120c4cbff147deb8fe68af2e9b3e412a0ab..3c52f3cf54372e1f0a60969aef1f4acdecaf0d06 100644 (file)
@@ -7,7 +7,9 @@ module.exports = {
         {
           data: [0, -0.01, 0.01, 30, -30],
           backgroundColor: '#00ff00',
-          borderWidth: 0,
+          borderColor: '#000',
+          borderSkipped: ctx => ctx.raw === 0 ? false : 'start',
+          borderWidth: 4,
           minBarLength: 20
         }
       ]
index 39b1d3f12523224887251a8c77749777fc977fcd..11aefdb1d8d32b405dac41e42da214c86568947d 100644 (file)
Binary files a/test/fixtures/controller.bar/minBarLength/horizontal.png and b/test/fixtures/controller.bar/minBarLength/horizontal.png differ
index 92f853f426f888a5b3627fd2471ab75faed8d68f..7cee3a8a1e290e6d1653e4482a1b912559cd3db6 100644 (file)
@@ -7,7 +7,8 @@ module.exports = {
         {
           data: [0, -0.01, -30],
           backgroundColor: '#00ff00',
-          borderWidth: 0,
+          borderColor: '#000',
+          borderWidth: 4,
           minBarLength: 20
         }
       ]
index ec9636feb5f709b3ff09e4fb777de4f8a3cc2e39..4d75f1cb238c9d2679a0e8bb59392c67a4a01b19 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 cf0afa2b14b3edc14dd8c52da58db60efd6992cc..96e2756c33a050be09894e3a0d8670e92ca610c9 100644 (file)
@@ -7,7 +7,8 @@ module.exports = {
         {
           data: [0, 0.01, 30],
           backgroundColor: '#00ff00',
-          borderWidth: 0,
+          borderColor: '#000',
+          borderWidth: 4,
           minBarLength: 20
         }
       ]
index b5d4f8a2e12ee711beb6589babc643a9d0cb6a1e..5561b4f3b3de56ec49cae89a65934241c294d661 100644 (file)
Binary files a/test/fixtures/controller.bar/minBarLength/vertical-pos.png and b/test/fixtures/controller.bar/minBarLength/vertical-pos.png differ
index 4a45513c53cdd9211322c553da40c1bf7683a268..d7313be9be2c571ba969f9bfb7bb8c5de6fce0a3 100644 (file)
@@ -7,7 +7,9 @@ module.exports = {
         {
           data: [0, -0.01, 0.01, 30, -30],
           backgroundColor: '#00ff00',
-          borderWidth: 0,
+          borderColor: '#000',
+          borderSkipped: ctx => ctx.raw === 0 ? false : 'start',
+          borderWidth: 4,
           minBarLength: 20
         }
       ]
index a78cb133d00a3d09701b6bdf6aba54d808c5b718..c79c4f3f3c90fa3675a166bcae2708f5d4618a9f 100644 (file)
Binary files a/test/fixtures/controller.bar/minBarLength/vertical.png and b/test/fixtures/controller.bar/minBarLength/vertical.png differ