更新CanvasJS图表的数据点时遇到问题,并且错误消息显示


  “未捕获的TypeError:无法读取未定义的属性'getTime'”。


我也尝试过使其成为这样的对象

var parLabels = JSON.stringify(labels);
var obj = JSON.parse(parLabels);


它仍然没有用。

index.html

<div id="chartContainer" style="height: 300px; width: 100%;"></div>

<script type="text/javascript>
$(function() {
  google.script.run.withSuccessHandler(showDaily).getData();
});
function showDaily(myObj) {
  var email = myObj.email;
  var entity = myObj.entity;
  var entity = JSON.parse(entity);
  var monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
  var labels = '';
  for (var i = 1; i < entity.length; i++) {
    var month = new Date(entity[i][0]).getMonth();
    var day = new Date(entity[i][0]).getDate();
    var date = day + " " + monthNames[month];
    var score = entity[i][7];
    if (entity[i][2] != '') {
      if (labels != "") {
        labels = labels + ",{label:" + date + ",y:" + score + "}";
      } else {
        labels = "{label:" + date + ",y:" + score + "}";
      }
    }
  }
  var chart = new CanvasJS.Chart("chartContainer", {
    title: {
        text: "My First Chart in CanvasJS"
    },
    data: [
    {
        // Change type to "doughnut", "line", "splineArea", etc.
        type: "line",
        dataPoints: [ labels ]
    }
    ]
  });
  chart.render();
}

</script>

最佳答案

在CanvasJS中,dataPoints应该是一个对象数组。但是在您的情况下,它似乎是一个字符串。尝试将labels更改为数组并将其传递给dataPoints应该可以正常工作。

var labels = [];
for (var i = 1; i < entity.length; i++) {
    var month = new Date(entity[i][0]).getMonth();
    var day = new Date(entity[i][0]).getDate();
    var date = day + " " + monthNames[month];
    var score = entity[i][7];
    if (entity[i][2] != '') {
        labels.push({label: date, y: score });
    }
}


然后将其作为dataPoints: labels传递。

关于javascript - 未捕获的TypeError:无法读取CanvasJS中未定义的属性'getTime',我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52815117/

10-09 23:26