我试图动态添加图表的标签和数据集。
var LabelResults = [];
var DatasetResults = [];
var getObject = jQuery.getJSON("<?=base_url()?>admin/JSONDateTime/", function (data) {
data.forEach(function (packet){
LabelResults.push(packet.pickup_datetime);
DatasetResults.push(packet.total);
});
});
// chart snippet
data: {
labels: LabelResults,
type: 'line',
defaultFontFamily: 'Montserrat',
datasets: [{
label: "Foods",
data: DatasetResults,
backgroundColor: 'transparent',
borderColor: 'rgba(220,53,69,0.75)',
borderWidth: 3,
pointStyle: 'circle',
pointRadius: 5,
pointBorderColor: 'transparent',
pointBackgroundColor: 'rgba(220,53,69,0.75)',
}, {
label: "Electronics",
data: [500, 800],
backgroundColor: 'transparent',
borderColor: 'rgba(40,167,69,0.75)',
borderWidth: 3,
pointStyle: 'circle',
pointRadius: 5,
pointBorderColor: 'transparent',
pointBackgroundColor: 'rgba(40,167,69,0.75)',
}]
}
当我将数组分别分配到标签和数据上时,我的线不会生成,但是另一个静态线图完美地工作。我已经阅读了与我类似的其他问题,我确定我没有生成2d数组。这些属性根本不识别数组,或者我只是做错了什么?谢谢。