当我超过2时,我试图从我的图表中删除/隐藏/弹出某个数据集。弹出它只删除最后一个数据集推送(ed)。请参阅以下内容:
$('#radar2015').on('change', function () {
var newDataset2 = {
label: '2016',
data: [2019, 552, 750, 1280, 1190, 2345, 2123, 534, 1234],
backgroundColor: 'rgba(0, 183, 253, .25)',
borderColor: 'rgb(0, 183, 253)',
borderWidth: 2
};
if ($(this).is(':checked')) {
radarChart.data.datasets.push(newDataset2);
}
else {
radarChart.data.datasets.pop();
}
radarChart.update();
});
$('#radar2016').on('change', function () {
var newDataset3 = {
label: '2017',
data: [3235, 1125, 2650, 1380, 990, 2345, 123, 1234, 1234],
backgroundColor: 'rgba(220, 40, 180, .25)',
borderColor: 'rgb(220, 40, 180)',
borderWidth: 2
};
if ($(this).is(':checked')) {
radarChart.data.datasets.push(newDataset3);
}
else {
radarChart.data.datasets.pop();
}
radarChart.update();
});
答案 0 :(得分:0)
您需要使用filter()从数组中删除特定值
radarChart.data.datasets.filter((item) => {
// condition for removing dataset
});
注意:filter()每次都会返回一个新的数组
来自MDN的示例:
var words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];
const result = words.filter(word => word.length > 6);
console.log(result);