gpt4 book ai didi

java - ChartJS "Unable to get property ' getTime' 未定义或空引用”

转载 作者:行者123 更新时间:2023-12-01 17:59:48 24 4
gpt4 key购买 nike

我正在尝试将图表值从 java servlet 传递到网页。图表应将日期显示为 x,将整数显示为 y。

Servlet 发送包含年、月、日和值的对象数组。

功能:

function getMultiChart() {
jQuery.ajax({
type: "POST",
data: {formType: 'getProjection'},
url: "ChartServlet",
dataType: 'json',
asynch: false,
success: function (data) {
var obj = JSON.stringify(data);
var obj2 = JSON.parse(obj);
var tmpArr = new Array();
jQuery.each(obj2, function (prop, val){
tmpArr.push("x: new Date(" + Number(val.year) + "," + Number(val.month) + "," + Number(val.day) + "), y: " + Number(val.value));
});
var chart = new CanvasJS.Chart("chartContainer",{
title: {
text: "Manpower"
},
data: [
{
type: "column",
indexLabel: "{y}",
indexLabelPlacement: "outside",
indexLabelOrientation: "horizontal",
showInLegend: true,
name: "Overtime Personnel",
dataPoints: tmpArr
}
]
});
chart.render();
},
error: function () {
console.log(data);
}
});
}

当我运行它时,我在 ChartJS.js 中收到上述错误。

如果我用硬编码值替换 json 数据,效果很好,例如:

var tmpArr = [
{x: new Date(2020, 01, 1), y: 18},
{x: new Date(2020, 01, 2), y: 0},
{x: new Date(2020, 01, 3), y: 0},
{x: new Date(2020, 01, 4), y: 0},
{x: new Date(2020, 01, 5), y: 0},
{x: new Date(2020, 01, 6), y: 0},
{x: new Date(2020, 01, 7), y: 0},
{x: new Date(2020, 01, 8), y: 0},
{x: new Date(2020, 01, 9), y: 0},
{x: new Date(2020, 01, 10), y: 0},
{x: new Date(2020, 01, 11), y: 0},
{x: new Date(2020, 01, 12), y: 0}
]

我哪里搞砸了?

最佳答案

我的错误是数据数组必须是对象数组,而不是字符串。

即使这样,我仍然无法让“x: new Date(...)”部分工作。

工作代码是:

var tmpArr = new Array();
jQuery.each(obj2, function (prop, val) {
var t = {y: val.value, label: val.month + "/" + val.day + "/" + val.year};
tmpArr.push(t);
});

关于java - ChartJS "Unable to get property ' getTime' 未定义或空引用”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60657306/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com