2013-12-10 66 views

回答

1

你需要從那裏移動drawChart函數內部的AJAX調用和填充數據。這裏有一種方法:

function drawChart() { 
    $.ajax({ 
     type: "GET", 
     dataType: "xml", 
     async: true, 
     url: "https://test/computers", 
     contentType: "text/xml; charset=UTF-8", 
     success: function (xml) { 
      var size = $(xml).find("size"); 
      var total = (size.text()); 

      var data = google.visualization.arrayToDataTable([ 
       ['Task', 'Hours per Day'], 
       ['Work', total], 
       ['Eat', 2], 
       ['Commute', 2], 
       ['Watch TV', 2], 
       ['Sleep', 7] 
      ]); 

      var options = { 
       title: '' 
      }; 

      var chart = new google.visualization.PieChart(document.getElementById('piechart')); 
      chart.draw(data, options); 
     } 
    }); 
} 
google.load("visualization", "1", {packages:["corechart"], callback: drawChart}); 
+0

真棒謝謝你,除了當變量是第一,但我會解決它。 – user1938745

+0

將XML解析爲一個數字:'var total = new Number(size.text());' – asgallant

+0

太棒了!我做了一個微小的修改刪除'新',只是使用數字。非常感謝。 – user1938745