Skip to content

Commit

Permalink
fix: respect minBarLength in stacked bar chart (#10766)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kit-p committed Nov 23, 2022
1 parent 36fb08c commit 667b28b
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 19 deletions.
7 changes: 6 additions & 1 deletion src/controllers/controller.bar.js
Expand Up @@ -538,7 +538,7 @@ export default class BarController extends DatasetController {
* @private
*/
_calculateBarValuePixels(index) {
const {_cachedMeta: {vScale, _stacked}, options: {base: baseValue, minBarLength}} = this;
const {_cachedMeta: {vScale, _stacked, index: datasetIndex}, options: {base: baseValue, minBarLength}} = this;
const actualBase = baseValue || 0;
const parsed = this.getParsed(index);
const custom = parsed._custom;
Expand Down Expand Up @@ -586,6 +586,11 @@ export default class BarController extends DatasetController {
const max = Math.max(startPixel, endPixel);
base = Math.max(Math.min(base, max), min);
head = base + size;

if (_stacked && !floating) {
// visual data coordinates after applying minBarLength
parsed._stacks[vScale.axis]._visualValues[datasetIndex] = vScale.getValueForPixel(head) - vScale.getValueForPixel(base);
}
}

if (base === vScale.getPixelForValue(actualBase)) {
Expand Down
8 changes: 7 additions & 1 deletion src/core/core.datasetController.js
Expand Up @@ -158,6 +158,9 @@ function updateStacks(controller, parsed) {

stack._top = getLastIndexInStack(stack, vScale, true, meta.type);
stack._bottom = getLastIndexInStack(stack, vScale, false, meta.type);

const visualValues = stack._visualValues || (stack._visualValues = {});
visualValues[datasetIndex] = value;
}
}

Expand Down Expand Up @@ -207,6 +210,9 @@ function clearStacks(meta, items) {
return;
}
delete stacks[axis][datasetIndex];
if (stacks[axis]._visualValues !== undefined && stacks[axis]._visualValues[datasetIndex] !== undefined) {
delete stacks[axis]._visualValues[datasetIndex];
}
}
}

Expand Down Expand Up @@ -578,7 +584,7 @@ export default class DatasetController {
const value = parsed[scale.axis];
const stack = {
keys: getSortedDatasetIndices(chart, true),
values: parsed._stacks[scale.axis]
values: parsed._stacks[scale.axis]._visualValues
};
return applyStack(stack, value, meta.index, {mode});
}
Expand Down
@@ -0,0 +1,55 @@
const minBarLength = 50;

module.exports = {
config: {
type: 'bar',
data: {
labels: [1, 2, 3, 4],
datasets: [
{
data: [1, -1, 1, 20],
backgroundColor: '#bb000066',
minBarLength
},
{
data: [1, -1, -1, -20],
backgroundColor: '#00bb0066',
minBarLength
},
{
data: [1, -1, 1, 40],
backgroundColor: '#0000bb66',
minBarLength
},
{
data: [1, -1, -1, -40],
backgroundColor: '#00000066',
minBarLength
}
]
},
options: {
indexAxis: 'y',
scales: {
x: {
display: false,
stacked: true
},
y: {
type: 'linear',
position: 'left',
stacked: true,
ticks: {
display: false
}
}
}
}
},
options: {
canvas: {
height: 512,
width: 512
}
}
};
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@@ -0,0 +1,54 @@
const minBarLength = 50;

module.exports = {
config: {
type: 'bar',
data: {
labels: [1, 2, 3, 4],
datasets: [
{
data: [1, -1, 1, 20],
backgroundColor: '#bb000066',
minBarLength
},
{
data: [1, -1, -1, -20],
backgroundColor: '#00bb0066',
minBarLength
},
{
data: [1, -1, 1, 40],
backgroundColor: '#0000bb66',
minBarLength
},
{
data: [1, -1, -1, -40],
backgroundColor: '#00000066',
minBarLength
}
]
},
options: {
scales: {
x: {
display: false,
stacked: true
},
y: {
type: 'linear',
position: 'left',
stacked: true,
ticks: {
display: false
}
}
}
}
},
options: {
canvas: {
height: 512,
width: 512
}
}
};
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
34 changes: 17 additions & 17 deletions test/specs/core.datasetController.tests.js
Expand Up @@ -768,12 +768,12 @@ describe('Chart.DatasetController', function() {

expect(chart._stacks).toEqual({
'x.y.1': {
0: {0: 1, 2: 3, _top: 2, _bottom: null},
1: {0: 10, 2: 30, _top: 2, _bottom: null}
0: {0: 1, 2: 3, _top: 2, _bottom: null, _visualValues: {0: 1, 2: 3}},
1: {0: 10, 2: 30, _top: 2, _bottom: null, _visualValues: {0: 10, 2: 30}}
},
'x.y.2': {
0: {1: 2, _top: 1, _bottom: null},
1: {1: 20, _top: 1, _bottom: null}
0: {1: 2, _top: 1, _bottom: null, _visualValues: {1: 2}},
1: {1: 20, _top: 1, _bottom: null, _visualValues: {1: 20}}
}
});

Expand All @@ -782,12 +782,12 @@ describe('Chart.DatasetController', function() {

expect(chart._stacks).toEqual({
'x.y.1': {
0: {0: 1, _top: 2, _bottom: null},
1: {0: 10, _top: 2, _bottom: null}
0: {0: 1, _top: 2, _bottom: null, _visualValues: {0: 1}},
1: {0: 10, _top: 2, _bottom: null, _visualValues: {0: 10}}
},
'x.y.2': {
0: {1: 2, 2: 3, _top: 2, _bottom: null},
1: {1: 20, 2: 30, _top: 2, _bottom: null}
0: {1: 2, 2: 3, _top: 2, _bottom: null, _visualValues: {1: 2, 2: 3}},
1: {1: 20, 2: 30, _top: 2, _bottom: null, _visualValues: {1: 20, 2: 30}}
}
});
});
Expand All @@ -812,12 +812,12 @@ describe('Chart.DatasetController', function() {

expect(chart._stacks).toEqual({
'x.y.1': {
0: {0: 1, 2: 3, _top: 2, _bottom: null},
1: {0: 10, 2: 30, _top: 2, _bottom: null}
0: {0: 1, 2: 3, _top: 2, _bottom: null, _visualValues: {0: 1, 2: 3}},
1: {0: 10, 2: 30, _top: 2, _bottom: null, _visualValues: {0: 10, 2: 30}}
},
'x.y.2': {
0: {1: 2, _top: 1, _bottom: null},
1: {1: 20, _top: 1, _bottom: null}
0: {1: 2, _top: 1, _bottom: null, _visualValues: {1: 2}},
1: {1: 20, _top: 1, _bottom: null, _visualValues: {1: 20}}
}
});

Expand All @@ -826,12 +826,12 @@ describe('Chart.DatasetController', function() {

expect(chart._stacks).toEqual({
'x.y.1': {
0: {0: 1, 2: 4, _top: 2, _bottom: null},
1: {0: 10, _top: 2, _bottom: null}
0: {0: 1, 2: 4, _top: 2, _bottom: null, _visualValues: {0: 1, 2: 4}},
1: {0: 10, _top: 2, _bottom: null, _visualValues: {0: 10}}
},
'x.y.2': {
0: {1: 2, _top: 1, _bottom: null},
1: {1: 20, _top: 1, _bottom: null}
0: {1: 2, _top: 1, _bottom: null, _visualValues: {1: 2}},
1: {1: 20, _top: 1, _bottom: null, _visualValues: {1: 20}}
}
});
});
Expand Down Expand Up @@ -947,7 +947,7 @@ describe('Chart.DatasetController', function() {
});

var meta = chart.getDatasetMeta(0);
expect(meta._parsed[0]._stacks).toEqual(jasmine.objectContaining({y: {0: 10, 1: 20, _top: 1, _bottom: null}}));
expect(meta._parsed[0]._stacks).toEqual(jasmine.objectContaining({y: {0: 10, 1: 20, _top: 1, _bottom: null, _visualValues: {0: 10, 1: 20}}}));
});

describe('resolveDataElementOptions', function() {
Expand Down

0 comments on commit 667b28b

Please sign in to comment.