不兼容的数据表: 错误:表包含的列数超过预期

Incompatible data table: Error: Table contains more columns than expected

本文关键字:包含 数据表 错误 不兼容      更新时间:2023-09-26

我在创建GeoChart时遇到问题。 我的代码如下所示:

var table = new google.visualization.DataTable();
//set the columns for the table
table.addColumn('string', 'Latitude');
table.addColumn('string', 'Longitude');
table.addColumn({
    type: 'string',
    role: 'tooltip'
});
table.addColumn('number', 'Music Consumers');
var rows = [
    ["42.651445", "-73.755254", "Albany-Schenectady-Troy NY", 100],
    ["35.2225", "-80.837539", "Charlotte NC", 106],
    ...
];
table.addRows(rows);

上面的代码生成一个红色错误框,其中图表应为:

Incompatible data table: Error: Table contains more columns than expected (Expecting 3 columns)

我已经验证了行列表中的所有列表确实有 4 个成员(纬度、经度、描述、值)。 知道为什么它说它只期待 3 列吗?

所以我已经确定了问题,我最初发布的代码有几个问题。 首先,lat/long 应该是数字列类型,行需要将 lat 和 long 格式化为数字(parseFloat 为此工作)。

正确的代码如下所示:

//set the columns for the table
table.addColumn('number', 'LATITUDE', 'Latitude');
table.addColumn('number', 'LONGITUDE', 'Longitude');
table.addColumn('string', 'DESCRIPTION', 'Description');
table.addColumn('number', 'VALUE', 'Music Consumers');
var rows = [
    [42.651445, -73.755254, "Albany-Schenectady-Troy NY", 100],
    [35.2225, -80.837539, "Charlotte NC", 106],
    ...
];
table.addRows(rows);
var geochart = new google.visualization.GeoChart($mapContainer[0]);
geochart.draw(...);
相关文章: