javascript – 将Json数据加载到jqPlot中
作者:互联网
我对JSON和jqPlot有疑问.
jQuery脚本:
var line = [ ];
$(function(){
$.getJSON('bin/gielda.php', function(data) {
$.each(data, function (index, value) {
line.push(["'"+data[index].data+"'",data[index].kurs_odn]);
});
console.log(line);
});
$.jqplot('chartdiv', [line], {
title :' Giełda',
axes : {
xaxis : {
renderer : $.jqplot.DateAxisRenderer
}
},
series : [{
lineWidth : 4,
markerOptions : {
style : 'square'
}
}]
});
});
来自gielda.php的php:
$pdo = new PDO('mysql:host=localhost;dbname=gielda', 'root', '');
$pdo -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = $pdo -> prepare("SELECT data,kurs_odn FROM template WHERE nazwa=?");
$sql -> execute(array("ASSECOPOL"));
$gielda = $sql->fetchAll(PDO::FETCH_ASSOC);
echo json_encode($gielda);
php文件的结果如下:
[{"data":"2010-08-19","kurs_odn":"55.75"},{"data":"2010-08-19","kurs_odn":"55.75"},{"data":"2010-08-19","kurs_odn":"55.75"},{"data":"2010-08-20","kurs_odn":"56.2"},{"data":"2010-08-20","kurs_odn":"56.2"},{"data":"2010-08-20","kurs_odn":"56.2"}]
变量行的Console.log:
[["'2010-08-19'", "55.75"], ["'2010-08-19'", "55.75"], ["'2010-08-19'", "55.75"], ["'2010-08-20'", "56.2"], ["'2010-08-20'", "56.2"], ["'2010-08-20'", "56.2"]]
和错误:未捕获的异常:[object Object]
解决方法:
我可能找到了解决方案.
起初$.jqplot必须在$.getJSON里面 – 我忘记了JavaScript中的异步调用代码.
我不必要地在数据[index] .data中添加了引号
line.push(["'"+data[index].data+"'",data[index].kurs_odn]);
但我必须添加Number(data [index] .kurs_odn)因为默认情况下是字符串.
现在看来工作正常.
标签:jquery,javascript,jqplot 来源: https://codeday.me/bug/20190705/1383797.html