google.visualization.DataTable,日期未定义

时间:2013-08-17 18:51:47

标签: javascript callback google-visualization

请考虑以下代码:

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

function drawChart() {
    var data = google.visualization.DataTable();
    data.addColumn('datetime', 'Time');
    data.addColumn('number', 'Leads');
    data.addRow([Date.parse("2013-08-17 10:12:18"), 0]);
    data.addRow([Date.parse("2013-08-17 09:13:42"), 14]);
    data.addRow([Date.parse("2013-08-17 10:00:05"), 15]);
    data.addRow([Date.parse("2013-08-17 11:12:18"), 3]);
    var options = {
        title: 'Company Performance',
        fontSize: '12px',
        curveType: 'function',
        pointSize: 5,
        hAxis: {
            title: 'Daily Report',
            titleTextStyle: {
                color: '#FF0000'
            }
        },
        vAxis: {
            title: 'Leads',
            titleTextStyle: {
                color: '#FF0000'
            }
        }
    };

    var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
    chart.draw(data, options);
}

现在,上面的代码被要求加载谷歌图表api,定义了DataTable&生成一个LineChart图,但它没有。相反,我在尝试添加列时出现以下错误

TypeError: data is undefined #at this line --> data.addColumn('datetime', 'Time')

谁能告诉我我错过了什么?

1 个答案:

答案 0 :(得分:2)

在创建new的新对象时没有使用google.visualization.Datatable

 function drawChart() {
    // var data = google.visualization.DataTable(); missed new keyword
    var data = new google.visualization.DataTable();
 //...
 }