2011-07-22 62 views
0

我正在使用Google chart tool來編寫條形圖。我的數據在哪裏?

這裏是我的代碼:

function drawBar(){ 
    var data2 = new google.visualization.DataTable(); 
    data2.addColumn('string', 'Project'); 
    data2.addColumn('number', '2011 Fiscal Spending'); 
    data2.addRows(5); 


    // Gather Data 

    $.getJSON("/Organizations/TopFiveSpending",{orgName : "Office of Technology"},function(data){ 
     $(data).each(function(i,e){ 
          //data2 is populating fine here when i debug 
          data2.setValue(i,0,e.ProjectName); 
          data2.setValue(i,1,e.TotalFYSpending);   
      }) 
     }); 


    barGraph = new google.visualization.ColumnChart(document.getElementById('portfolio-total-spending')); 

    var options = {width: 250, height: 190, legend: 'none', 
         hAxis: {title: 'Project', titleTextStyle: {color: 'red'}} 
        }; 
    //data2 has null data here 
    barGraph.draw(data2,options); 
    google.visualization.events.addListener(barGraph, 'onmouseover', function(e) { 
    console.log('barGraph hover '+e['row']); 
    table.setSelection([{row:e['row']}]); 
    }); 


    } 

我的圖表繪製從來沒有任何東西,我不會在我的控制檯得到任何錯誤。當我調試代碼時,我不是那個data2正在填充正確的數據。但是,當調用barGraph.draw()函數時,數據不見了。我在代碼中發表了這方面的評論。任何想法如何解決這個問題?

回答

1

您可以嘗試使用JSON回調函數內的數據填充圖形,可能會在將data2對象傳遞給圖形函數時尚未填充該圖形函數。

+0

看起來像這樣做。謝謝! – ZeroDivide