0

我的问题在于显示数据,我根本无法显示它。

当我这样写它时,它可以工作 "data: [5, 0, 2, 1, 2, 1, 0, 0, 5]"

但是当我使用函数加载和推送数据时,它什么也不显示。控制台中的数据对我来说看起来一样"[5, 0, 2, 1, 2, 1, 0, 0, 5]"

  var f = document.getElementById("lineChart");
            new Chart(f, {
                type: "line",
                data: {
                    labels: ["3:00","6:00", "9:00", "12:00", "15:00", "18:00", "21:00", "24:00"],
                    datasets: [{
                        label: "Stress value:",
                        backgroundColor: "rgba(38, 185, 154, 0.31)",
                        borderColor: "rgba(38, 185, 154, 0.7)",
                        pointBorderColor: "rgba(38, 185, 154, 0.7)",
                        pointBackgroundColor: "rgba(38, 185, 154, 0.7)",
                        pointHoverBackgroundColor: "#fff",
                        pointHoverBorderColor: "rgba(220,220,220,1)",
                        pointBorderWidth: 1,
                        data: (function (){
                                var res = [];
                                var len = 10;       

                                 $.ajax({         
                                  dataType:"JSON",            
                                  type: 'GET',
                                  url: "https://stressmonitor.smasty.net/api/1.0/day_stress/2017-02-26/?app_id=12345&token=12345"
                                 }).then(function(dataa) {

                                    var i= 0;
                                        while (len--) {
                                        console.log(res);
                                        res.push(dataa.intervals[i].value);
                                        i++;
                                        }
                                 });            

                            return res;
                         })()
                    }]
                }
            })
        }
4

0 回答 0