diff --git a/src/controllers/controller.bar.js b/src/controllers/controller.bar.js index 9a568cdeb99..1ab4f2217fb 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 85e6dba68e5..c625aa64e4f 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 4b8335a436d..f012c82b6fb 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 d12c91cf9d7..397e54adc16 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 b19acd76753..0f89b8676ed 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 ac320120c4c..3c52f3cf543 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 39b1d3f1252..11aefdb1d8d 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 92f853f426f..7cee3a8a1e2 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 ec9636feb5f..4d75f1cb238 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 cf0afa2b14b..96e2756c33a 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 b5d4f8a2e12..5561b4f3b3d 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 4a45513c53c..d7313be9be2 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 a78cb133d00..c79c4f3f3c9 100644 Binary files a/test/fixtures/controller.bar/minBarLength/vertical.png and b/test/fixtures/controller.bar/minBarLength/vertical.png differ