Google Visualization - 第0行的行类型无效

时间:2014-01-24 13:17:10

标签: javascript php google-visualization

我必须使用谷歌可视化在饼图上可视化url json数据。我的代码似乎是为了这个目的,但我在控制台中得到了“第0行的无效行类型”错误。数据格式有问题吗?如果有人可以提供帮助,那将非常感激。这是我的代码:

PHP:

<?php
    $json = file_get_contents('http://data.police.uk/api/crimes-street/all-crime?lat=52.629729&lng=-1.131592&date=2013-01'); 
    $json = str_replace("\xe2\x80\xa8", '\\u2028', $json);
    $json = str_replace("\xe2\x80\xa9", '\\u2029', $json);
    echo $json;
?>

JavaScript的:

<script type="text/javascript" src="https://www.google.com/jsapi"></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.load('visualization', '1', {'packages':['corechart']});

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

function drawChart() {
  var jsonData = $.ajax({
      url: "getData.php",
      dataType:"json",
      async: false
      }).responseText;

  //Create an array of the JSON data and then create our data table out of JSON data loaded from server.
  var array = JSON.parse(jsonData);
  var dataTableData = new google.visualization.arrayToDataTable(array);
  var table = google.visualization.DataTable(dataTableData);

  var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
  chart.draw(data, {width: 400, height: 240});
}

</script>
</head>

<body>
<div id="chart_div"></div>
</body>

谢谢。

1 个答案:

答案 0 :(得分:3)

  1. JSON不代表可以转换为 2维数组的数据 google.visualization.DataTable根本无法解释这一点。每个项目都包含复杂的子项目,例如locationoutcome_status

  2. 必须包含列 - arrayToDataTable is very specific about that

  3. 最重要的是:您如何将这些数据想象成一个饼图?第一级只有一个特定的数字字段location_subtype。您无法根据各种字符串生成饼图。

  4. 但是,您可以清理数据并将其显示为google.visualization.Table

    首先使用一些列创建一个DataTable:

    var dataTable = new google.visualization.DataTable();
    dataTable.addColumn('string','category');
    dataTable.addColumn('string','context');
    dataTable.addColumn('string','id');
    dataTable.addColumn('number','location_subtype');
    dataTable.addColumn('string','location_type');
    dataTable.addColumn('string','month');
    dataTable.addColumn('string','persistent_id');
    

    将您的jsonData转换为JSON

    var json=JSON.parse(jsonData);
    

    清理并插入数据。在这里,我只删除locationoutcome_status子项,但您可能希望从这些子项中提取值并将其作为列插入dataTable

    for (var i=0;i<json.length;i++) {
       delete json[i].location;
       delete json[i].outcome_status;
       var row = [];        
       for (var item in json[i]) {
         row.push(json[i][item]);
       }        
       dataTable.addRow(row);
    }
    

    最后创建Table():

    var chart = new google.visualization.Table(document.getElementById('chart_div'));
    chart.draw(dataTable, {width: 1000, height: 300});
    

    结果将如下所示:

    enter image description here


    <强>更新

    var dataTable = new google.visualization.DataTable();
    dataTable.addColumn('string','category');
    dataTable.addColumn('string','context');
    dataTable.addColumn('number','id');
    dataTable.addColumn('string','location_subtype');
    dataTable.addColumn('string','location_type');
    dataTable.addColumn('string','month');
    dataTable.addColumn('string','persistent_id');
    dataTable.addColumn('string','street name');
    dataTable.addColumn('string','outcome status');
    
    json=JSON.parse(jsonData);
    
    for (var i=0;i<json.length;i++) {
        var row = [];       
        row.push(json[i].category);
        row.push(json[i].context);
        row.push(json[i].id);
        row.push(json[i].location_subtype);
        row.push(json[i].location_type);
        row.push(json[i].month);
        row.push(json[i].persistent_id);
        row.push(json[i].location.street.name);
        row.push(json[i].outcome_status ? json[i].outcome_status.category : "null");
        dataTable.addRow(row);
    }