2010-08-21 6 views
4

J'ai un problème avec JSON et jqPlot.Chargement des données JSON dans jqPlot

scénario 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' 
      } 
     }] 
    }); 
}); 

php de 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); 

Résultat fichier php est comme ceci:

[{"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 de ligne 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"]] 

et erreur: uncaught exception: [object Object]

Répondre

2

J'ai probablement trouvé la solution. Au début, $ .jqplot doit être dans $ .getJSON - J'ai oublié le code d'invocation asynchrone en JavaScript.

Je inutilement ajouté guillemet aux données [index] .DATA

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

Mais je devais ajouter Nombre (données [index] .kurs_odn) becouse qui était chaîne par défaut. Maintenant, il semble bien fonctionner.

Questions connexes