我有一个ChartJS对象数组,由于某种原因,在数组中的图表上使用chart.data.datasets.push({});也会将一个空数据集推送到数组中的其他图表。
res_chartArray.forEach((chart) => {
    console.log(chart);
}); //this displays all datasets for all charts
res_chartArray[chartElement].config.data.datasets.push({data: []});
//i also tried chart.data.datasets.push()
res_chartArray.forEach((chart) => {
    console.log(chart);
}); //this displays all datasets for all charts again, it's how i checked that the dataset meant for just one chart is also pushed to other charts我预计空数据集将被推送到一个图表,但也会被推送到图表数组中的其他图表。
0是第一张图,1是第二张图(图)

发布于 2019-07-29 15:13:39
修复了!我有两个变量用于初始数据和选项,这些变量在创建时设置为每个图表。每次操作每个图表时,更新都会继续进行到其他图表,因为它们是用相同的初始变量初始化的;)
    var defaultData = {labels: [], datasets: []};
    var defaultOptions = {};
    function addDataset(){
        chartArray.push(new Chart(ctx, {type: 'bar',
            data: defaultData,       //these two
            options: defaultOptions, //were causing the problem
        }));
    }https://stackoverflow.com/questions/57247137
复制相似问题