将 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]
I've problem with JSON and jqPlot.
jQuery script:
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'
}
}]
});
});
php from gielda.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);
Result from php file is like this:
[{"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 from variable line:
[["'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"]]
and error: uncaught exception: [object Object]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我可能找到了解决方案。
首先 $.jqplot 必须位于 $.getJSON 内 - 我忘记了 JavaScript 中的异步调用代码。
我不必要地向 data[index].data 添加了引号,
但我必须添加 Number(data[index].kurs_odn) 因为默认情况下是字符串。
现在看来工作正常。
I probably found the solution.
At first $.jqplot have to be inside $.getJSON - I forgot about asynchronous invocation code in JavaScript.
I unnecessarily added quote mark to data[index].data
But I had to add Number(data[index].kurs_odn) becouse that was string by default.
Now it seems working fine.