Google图表错误:给定的每一行都必须为null或数组

时间:2018-10-24 15:06:43

标签: google-visualization

我正在一个旨在显示一些基本Google图表的网站上工作。数据来自我通过Ajax检索的文本文件。它有一个x,y值和一个注释字段。数据如下:

[[-0.8, -0.47, "100-005-10"],
[-0.7, -0.46, "100-005-9"],
[-0.6, -0.45, "100-005-8"],
[-0.5, -0.44, "100-005-7"]]

这是我的代码:

    <script >


  var xmlhttp = new XMLHttpRequest();
  var array;
  xmlhttp.onreadystatechange = function() {

      array = this.responseText;    

  };
  xmlhttp.open("GET", "array.array", true);
  xmlhttp.send(); 


    google.charts.load("current", {packages:["corechart"]});
    google.charts.setOnLoadCallback(drawChart);

    function drawChart() {

        var data = google.visualization.arrayToDataTable($.parseJSON(array), true)

        data.addColumn('number', 'x');
        data.addColumn('number', 'y');
        data.addColumn({type: 'string', role: 'annotation'});

        data.addRow(array);


      var options = {
        legend: 'none',
        colors: ['#087037'],        
        selectionMode: 'single',       
        tooltip: {trigger: 'selection'},
        pointSize: 12,
        animation: {
          duration: 200,
          easing: 'inAndOut',
        }
      };

      var chart = new google.visualization.ScatterChart(document.getElementById('animatedshapes_div'));

      chart.draw(data, options);


    }
  </script>

运行代码时,出现以下错误消息:

  

错误:如果将参数赋给addRow,则该参数必须为数组,或者为空

我只是不知道如何将纯文本从ajax响应转换为数组。

1 个答案:

答案 0 :(得分:0)

尝试使用import scipy as sp data = sp.genfromtxt(r"C:\Users\DELL INSPIRON N3542\Downloads/1400OS_Code/1400OS_01_Codes/data/web_traffic.tsv" , "\t") print(data[:24]) [[ 1 2272] [ 2 -1] [ 3 1386] [ 4 1365] [ 5 1488] [ 6 1337] ... and the original Data set looks like that 1 2272 2 nan 3 1386 4 1365 5 1488 6 1337 ... 将字符串转换为实际数组...

JSON.parse