Search code examples
javascriptjqueryarrayschartsgoogle-visualization

Passing Array Into Google Chart


I have some records for that I am using three arrays which contains dynamic values.

var Results=new Array();
var Second=new Array();
var First=new Array();

I want to show these array values in Google chart but as per Google chart they shows array values differently.

How to add my arrays into Google Chart ?


Solution

  • If they are all the same length, you can try something like this:

    var Combined = new Array();
    Combined[0] = ['Results', 'First', 'Second'];
    for (var i = 0; i < Results.length; i++){
      Combined[i + 1] = [ Results[i], First[i], Second[i] ];
    }
    //second parameter is false because first row is headers, not data.
    var table = google.visualization.arrayToDataTable(Combined, false);
    

    See here for documentation on array to DataTable.