How to add the data from html tag on "Google Chart" arrayToDataTable? using javascript

989 views Asked by At

the td id='data' has the value below

['car', 7],['jeep', 2],['taxi', 1]

I want to get the value of 'td tag' and put on "Google Chart" arrayToDataTable

i tried this but not working

var x = document.getElementById('data').innerHTML; var data = google.visualization.arrayToDataTable([ ['Data', 'Value'], x
]);

Can anyone tell me how to do it.... need help

1

There are 1 answers

3
4nti On BEST ANSWER

you need to add brackets and make an array out of the string (innerHTML / innerTEXT) that you get from the TD and then add those rows.

Be sure the TD is inside TR and that is inside TABLE, or use a DIV instead of a TD

var data = new google.visualization.DataTable();
data.addColumn('string', 'Cartype');
data.addColumn('number', 'Amount');

//var tdString = "['car', 7],['jeep', 2],['taxi', 1]";
//var tdString = document.getElementById('data').textContent;
var tdString = document.getElementById('data').innerHTML;

var arr = eval("[" + tdString + "]");

data.addRows(arr);

Anyway: working fiddle here : https://jsfiddle.net/L3tNE/15/