2013-03-22 37 views
1

我與jqplot的工作,我已經繪製的是來自數組PHP中的數據,但是當我試圖從JavaScript來繪製不工作攜帶一個PHP數組javascript和情節witjh jqplot

這是PHP代碼:

$array= array(); 
$array[] = 34.890; 
$array[] = 25.090; 

現在代碼吸收水分:

<script> 
$(function() { 

    var array = new Array(); 

    <?php 
     for($i=0; $i<count($array); $i++){ 
      echo "\narray[$i] = '$array[$i]';"; 
     } 
    ?> 
    graficar( array ); 
}); 
</script> 

功能graficar是:

function graficar(array) 
{ 

    var plot2 = $.jqplot('lienzo', 
     [ 
      array 
     ], 
     { 
      seriesDefaults: { 
       renderer: $.jqplot.BarRenderer, 
       // Show point labels to the right ('e'ast) of each bar. 
       // edgeTolerance of -15 allows labels flow outside the grid 
       // up to 15 pixels. If they flow out more than that, they 
       // will be hidden. 
       pointLabels: {show: true, location: 'e', edgeTolerance: -15}, 
       // Rotate the bar shadow as if bar is lit from top right. 
       shadowAngle: 135, 
       // Here's where we tell the chart it is oriented horizontally. 
       rendererOptions: { 
        barDirection: 'horizontal', 
        barWidth:18 
       } 
      }, 
      axes: { 
       yaxis: { 
        renderer: $.jqplot.CategoryAxisRenderer 
       } 
      } 
     } 
    ); 

} 

我得到的錯誤是:

TypeError: this[r]._ticks[0] is undefined 
[Break On This Error] 

...{href:function(a){return a.getAttribute("href")},type:function(a){return a.getAt... 

的圖表顯示:

enter image description here

如何解決我的麻煩?

坦克。

+2

與PHP和JS中parseJson(jQuery的)json_encode :-) – 2013-03-22 16:10:43

+0

也試試,智者可以讓你通過PHP數組到HTML頁面,然後你可以調用與JavaScript – 2013-03-22 16:20:39

+0

這些陣列作爲KA_lin說使用json_encode在PHP使你的數組​​可以通過JavaScript來理解。但是,不需要在Javascript中使用parseJson,Jqplot可以處理它 – AnthonyLeGovic 2013-03-22 16:49:57

回答

0

有什麼不對你的「陣列」,因爲在我的代碼,當

array = ["0","0","0","0"] 

array = ["w","r","o","n","g"] 

錯誤發生,那麼我應該使用parseInt方法字符串元素解析到整數在第一個。如果

array = ["1","2","3","4"] 

有沒有必要解析字符串元素intger在a rray,jqplot會爲你做,但如果數組就像前兩個一樣,這裏就會出現錯誤。