2014-02-27 101 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