Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error "Row given with size different than 2" when using Google Visualization

I have a template that uses jQuery/AJAX to pull some data. Getting the data isn't an issue, but I can't seem to use it in a way that GoogleVis wants. The line causing problems for me is mapdata.addRows([gapidata]);. It seems there's an issue with the array but I'm not sure how to fix it. Any help is greatly appreciated!

The jQuery/AJAX I'm using:

        var gapidata = new Array();
        $.ajax({
          url: "inc/index.gapi.inc.php",
          cache: false,
        dataType: "text",
          success: function(html){
            gapidata = html;
          }
        });

The data I get from the AJAX call:

    ['Korea, Republic of', 50],['Japan', 38]

The code I use to display the data:

        // Geo Map Chart
        var mapWidth = Math.round(((screenWidth / 12) * 10) * 0.8);
        var mapHeight = Math.round(mapWidth * 0.5);

        $('#dashboard-visit-map').width(mapWidth*1.1);
        $('#dashboard-visit-map').height(mapHeight*1.1);
        var mapdata = new google.visualization.DataTable();
        mapdata.addColumn('string','Country');
        mapdata.addColumn('number','IPs Listed');
        mapdata.addRows([gapidata]);

        var geochart = new google.visualization.GeoChart(document.getElementById('dashboard-visit-map'));
        geochart.draw(mapdata, {width: mapWidth, height: mapHeight,backgroundColor: { fill:'transparent' }});

Currently the map doesn't populate with any data. If I remove the brackets around gapidata I receive the following error on my JavaScript console:

    Uncaught Error: Row given with size different than 2 (the number of columns in the table).
like image 838
Krys Avatar asked Jan 18 '26 21:01

Krys


1 Answers

I just normalized my data frame before calling the rendering function; it worked fine for me.

Normalizes data frame:

data <- as.data.frame.matrix(data)
like image 115
David Avatar answered Jan 20 '26 12:01

David