繁体   English   中英

数据表未填充-Google图表

[英]Data table not being populated - google charts

我正在尝试使用Google图表和一些动态生成的JSON绘制图形和表格。 我已经更改了输出以尝试满足Google的要求,但我仍在努力使其正常工作。 我花了无数小时试图解决这个问题,但是我终于碰到了墙。 我提供了生成的JSON的示例。

我在图表应有的地方留了一个空白,并在其中有一个可见的标题表,其中没有数据(但行数大致正确)。

<!DOCTYPE html>
<head>
<title>Nation stats analyser</title>
        <!--Load the AJAX API-->
    <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
    <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <script type="text/javascript">

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

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

    function drawChartAndTable() {
    var jsonData = $.ajax({
        url: "data.php",
        dataType: "json",
        }).done(function (jsonData) {
            var data = new google.visualization.DataTable();
            data.addColumn('string', 'Category');
            data.addColumn('number', 'Value');

            Object.keys(jsonData).forEach(function (row) {
                data.addRow([
                    row.Category
                    row.Value
                ]);
            });
            var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
            chart.draw(data, {width: 400, height: 240});

            var table = new google.visualization.Table(document.getElementById('table_div'));
            table.draw(data, {showRowNumber: true, width: '100%', height: '100%'});
        });
    }

    setTimeout(drawChartAndTable(), 100);


    </script>
</head>
<body>
<div id="chart_div"></div>
<div id="table_div"></div>
</body>

[{"Category":"Anarchy","Value":317},
{"Category":"Capitalizt","Value":108},{"Category":"New York Times 
Democracy","Value":918},{"Category":"Benevolent 
Dictatorship","Value":44},{"Category":"Inoffensive Centrist   
Democracy","Value":1993}]

尝试这个...

        jsonData.forEach(function (row) {
            data.addRow([
                row.Category,
                row.Value
            ]);
        });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM