2

我有以下用于 LineChart 的 JavaScript,它使用 JSHint 签出但拒绝显示:

// Load the LineChart charting package
google.load("visualization", "1", {
  packages: ["corechart", "line", "LineChart"]
});
// Set a callback to run when the Google Visualization API is loaded.
google.setOnLoadCallback(drawChart);

// Callback that creates and populates a data table,
// instantiates the line chart, passes in the data and
// draws it.
function drawChart() {
  var data = google.visualization.DataTable();
  data.addColumn('date', 'Date');
  data.addColumn('number', 'Sales');
  data.addrows([
    [new Date(2015, 2, 30), 419.5],
    [new Date(2015, 2, 31), 497.51],
    [new Date(2015, 3, 1), 1465.85],
    [new Date(2015, 3, 2), 2594.71],
    [new Date(2015, 3, 4), 2620.7],
    [new Date(2015, 3, 5), 3189.86],
    [new Date(2015, 3, 6), 4172.96],
    [new Date(2015, 3, 7), 4332.96],
    [new Date(2015, 3, 8), 4653.03],
    [new Date(2015, 3, 9), 4678.98],
    [new Date(2015, 3, 12), 5626.48],
    [new Date(2015, 3, 13), 9779.28],
    [new Date(2015, 3, 14), 10428.3],
    [new Date(2015, 3, 15), 10647.18],
    [new Date(2015, 3, 17), 10815.58],
    [new Date(2015, 3, 20), 11471.58],
    [new Date(2015, 3, 21), 11875.57],
    [new Date(2015, 3, 22), 12052.07],
    [new Date(2015, 3, 23), 13461.14],
    [new Date(2015, 3, 24), 14072.78],
    [new Date(2015, 3, 25), 14199.78],
    [new Date(2015, 3, 27), 14320.28],
    [new Date(2015, 4, 27), 100000]
  ]);
  var options = {};
  // Instantiate and draw our chart, passing in some options.
  var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
  chart.draw(data, options);
}
<script type="text/javascript" src="https://www.google.com/jsapi?autoload={'modules':[{'name':'visualization','version':'1.1','packages':['corechart', 'timeline','LineChart']}]}">
</script>
<div id="chart_div"></div>

那么,我做错了什么?我尝试了几种日期构造方法,包括:

    [new Date('2015-3-30'), 419.5],
4

1 回答 1

0

看起来您new在实例化之前缺少关键字DataTable,这可以看作是控制台中的错误消息。此外,添加行时有一个错字。它应该addRows代替addrows.

尝试改变

var data = google.visualization.DataTable();
  // ...
data.addrows([
  // ...

var data = new google.visualization.DataTable();
  // ...
data.addRows([
  // ...
于 2015-04-29T16:54:29.033 回答