使用ajax xml变量填充Google Charts

时间:2013-12-10 01:53:34

标签: javascript jquery ajax google-visualization

如何在我的xmlParser函数中获取我的'total'变量,以显示为Google Charts的变量。

Fiddle

1 个答案:

答案 0 :(得分: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});