Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How To dynamically Add row To Google Chart with for loop

I Have Problem in Google chart.

I am using asp.net Mvc. After I fetch data from controller, I pass it into Google chart .

When i use manual data, for example:

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

It Show me correctly .

But how can i add my data dynamically into Google chart ?

This is my code:

for (var i = 0; i < chartsdata.length; i++) {

    var data = google.visualization.arrayToDataTable([
        ['Element', 'Density', {role: 'style'}],
        [chartsdata[i].MonthValue, chartsdata[i].CountValue, '#b87333']
    ]);

    var options = {
        title: ''
    };

    var chart = new google.visualization.ComboChart(document.getElementById('chartdiv'));
    chart.draw(data, options);
}
like image 387
user3087357 Avatar asked Jan 27 '15 07:01

user3087357


1 Answers

you can use "google.visualization.DataTable()" and its "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]);
}
like image 112
Manoj De Mel Avatar answered Nov 01 '22 12:11

Manoj De Mel