javascript - ChartJs 从数组创建图表
问题描述
我通过 ajax 获取 ChartJs 图表的数据和标签,并从中创建了一个数组。
当我制作 console.log 时,数组看起来可以正常工作,但图表没有标签也没有数据。
我在这里找到了这个问题,但它并没有解决问题。
这是我的chartJs代码:
config = {
type: 'bar',
data: {
labels: labels,
datasets: [{
label: "Aufrufe",
data: dataarray,
backgroundColor: 'rgba(0, 188, 212, 0.8)'
}]
},
options: {
responsive: true,
legend: false
}
}
以及我从 console.log 获得的数组:
这就是我创建数组的方式:
/* create a labels array */
while(year !== (new Date).getFullYear() || month !== enddate) {
console.log(year, month);
labels.push(month + " " + year);
month++;
if(month === 13) {
year++;
month = 1;
}
}
但图表只是一个空图表......
解决方案
问题是 Ajax 请求是异步的。所以图表是在没有任何数据的情况下绘制的。
必须有 ajax 请求,async: false,
以便可以加载数据,然后使用数据绘制图表。
推荐阅读
- java - Jooq/SQL 查找另一列中唯一值的平均值
- sql - 在子查询中将数据类型 nvarchar 转换为 bigint 时出错
- docker - SLURM+Docker:如何使用 SLURM 杀死 docker 创建的进程 scancel
- vue.js - 如何访问 vuex 商店中的路由器 ID 并将其附加到 Axios 获取方法
- amazon-web-services - 每个任务/功能都需要一个新的 EC2 实例吗?
- phaser-framework - 使用 moveTo 函数在指针向下移动精灵时出现故障
- c# - 使用 Graph API 从 Azure AD 检索所有用户时禁止 403
- vba - VBA:从;-分隔的单元格中查找字符串中的单词
- c# - 从注释参数创建变量
- vue.js - 在 VueJS 中从 Prop 和 Prop 值创建类