谷歌折线图:可视化库。未捕获的TypeError:无法调用未定义的方法'arrayToDataTable'

时间:2014-03-07 20:55:24

标签: javascript google-visualization

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

<script type="text/javascript">
google.load("visualization", "1", {packages: ["corechart"]});
google.setOnLoadCallback(drawBarChart);

function drawBarChart() {
    var data = google.visualization.arrayToDataTable([
    ['Subject Name', 'Correct', 'Wrong'],
    ['Four-digit Numbers', 1, 1],
    ['Substraction with Borrowing', 1, 0],
    ['Multiplication', 1, 0],
    ['Measures of Mass', 1, 0],
]);

    var view = new google.visualization.DataView(data);
    view.setColumns([0, 1,
                        { calc: "stringify",
                          sourceColumn: 1,
                          type: "string",
                          role: "annotation"
                        },
                        2,
                        { calc: "stringify",
                          sourceColumn: 2,
                          type: "string",
                          role: "annotation" }
    ]);


    var options = {
        title: 'No. of Right & Wrong Questions',
        isStacked: true,
        legend: { position: 'top',alignment :'end' },
        chartArea: {width: '85%'},
        series: [
            {color:'#199EAF'},
            {color:'#BD2B53'}
        ]

    };

    var chart = new google.visualization.ColumnChart(document.getElementById("bar_1"));
    chart.draw(view, options);
}

我收到错误Uncaught TypeError:无法调用未定义的方法'arrayToDataTable'。 看起来可视化包未初始化。我在这做错了什么?

1 个答案:

答案 0 :(得分:1)

由于我无法测试这个我不确定,但我确实看到你发布的语法错误:

function drawBarChart() {
    var data = google.visualization.arrayToDataTable([
    ['Subject Name', 'Correct', 'Wrong'],
    ['Four-digit Numbers', 1, 1],
    ['Substraction with Borrowing', 1, 0],
    ['Multiplication', 1, 0],
    ['Measures of Mass', 1, 0],
]);

最后一项之后应该没有逗号:

function drawBarChart() {
    var data = google.visualization.arrayToDataTable([
    ['Subject Name', 'Correct', 'Wrong'],
    ['Four-digit Numbers', 1, 1],
    ['Substraction with Borrowing', 1, 0],
    ['Multiplication', 1, 0],
    ['Measures of Mass', 1, 0]
]);