Fix stacking bug when a dataset is removed (#8520)

This commit is contained in:
Jukka Kurkela 2021-02-25 00:31:17 +02:00 committed by GitHub
parent b06cd36697
commit 8796a1ba1d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 108 additions and 2 deletions

View File

@ -107,6 +107,7 @@ class Chart {
this._options = options;
this._layers = [];
this._metasets = [];
this._stacks = undefined;
this.boxes = [];
this.currentDevicePixelRatio = undefined;
this.chartArea = undefined;
@ -370,8 +371,11 @@ class Chart {
*/
_removeUnreferencedMetasets() {
const me = this;
const datasets = me.data.datasets;
me._metasets.forEach((meta, index) => {
const {_metasets: metasets, data: {datasets}} = me;
if (metasets.length > datasets.length) {
delete me._stacks;
}
metasets.forEach((meta, index) => {
if (datasets.filter(x => x === meta._dataset).length === 0) {
me._destroyDatasetMeta(index);
}

View File

@ -0,0 +1,102 @@
var barChartData = {
labels: [0, 1, 2, 3, 4, 5, 6],
datasets: [
{
backgroundColor: 'red',
data: [
// { x: 0, y: 0 },
{x: 1, y: 5},
{x: 2, y: 5},
{x: 3, y: 5},
{x: 4, y: 5},
{x: 5, y: 5},
{x: 6, y: 5}
]
},
{
backgroundColor: 'blue',
data: [
{x: 0, y: 5},
// { x: 1, y: 0 },
{x: 2, y: 5},
{x: 3, y: 5},
{x: 4, y: 5},
{x: 5, y: 5},
{x: 6, y: 5}
]
},
{
backgroundColor: 'green',
data: [
{x: 0, y: 5},
{x: 1, y: 5},
// { x: 2, y: 0 },
{x: 3, y: 5},
{x: 4, y: 5},
{x: 5, y: 5},
{x: 6, y: 5}
]
},
{
backgroundColor: 'yellow',
data: [
{x: 0, y: 5},
{x: 1, y: 5},
{x: 2, y: 5},
// {x: 3, y: 0 },
{x: 4, y: 5},
{x: 5, y: 5},
{x: 6, y: 5}
]
},
{
backgroundColor: 'purple',
data: [
{x: 0, y: 5},
{x: 1, y: 5},
{x: 2, y: 5},
{x: 3, y: 5},
// { x: 4, y: 0 },
{x: 5, y: 5},
{x: 6, y: 5}
]
},
{
backgroundColor: 'grey',
data: [
{x: 0, y: 5},
{x: 1, y: 5},
{x: 2, y: 5},
{x: 3, y: 5},
{x: 4, y: 5},
// { x: 5, y: 0 },
{x: 6, y: 5}
]
}
]
};
module.exports = {
config: {
type: 'bar',
data: barChartData,
options: {
scales: {
x: {
display: false,
stacked: true
},
y: {
display: false,
stacked: true
}
}
}
},
options: {
run(chart) {
chart.data.datasets.splice(0, 1);
chart.update();
}
}
};

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB