2012-04-02 115 views
0

我有這樣的代碼來生成圖表:如果我使用的PHP文件谷歌圖的返回結果不工作如何使用jQuery ajax()繪製Google Chart?

foreach ($arr as $v){ 
    $ts = $v['timestamp']; 
    $p = $v['price']; 
    $po = $v['price_old']; 
    $resp[] = "[" 
            . "'$ts'" . ", " 
            . "$p" . ", " 
            . "$po" 
            . "]"; 
} 

echo implode(',',$resp); 

<script type="text/javascript"> 

    google.load('visualization', '1.0', {'packages':['corechart']}); 
    $(document).ready(function() { 

    $('.show_chart').click(function(){ 
     var sku = $(this).attr("data-sku"); 
     var vid = $(this).attr("data-vid"); 

     $.ajax({ 
      type: "POST", 
      url:"../ajax/priceChart.php", 
      async: false, 
      dataType:'text',//moment google apito mai ne go zaredi ! 
      data: {'sku':sku,'vid':vid}, 
      success:function(vals){ 
       var vals = vals; 
       $(".dialog").dialog({ 
        modal:true, 
        buttons: { "Cancel": function() { $(this).dialog("close"); } } 
       }); 


drawChart(vals); 
       return false; 



      }, 
       error:function (xhr, ajaxOptions, thrownError){ 
        alert(xhr.status); 
        alert(thrownError); 
       } 
     });  
    }); 



    }) 
function drawChart(vals) { 
     console.log(1); 
     // Create the data table. 
     var data = new google.visualization.DataTable(); 
     data.addColumn('string', 'Date'); 
     data.addColumn('number', 'New'); 
     data.addColumn('number', 'Old'); 
     data.addRows([ 
     // ['2012-03-29 16:36:58', 313.99000, 314.00000], 
     // ['2012-03-29 16:36:58', 313.99000, 314.00000] 
      vals 

     ]); 


     // Set chart options 
     var options = {'title':'PriceChart'}; 

     // Instantiate and draw our chart, passing in some options. 
     var chart = new google.visualization.LineChart(document.getElementById('chart_div')); 
     chart.draw(data, options); 
     console.log(4); 
     } 

</script> 

從PHP返回的結果。如果我使用默認值谷歌圖表工作。哪裏有問題 ?在此先感謝,我爲我的英語道歉!

回答

1

在您的Drawchart函數中,您已將New和Old定義爲數字。你從PHP中獲得了兩倍。

foreach ($arr as $v){ 
$ts = $v['timestamp']; 
$p = (int) $v['price']; 
$po = (int) $v['price_old']; 
$resp[] = "[" 
           . "'$ts'" . ", " 
           . "$p" . ", " 
           . "$po" 
           . "]"; 
} 

echo implode(',',$resp); 

請試試這個代碼。

希望得到這個幫助。

〜K