【问题标题】:ChartJs create Chart from arrayChartJs 从数组创建图表
【发布时间】:2019-02-10 11:10:14
【问题描述】:

我正在通过 ajax 获取 ChartJs 图表的数据和标签,并以此创建一个数组。

当我制作 console.log 时,数组看起来可以正常工作,但图表没有标签也没有数据。

我在这里找到了this 的问题,但并没有解决问题。

这是我的 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;
                            }

                        }

但图表只是一个空图表...

【问题讨论】:

    标签: javascript chart.js


    【解决方案1】:

    问题在于 Ajax 请求是异步的。所以图表是在没有任何数据的情况下绘制的。

    ajax 请求必须有async: false,,这样才能加载数据,然后用数据绘制图表。

    【讨论】:

      猜你喜欢
      • 2021-04-28
      • 2021-09-24
      • 2017-08-04
      • 1970-01-01
      • 2019-03-13
      • 2021-04-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多