Google Charts Javascript Literal数据符号表示"不是数组"错误?

时间:2014-04-03 08:53:18

标签: javascript charts google-visualization syntax-error

将Javascript Literal数据与Google Charts arrayToTable函数结合使用时,会在Javascript中返回错误,该错误在Modernizr-3.5.2.js文件中指出“Not a array” 433 - > ret = !!(window.WebGLRenderingContext && (canvas.getContext('experimental-webgl') || canvas.getContext('webgl')));

任何想法为什么?数据对象是以谷歌图表API文档状态生成的。

生成图表的代码如下:

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

    function StackedColumnChart(ChartDivContainerID) { 

    var ChartData1 = {
            cols: [{ id: 'Genre', label: 'Genre', type: 'string' },
                   { id: 'Fantasy & Sci Fi', label: 'Fantasy & Sci Fi', type: 'number' },
                   { id: 'Romance', label: 'Romance', type: 'number' },
                   { id: 'Mystery/Crime', label: 'Mystery/Crime', type: 'number' },
                   { id: 'General', label: 'General', type: 'number' },
                   { id: 'Western', label: 'Western', type: 'number' },
                   { id: 'Literature', label: 'Literature', type: 'number' }
            ],
            rows: [{ c: [{ v: '2010' }, { v: 10 }, { v: 24 }, { v: 20 }, { v: 32 }, { v: 18 }, { v: 5 }] },
                   { c: [{ v: '2020' }, { v: 16 }, { v: 22 }, { v: 23 }, { v: 30 }, { v: 16 }, { v: 9 }] },
                   { c: [{ v: '2030' }, { v: 28 }, { v: 19 }, { v: 29 }, { v: 30 }, { v: 12 }, { v: 13 }] }
            ]
        }

        var data = google.visualization.arrayToDataTable(ChartData1);

        var options = {
            width: 600,
            height: 400,
            legend: { position: 'top', maxLines: 3 },
            bar: { groupWidth: '75%' },
            isStacked: true,
        };

        var chart = new google.visualization.ColumnChart(document.getElementById(ChartDivContainerID));
        chart.draw(data, options);
    }

1 个答案:

答案 0 :(得分:3)

您的数据对象是正确的,但您正在调用

var data = google.visualization.arrayToDataTable(ChartData1);

而不是

var data = new google.visualization.DataTable(ChartData1);