简体   繁体   中英

google.visualization.DataTable, date is undefined

Consider the following code:

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);
}

Now, the above code is suppsed to load google chart api, defined a DataTable & generate a LineChart graph but it doesn't. Instead, I the following error when trying to add Columns

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

Can anyone tell me what am I missing?

you did not use new while creating a new object of google.visualization.Datatable .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM