本文介绍了将Json数据加载到jqPlot中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在使用JSON和jqPlot时遇到问题.
I've problem with JSON and 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:
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);
php文件的结果如下:
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来自可变行:
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"]]
和错误:uncaught exception: [object Object]
推荐答案
我可能找到了解决方案.首先,$.jqplot必须位于$ .getJSON内-我忘记了JavaScript中的异步调用代码.
I probably found the solution.At first $.jqplot have to be inside $.getJSON - I forgot about asynchronous invocation code in JavaScript.
我不必要在data [index] .data中添加了引号
I unnecessarily added quote mark to data[index].data
line.push(["'"+data[index].data+"'",data[index].kurs_odn]);
但是我不得不添加Number(data [index] .kurs_odn),因为默认情况下它是字符串.现在看来一切正常.
But I had to add Number(data[index].kurs_odn) becouse that was string by default.Now it seems working fine.
这篇关于将Json数据加载到jqPlot中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!