TypeError:google.visualization未定义

时间:2014-02-09 13:35:14

标签: javascript google-visualization

我正在尝试使用Google图表API,但是我收到了一个我无法理解的错误:

这是我的代码:

<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript" src="js/statistics/overview.js"></script>

来自overview.js的代码是这样的:

var statisticsOverview = {
    init: function() {
        google.load("visualization", "1", {packages: ["corechart"]});
        google.setOnLoadCallback(this.drawIncomeChart());
        google.setOnLoadCallback(this.drawExpensesChart());
        google.setOnLoadCallback(this.drawEconomiesChart());
    },
    drawIncomeChart: function() {
        var data = [
            ['Year', 'Income'],
            ['October 2013', 1000],
            ['January 2014', 1170],
            ['March 2014', 660]
        ];
        var options = {
            title: 'Income Performance',
            colors: ['green']
        };
        var id = 'chart_income';

        this.drawLineChart(data, options, id);
    },
    drawExpensesChart: function() {
        var data = [
            ['Year', 'Expense'],
            ['October 2013', 1000],
            ['January 2014', 1170],
            ['March 2014', 660]
        ];
        var options = {
            title: 'Expense Performance',
            colors: ['red']
        };
        var id = 'chart_expenses';
        this.drawLineChart(data, options, id);
    },
    drawEconomiesChart: function() {
        var data = [
            ['Year', 'Savings'],
            ['2004', 1000],
            ['2005', 1170],
            ['2006', 660],
            ['2007', 1030]
        ];

        var options = {
            title: 'Savings Performance',
            colors: ['orange']
        };

        var id = 'chart_economies';
        this.drawLineChart(data, options, id);
    },
    drawLineChart: function(data, options, id) {
        console.log(google.visualization);
        var chartData = google.visualization.arrayToDataTable(data);

        var chart = new google.visualization.LineChart(document.getElementById(id));
        chart.draw(chartData, options);
    }
};

statisticsOverview.init();

这很奇怪,因为console.log()会给我带有属性可视化的google对象。我做错了什么?

2 个答案:

答案 0 :(得分:22)

时间问题。通常你会像谷歌一样调用

  google.load("visualization", "1", {packages:["corechart"]});
  google.setOnLoadCallback(drawChart);
  function drawChart() {
  ...
  }

因此,当加载可视化包时,将调用绘制函数。

现在,在你的情况下,一切都在以后完成,所以你必须这样做:

google.load("visualization", "1", {packages: ["corechart"]});

function doStats() {

var statisticsOverview = {
    init: function() {
        console.log('init');
        this.drawIncomeChart();
        this.drawExpensesChart();
        this.drawEconomiesChart();
    },
    ...
};    

statisticsOverview.init()
}

google.setOnLoadCallback(doStats);

可以使用

获得类似的结果
setTimeout(function() {
    statisticsOverview.init();
}, 3000);

没有包装功能。

答案 1 :(得分:0)

试试这种方式。这对我来说很好用

    var timeout;
    google.charts.load('current', { 'packages': ['corechart'] });
    $(document).ready(function(){
       timeout = setInterval(function () {
          if (google.visualization != undefined) {
             drawChart();
             clearInterval(timeout);
          }
       }, 300);
    });

    function drawChart(range, type) {
       var data = new google.visualization.DataTable();
    .....
    }