[英]Group Bar Chart Dinamic Data Update By Button Fuction ChartJS
I have Data For My Chart Like This我有这样的图表数据
For Data对于数据
var dataObject = [
{
label: ['1 Mouth', '2 Mouth', '3 Mouth', '4 Mouth', '5 Mouth', '6 Mouth', '7 Mouth'],
datachart: [
{
label: 'Good',
data: [1,2,3,4,5,6,7],
backgroundColor: '#f56954'
},
{
label: 'Middle',
data: [1,2,3,4,5,6,7],
backgroundColor: '#3c8dbc'
},
{
label: 'Not Good',
data: [1,2,3,4,5,6,7],
backgroundColor: '#74b32d'
}
]
},
{
label: ['1 Mouth', '2 Mouth', '3 Mouth', '4 Mouth', '5 Mouth', '6 Mouth', '7 Mouth'],
datachart: [
{
label: 'Good',
data: [11,12,13,14,15,16,71],
backgroundColor: '#f56954'
},
{
label: 'Middle',
data: [1,12,31,4,5,16,7],
backgroundColor: '#3c8dbc'
},
{
label: 'Not Good',
data: [1,2,3,4,5,61,7],
backgroundColor: '#74b32d'
}
]
}
];
For Chartjs对于 Chartjs
var data = {
labels: dataObject[0].label,
datasets: dataObject[0].datachart
};
How to update data according to dataObject variable?如何根据 dataObject 变量更新数据? I've done this
我已经做到了
function changeData7(index) {
chart7.data.datasets.forEach(function(dataset) {
dataset.label = dataObject[index].label;
dataset.data = dataObject[index].datachart;
});
chart7.update();
}
but the data is not updated instead it is lost, whereas if I only use 1 data not a group, it is successfully updated但是数据没有更新而是丢失了,而如果我只使用1个数据而不是一个组,则它已成功更新
function changeData7(index) {
chart7.data.datasets.forEach(function(item, indexs) {
item.label = dataObject[index].datachart[indexs].label;
item.data = dataObject[index].datachart[indexs].data;
});
chart7.update();
}
I do this but if i call changeData7(0)
, data doesnt change to index 0我这样做,但如果我调用
changeData7(0)
,数据不会更改为索引 0
You're updating each dataset, and feeding it's data
with the new dataset.您正在更新每个数据集,并使用新数据集为其提供
data
。 Also, the labels should also be provided on the chart itself, instead of the dataset items.此外,还应在图表本身上提供标签,而不是在数据集项上提供。
The correct way would be正确的方法是
function changeData(index) {
var data = dataObject[index];
chart.data.labels = data.label;
chart.data.datasets = data.datachart;
chart.update();
}
https://jsfiddle.net/8kzoc6nb/ https://jsfiddle.net/8kzoc6nb/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.