【发布时间】:2015-06-07 02:32:36
【问题描述】:
我正在尝试使用数据库数据从 canvasjs 制作线条图。但我在将数据传递给数据点时遇到问题。
$.ajax({
dataType : "json",
type: "POST",
url: base_url + 'index.php/dashboard/line_graph',
data: {},
success: function(data)
{
for(var i = 0; i < data.length; i++)
{
var firstdate = data[i].nextdate;
var res = firstdate.replace(/[-]/g,',');
fd += '{ x: new Date(' + res + '), y:' + data[i].count +'},';
}
var fin = fd.slice(0,-1);
finals = "[" + fin + "]";
var chart = new CanvasJS.Chart("chartContainer",
{
title:{
text: "Booking - per month (DEMO)"
},
data: [
{
type: "line",
dataPoints: finals
}
]
});
chart.render();
}
});
当我提醒finals 并将提醒的数据复制粘贴到数据点并运行时,它就开始工作了。但是当包含相同数据的变量传递给数据点时,它就不起作用了。
在控制台日志中,我收到此错误。
TypeError: l.dataPoints[q].x is undefined
数据点的格式是这样的。当我发出警报时,变量finals 也包含相同的数据。
[
{ x: new Date(2015,03,6), y:4},
{ x: new Date(2015,03,11), y:0},
{ x: new Date(2015,03,16), y:0},
{ x: new Date(2015,03,21), y:0},
{ x: new Date(2015,03,26), y:0},
{ x: new Date(2015,03,31), y:14}
]
我的页面返回这个 json_encode 格式。
[
{"firstdate":"2015-03-01","nextdate":"2015-03-6","count":"4"},
{"firstdate":"2015-03-6","nextdate":"2015-03-11","count":"0"},
{"firstdate":"2015-03-11","nextdate":"2015-03-16","count":"0"},
{"firstdate":"2015-03-16","nextdate":"2015-03-21","count":"0"},
{"firstdate":"2015-03-21","nextdate":"2015-03-26","count":"0"},
{"firstdate":"2015-03-26","nextdate":"2015-03-31","count":"14"}
]
我不明白这些问题。请帮忙。
【问题讨论】:
-
您好,您可以添加您的页面
index.php/dashboard/line_graph返回的数据吗? -
您好,我已经编辑了我的问题。折线图返回 json 格式。
标签: javascript jquery ajax html5-canvas canvasjs