From: Jukka Kurkela Date: Tue, 13 Jul 2021 12:02:14 +0000 (+0300) Subject: Fix bar direction when minBarLength is applied (#9400) X-Git-Tag: v3.5.0~23 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c3e709e90e1a78c639a8d403e8c703077ccf5cb4;p=thirdparty%2FChart.js.git Fix bar direction when minBarLength is applied (#9400) --- diff --git a/src/controllers/controller.bar.js b/src/controllers/controller.bar.js index 9a568cdeb..1ab4f2217 100644 --- a/src/controllers/controller.bar.js +++ b/src/controllers/controller.bar.js @@ -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 { diff --git a/test/fixtures/controller.bar/minBarLength/horizontal-neg.js b/test/fixtures/controller.bar/minBarLength/horizontal-neg.js index 85e6dba68..c625aa64e 100644 --- a/test/fixtures/controller.bar/minBarLength/horizontal-neg.js +++ b/test/fixtures/controller.bar/minBarLength/horizontal-neg.js @@ -7,7 +7,8 @@ module.exports = { { data: [0, -0.01, -30], backgroundColor: '#00ff00', - borderWidth: 0, + borderColor: '#000', + borderWidth: 4, minBarLength: 20 } ] diff --git a/test/fixtures/controller.bar/minBarLength/horizontal-neg.png b/test/fixtures/controller.bar/minBarLength/horizontal-neg.png index 4b8335a43..f012c82b6 100644 Binary files a/test/fixtures/controller.bar/minBarLength/horizontal-neg.png and b/test/fixtures/controller.bar/minBarLength/horizontal-neg.png differ diff --git a/test/fixtures/controller.bar/minBarLength/horizontal-pos.js b/test/fixtures/controller.bar/minBarLength/horizontal-pos.js index d12c91cf9..397e54adc 100644 --- a/test/fixtures/controller.bar/minBarLength/horizontal-pos.js +++ b/test/fixtures/controller.bar/minBarLength/horizontal-pos.js @@ -7,7 +7,8 @@ module.exports = { { data: [0, 0.01, 30], backgroundColor: '#00ff00', - borderWidth: 0, + borderColor: '#000', + borderWidth: 4, minBarLength: 20 } ] diff --git a/test/fixtures/controller.bar/minBarLength/horizontal-pos.png b/test/fixtures/controller.bar/minBarLength/horizontal-pos.png index b19acd767..0f89b8676 100644 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.js b/test/fixtures/controller.bar/minBarLength/horizontal.js index ac320120c..3c52f3cf5 100644 --- a/test/fixtures/controller.bar/minBarLength/horizontal.js +++ b/test/fixtures/controller.bar/minBarLength/horizontal.js @@ -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 } ] diff --git a/test/fixtures/controller.bar/minBarLength/horizontal.png b/test/fixtures/controller.bar/minBarLength/horizontal.png index 39b1d3f12..11aefdb1d 100644 Binary files a/test/fixtures/controller.bar/minBarLength/horizontal.png and b/test/fixtures/controller.bar/minBarLength/horizontal.png differ diff --git a/test/fixtures/controller.bar/minBarLength/vertical-neg.js b/test/fixtures/controller.bar/minBarLength/vertical-neg.js index 92f853f42..7cee3a8a1 100644 --- a/test/fixtures/controller.bar/minBarLength/vertical-neg.js +++ b/test/fixtures/controller.bar/minBarLength/vertical-neg.js @@ -7,7 +7,8 @@ module.exports = { { data: [0, -0.01, -30], backgroundColor: '#00ff00', - borderWidth: 0, + borderColor: '#000', + borderWidth: 4, minBarLength: 20 } ] diff --git a/test/fixtures/controller.bar/minBarLength/vertical-neg.png b/test/fixtures/controller.bar/minBarLength/vertical-neg.png index ec9636feb..4d75f1cb2 100644 Binary files a/test/fixtures/controller.bar/minBarLength/vertical-neg.png and b/test/fixtures/controller.bar/minBarLength/vertical-neg.png differ diff --git a/test/fixtures/controller.bar/minBarLength/vertical-pos.js b/test/fixtures/controller.bar/minBarLength/vertical-pos.js index cf0afa2b1..96e2756c3 100644 --- a/test/fixtures/controller.bar/minBarLength/vertical-pos.js +++ b/test/fixtures/controller.bar/minBarLength/vertical-pos.js @@ -7,7 +7,8 @@ module.exports = { { data: [0, 0.01, 30], backgroundColor: '#00ff00', - borderWidth: 0, + borderColor: '#000', + borderWidth: 4, minBarLength: 20 } ] diff --git a/test/fixtures/controller.bar/minBarLength/vertical-pos.png b/test/fixtures/controller.bar/minBarLength/vertical-pos.png index b5d4f8a2e..5561b4f3b 100644 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.js b/test/fixtures/controller.bar/minBarLength/vertical.js index 4a45513c5..d7313be9b 100644 --- a/test/fixtures/controller.bar/minBarLength/vertical.js +++ b/test/fixtures/controller.bar/minBarLength/vertical.js @@ -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 } ] diff --git a/test/fixtures/controller.bar/minBarLength/vertical.png b/test/fixtures/controller.bar/minBarLength/vertical.png index a78cb133d..c79c4f3f3 100644 Binary files a/test/fixtures/controller.bar/minBarLength/vertical.png and b/test/fixtures/controller.bar/minBarLength/vertical.png differ