Vishvendu Palawat
Vishvendu Palawat

Reputation: 576

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 ?

Upvotes: 2

Views: 8929

Answers (2)

tyne
tyne

Reputation: 115

var dataTable = new google.visualization.DataTable();
dataTable.addColumn('string','Lost Reason');
dataTable.addColumn('number','Value');
dataTable.addRows([
  [lostReason[0], parseInt(lostReasonVal[0])],
  [lostReason[1], parseInt(lostReasonVal[1])],
  [lostReason[2], parseInt(lostReasonVal[2])],
  [lostReason[3], parseInt(lostReasonVal[3])],
]);

console.log('dataTable=' + JSON.stringify(dataTable));

var options = {
    title: 'Lost Reason',
    width: 500,
    height: 400,
    backgroundColor: { fill: '#cac9c9' }
};

Upvotes: 0

nbering
nbering

Reputation: 2800

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.

Upvotes: 7

Related Questions