How to dynamically add a row to a Google Chart with a for loop

I have a problem in google graphics.

I am using asp.net mvc. After I receive data from the controller, I pass it to the Google chart.

when i use manual data like

when i use this `

['Month', 'Bolivia', 'Ecuador', 'Madagascar', 'Papua New Guinea', 'Rwanda', 'Average'], ['2004/05', 165, 938, 522, 998, 450, 614.6], 

Show correctly.

but how can I dynamically add data to a google chart

this is my code

  success: function (chartsdata) { debugger; for (var i = 0; i < chartsdata.length; i++) { var data = google.visualization.arrayToDataTable([ ['Element', 'Density', { role: 'style' }], [chartsdata[i].MonthValue , chartsdata[i].CountValue, '#b87333'] ]); debugger; var options = { title: '', }; var chart = new google.visualization.ComboChart(document.getElementById('chartdiv')); chart.draw(data, options); } 
+5
source share
2 answers

The way you do is wrong. You should do something like below:

  var data=[]; var Header= ['Element', 'Density', { role: 'style' }]; data.push(Header); for (var i = 0; i < chartsdata.length; i++) { var temp=[]; temp.push(chartsdata[i].MonthValue); temp.push(chartsdata[i].CountValue); data.push(temp); } var chartdata = new google.visualization.arrayToDataTable(data); 
+5
source

you can use the " google.visualization.DataTable() " and " addRow() " google code

 var data = new google.visualization.DataTable(); data.addColumn('string', 'Month'); data.addColumn('number', 'Count'); for (var i = 0; i < chartsdata.length; i++) { data.addRow([chartsdata[i].month, chartsdata[i].count]); } 
+3
source

Source: https://habr.com/ru/post/1211974/


All Articles