2014-02-27 106 views
0

我正在使用谷歌圖表API和我得到一個問題,我不明白。如果我使用最終警報(「Im IN!」),所有代碼都可以正常工作,但是如果將其刪除,我的nArray未被填充。我不明白爲什麼。谷歌圖表Api:不加載數據

這是全局變量:

var Chart; 
var data; 
var nArray; 

這是我填滿我nArray,所以我可以加載到我的圖表。

function setArray(PlayerName,LeadPoints,OppPoints,PropPoints){ 

     var newPlayer = [PlayerName,LeadPoints,PropPoints,OppPoints,'Total']; 

     nArray.push(newPlayer); 

    } 

這是我去CRM帶來的數據並填寫我的數組調用setArray功能。

function setPointsByEntity() { 


    SDK.REST.retrieveMultipleRecords(
     "gamify_ponto", 
     "$select=gamify_Entidade,gamify_PlayerId,gamify_Pontos,gamify_gamify_utilizador_gamify_ponto/gamify_name&$orderby=gamify_PlayerId asc&$expand=gamify_gamify_utilizador_gamify_ponto", 

     function (results) { 


      if(results.length>0){ 

       for(var i=0;i<results.length;i++){ 
(...) // do something 
    setArray(lastPlayer.Name,leadPoints,oppPoints,propPoints); 



       } 

      } 
      else { 
       alert("No Contact records are available to set as the primary contact for the account."); 
       } 
     }, 

     errorHandler, 

     function() { 

     //OnComplete handler 
     } 
    ); 

這是我加載可視化API,運行谷歌可視化,並定義我的drawVisualization函數。

// Load the Visualization API and the piechart package. 
      google.load('visualization', '1', {'packages':['corechart']}); 

    // Set a callback to run when the Google Visualization API is loaded. 
      google.setOnLoadCallback(drawVisualization); 


    function drawVisualization() { 



     // Create and populate the data table. 
     data = google.visualization.arrayToDataTable(nArray); 

     setLabelTotal(data); 
     var view = new google.visualization.DataView(data); 

     view.setColumns([0, 1, 2, 3, { 
     type: 'number', 
     calc: function (dt, row) { 
      // set offset to determine how far you want to move the labels 
      var offset = MaxArray(nArray) *0.03; // 3% do valor total. 
      return dt.getValue(row, 1) + dt.getValue(row, 2) + dt.getValue(row, 3) + offset; 
     } 
    }, 
    4]); 


     var options = {title:"Pontos por Jogador", 
        width:500, height:300, 
        hAxis: { 
         textStyle: {'color': 'white'}}, 
       isStacked: true, 
        legend: { 
        position: 'top' 
        }, 
        series: { 
      3: { 
       type: 'line', 
       color: 'grey', 
       lineWidth: 0, 
       pointSize: 0, 
       visibleInLegend: false 
      } 
     }, 

       vAxis: { 
       viewWindowMode:'explicit', 
       viewWindow: { 
         max:MaxArray(nArray) + MaxArray(nArray)*0.3, 
         min:0 
        } 
       }, 
       animation:{ 
          duration: 1000, 
          easing: 'linear'} 
        }; 

     // Create and draw the visualization. 
     chart = new google.visualization.ColumnChart(document.getElementById('visualization')); 

     chart.draw(view, options); 

     } 

最後,當DOM就緒我通過調用setPointsByEntity()分配我nArray。這裏的問題是如果我評論「alert(」Im IN!「);」圖表不會出現。看來nArray沒有定義。

//每當DOM準備

$(document).ready(function() 
{ 

setPointsByEntity(); // This function fill my nArray 
alert("Im IN!"); 
}); 

這個問題可能有與我以前發佈的另一個人做,請按照從文件準備在Question

+0

由於異步調用,您似乎有問題。在創建數組時,必須從函數(結果){...'的末尾調用'drawVisualization()'。 –

+0

是的,但這是如何解釋這個事實,即如果我運行'alert(「...」);'它的工作原理。 – ePascoal

+0

它的工作原理是,當alert()被打開並關閉它時,一段時間過去並收集數據。它與你的另一個問題中的'setTimeout()'提示類似。 –

回答

1

移動的setPointsByEntity處理程序從谷歌加載器回調,然後在AJAX調用的成功處理程序結束時調用drawVisualization函數:

google.setOnLoadCallback(setPointsByEntity); 

function setPointsByEntity() { 
    SDK.REST.retrieveMultipleRecords(
     "gamify_ponto", 
     "$select=gamify_Entidade,gamify_PlayerId,gamify_Pontos,gamify_gamify_utilizador_gamify_ponto/gamify_name&$orderby=gamify_PlayerId asc&$expand=gamify_gamify_utilizador_gamify_ponto", 
     function (results) { 
      if(results.length>0){ 
       for(var i=0;i<results.length;i++){ 
        (...) // do something 
        setArray(lastPlayer.Name,leadPoints,oppPoints,propPoints); 
       } 
      } 
      else { 
       alert("No Contact records are available to set as the primary contact for the account."); 
      } 
      drawVisualization(); 
     }, 
     errorHandler, 
     function() { 
      //OnComplete handler 
     } 
    ); 
} 
+0

這是行不通的?我可能需要儘快在Dynamics CRM中使用Google圖表。 – Zec