1

Using the google embed api and datatables to visualize my analytics. The problem is the columns don't resort correctly -- that is everything is treated as a string. I have this code:

gapi.client.analytics.data.ga.get(queryObj1).execute(function(results){
    var myTable = new google.visualization.DataTable(results.dataTable);
    ....

This creates the myTable correctly and when I debug I see that the columns created have the correct type -- string or number. However when I check the values in results.dataTable (the object returned by the query) I notice that everything is a string -- as one might expect from an ajax call.

I found this thread about rewriting the sort function, but that seems a bit complex to me and if anything went wrong I'm not sure I'd be able to figure it out.

My approach is to iterate through the datatable and convert all the number columns to actual numbers.

function makeNumbers(results){

    // make numbers actually numbers so they will sort properly
    for(var c in results.cols){
        // check if the column is a number type
        if(results.cols[c].type === "number"){
            // fix the values for that column is all the rows
            for(var r in results.rows){
                results.rows[r].c[c].v = +results.rows[r].c[c].v
            }
        }   
    }
    return results;
}

This seems to work great and to my mind is much simpler than changing the sorting function.

Can anyone see a problem with this? Or a better way to do it?

Community
  • 1
  • 1
Rothrock
  • 1,413
  • 2
  • 16
  • 39

0 Answers0