Google Charts API:无法读取未定义的属性'toArrays'

时间:2016-07-08 19:59:33

标签: javascript charts google-visualization

尝试使用此代码从csv文件创建Google Charts。这是我发现在大多数教程中使用的代码。然而,似乎存在一些问题,因此我无法正确执行它。

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

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

      // Callback that creates and populates a data table,
      // instantiates the pie chart, passes in the data and
      // draws it.
      function drawChart() {
   // grab the CSV
      $.get("Chart1-data.csv", function(csvString) {
      // transform the CSV string into a 2-dimensional array
      var arrayData = $.csv.toArrays(csvString, {onParseValue: $.csv.hooks.castToScalar});

      // this new DataTable object holds all the data
      var data = new google.visualization.arrayToDataTable(arrayData);

      // this view can select a subset of the data at a time
      var view = new google.visualization.DataView(data);
      view.setColumns([0,1]);

     // set chart options
     var options = {
        title: "A Chart from a CSV!",
        hAxis: {title: data.getColumnLabel(0), minValue: data.getColumnRange(0).min, maxValue: data.getColumnRange(0).max},
        vAxis: {title: data.getColumnLabel(1), minValue: data.getColumnRange(1).min, maxValue: data.getColumnRange(1).max},
        legend: 'none'
     };

     // create the chart object and draw it
     var chart = new google.visualization.ScatterChart(document.getElementById('chart_div'));
     chart.draw(view, options);
  });
}

我要按照教程进行操作。但是在网络服务器上运行它会给我以下错误:

Uncaught TypeError: Cannot read property 'toArrays' of undefined

如果有人知道原因,请告诉我发生了什么以及解决此错误。

1 个答案:

答案 0 :(得分:2)

感谢@ebyt指出这一点。

放置

<script src="jquery-csv.js"></script>

初始化jquery并完美运行后。