Skip to content

Commit

Permalink
Fix bar direction when minBarLength is applied (#9400)
Browse files Browse the repository at this point in the history
  • Loading branch information
kurkle committed Jul 13, 2021
1 parent 31be610 commit c3e709e
Show file tree
Hide file tree
Showing 13 changed files with 30 additions and 21 deletions.
31 changes: 16 additions & 15 deletions src/controllers/controller.bar.js
Expand Up @@ -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 {

/**
Expand Down Expand Up @@ -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);
Expand All @@ -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
Expand All @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion test/fixtures/controller.bar/minBarLength/horizontal-neg.js
Expand Up @@ -7,7 +7,8 @@ module.exports = {
{
data: [0, -0.01, -30],
backgroundColor: '#00ff00',
borderWidth: 0,
borderColor: '#000',
borderWidth: 4,
minBarLength: 20
}
]
Expand Down
Binary file modified test/fixtures/controller.bar/minBarLength/horizontal-neg.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 2 additions & 1 deletion test/fixtures/controller.bar/minBarLength/horizontal-pos.js
Expand Up @@ -7,7 +7,8 @@ module.exports = {
{
data: [0, 0.01, 30],
backgroundColor: '#00ff00',
borderWidth: 0,
borderColor: '#000',
borderWidth: 4,
minBarLength: 20
}
]
Expand Down
Binary file modified test/fixtures/controller.bar/minBarLength/horizontal-pos.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 3 additions & 1 deletion test/fixtures/controller.bar/minBarLength/horizontal.js
Expand Up @@ -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
}
]
Expand Down
Binary file modified test/fixtures/controller.bar/minBarLength/horizontal.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 2 additions & 1 deletion test/fixtures/controller.bar/minBarLength/vertical-neg.js
Expand Up @@ -7,7 +7,8 @@ module.exports = {
{
data: [0, -0.01, -30],
backgroundColor: '#00ff00',
borderWidth: 0,
borderColor: '#000',
borderWidth: 4,
minBarLength: 20
}
]
Expand Down
Binary file modified test/fixtures/controller.bar/minBarLength/vertical-neg.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 2 additions & 1 deletion test/fixtures/controller.bar/minBarLength/vertical-pos.js
Expand Up @@ -7,7 +7,8 @@ module.exports = {
{
data: [0, 0.01, 30],
backgroundColor: '#00ff00',
borderWidth: 0,
borderColor: '#000',
borderWidth: 4,
minBarLength: 20
}
]
Expand Down
Binary file modified test/fixtures/controller.bar/minBarLength/vertical-pos.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 3 additions & 1 deletion test/fixtures/controller.bar/minBarLength/vertical.js
Expand Up @@ -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
}
]
Expand Down
Binary file modified test/fixtures/controller.bar/minBarLength/vertical.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit c3e709e

Please sign in to comment.