我使用canvasJS来绘制数据图表。我发出了一个AJAX请求,并获得了一个可以用这种格式绘制的数据列表:
dataPoints: [{
x: new Date(2012, 00, 1),
y: 450
}, {
x: new Date(2012, 01, 1),
y: 414
}, ]
我从AJAX请求中回来的列表如下:
sup_2 = [{ x: new Date(2012, 00, 1), y: 450 },{ x: new Date(2012, 01, 1), y: 414 },]
我似乎无法将其作为要使用的变量传递,即使它已正确格式化。为了澄清,如果我复制sup_2是什么并将其粘贴,我会看到一个图表,但是当我离开时,我得到上面的错误,我什么都没看到。
我的功能如下:
<script>
function sample_current(sup_2) {
console.log(sup_2)
var chart = new CanvasJS.Chart("temperatureChart", {
zoomEnabled: true,
toolTip: {
shared: "true" //disable here.
},
title: {
text: "Sample Temp",
fontSize: 18
},
axisX: {
title: "time",
gridThickness: .2,
lineThickness: .2,
titleFontSize: 14,
labelFontSize: 12,
},
axisY: {
title: "Temperature (C)",
gridThickness: .2,
lineThickness: .2,
titleFontSize: 14,
labelFontSize: 12,
},
data: [{
markerType: "triangle",
type: "line",
xValueType: "dateTime",
showInLegend: true,
name: "Supply",
dataPoints: sup_2
}, ],
});
chart.render();
}
</script>
<script>
function get_temp_data(element_id, period){
$("#temperatureChart").foggy();
$.ajax({
type: "GET",
url: $SCRIPT_ROOT + "/sample/test_function",
contentType: "application/xml; charset=utf-8",
data:{},
success: function(data) {
sample_current(data.b)
},
error: function(jqXHR, textStatus, errorThrown) {
alert('choose the facility');
}
});
}
<script>