Loading Json data in jqPlot

I have a 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); 

The result from the php file is as follows:

 [{"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 a string variable:

 [["'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]

+4
source share
1 answer

I probably found a solution. First, $ .jqplot should be inside $ .getJSON - I forgot about the asynchronous call code in JavaScript.

I unnecessarily added a quotation mark to the data [index] .data p>

 line.push(["'"+data[index].data+"'",data[index].kurs_odn]); 

But I had to add Number (data [index] .kurs_odn) because it was the default string. Now it works fine.

+2
source

All Articles