Google Charts Javascript 文字数据表示法给出“Not a array”错误

Google Charts Javascript Literal data notation gives "Not an array" error?

本文关键字:Not array 错误 Javascript Charts 文字 数据表示 Google      更新时间:2023-09-26

当将Javascript文字数据与Google Charts arrayToTable函数结合使用时,它会在Javascript中返回一个错误,该错误在Modernizr-3.5.2.js文件中指出"不是数组"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);
    }

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

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

而不是

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