Loading Json data into jqPlot [closed]

时间秒杀一切 提交于 2019-12-30 09:55:11

问题


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]


回答1:


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

line.push(["'"+data[index].data+"'",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.



来源:https://stackoverflow.com/questions/3535680/loading-json-data-into-jqplot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!