Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: stacked bar chart minBarLength causes overlapping #10766

Merged
merged 2 commits into from Nov 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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