简体   繁体   中英

google visualization - Incompatible data table: Error: Table contains more columns than expected

I'm having an issue with creating a GeoChart. My code looks like this:

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

The above code yields a red error box where the chart should be that reads:

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

I've verified that all the lists in the rows list do have 4 members (lat, long, desc, value). Any idea why it's saying that it's expecting only 3 columns?

So I've determined the issue, and there are several things wrong with my initially posted code. First, lat / long should be number column types, and the rows need to format the lat and long as numbers (parseFloat worked for this).

The proper code looked like:

//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(...);

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