2011-03-11 40 views
0

我想繪製使用從PHP和jqplot數據的簡單圖形。簡單的PHP數據的jquery(JSON)

在PHP中我只是這樣做:

$test = array('1' => '2', '3' => '4'); 
echo json_encode($test); 

結果我得到JS一邊是:{ 「1」: 「2」, 「3」: 「4」},我檢查了這一點。

於是,我試着畫出這個簡單的數組,但沒有運氣:

$.ajax({ 
     type: "POST", 
     url: "ajax.php", 
     data: "fname="+ fname +"& lname="+ lname, 
     dataType:"json", 
     success: function(data) 
     { 
     $('#dissapear').hide(function(){$('#chartdiv').css("height:400px;width:300px;");$('#chartdiv').fadeIn();}); 
     $.jqplot 
     ('chartdiv', [data], 
      { 
      title:'Jitter', 
      axes:{yaxis:{min:0,max:240},xaxis:{renderer:$.jqplot.DateAxisRenderer}}, 
      series:[{lineWidth:4, markerOptions:{style:'square'}},{ show: true } ] 
      } 
     ); 

我錯過了什麼?請幫助我,我已經嘗試了至少20個代碼組合,但沒有運氣。是的,我確實看過jqplot中的dataRenderer示例,但它並沒有幫助我。

回答

0

我不熟悉jqplot,但在我看來:

$test = array('1' => '2', '3' => '4'); 

也許應該

$test = array(1 => 2, 3 => 4); 

也就是說,你不需要數組值是字符串。

+0

是的,我同意,我錯過了一個。但即使改變了這個,也沒有。問題是我不明白jqplot期望的格式。 – Newman1510 2011-03-11 17:14:52

+1

你有沒有看到這個文檔呢? http://www.jqplot.com/docs/files/usage-txt.html。看起來你想生成如下所示的json:[[[1,2],[3,5.12],[5,13.1],[7,33.6],[9,85.9],[11,219.9]]] – mooreds 2011-03-14 14:55:17

0

我相信,問題就來了,當您返回的JSON ...您的JavaScript不明白,這是一個數組英寸沒有「陣列」類型轉換,所以我在這個問題上摸不着頭腦。