12

I want to create a Column Chart , using Google Visualization API , I can send the data to populate the chart's DataTable in array form. But I need to generate the chart with variable number of columns/rows , depending on what my arrays contain and i don`t know how to correctly iterate them and add them to the DataTable.

Here is an example for parsing STATIC data to generate the chart : (all this is in javascript)

var data = google.visualization.arrayToDataTable([
      ['Year', 'Sales', 'Expenses'],
      ['2004',  1000,      400],
      ['2005',  1170,      460],
      ['2006',  660,       1120],
      ['2007',  1030,      540]
    ]);

The API has these methods for adding columns and rows : - different method for obtaining the same data as above :

var data = new google.visualization.DataTable();
data.addColumn('string', 'Year');
data.addColumn('number', 'Sales');
data.addColumn('number', 'Expenses');
data.addRows([  ['2004', 1000 , 400], ['2005', 1170, 460], ['2006', 660, 1120], ['2007',1030,540]
  ]);

What i need is a for loop or a double for loop to iterate the arraylists that i send and dynamically add rows content.

To be more precise let`s say in one case i would have the data written above , and in other case i would have this :

['Year', 'Sales', 'Expenses' , 'Other'],
['2004',  1000,      400     ,  232   ],
['2005',  1170,      460    ,  421   ],
['2006',  660,       1120    ,  4324  ],
['2007',  1030,      540     ,  4234  ],
['2008',  1530,      50     ,    234  ],

so i would parse these data through parameters in the function let's say ( i don't know if this would be the right ideea) many arraylists containing each row : Row1=['2004', 1000, 400 , 232 ] Row2=['2005', 1170, 460 , 421 ] and ....

How can i use a for loop , or a double for loop , to iterate the arraylists that i am sending to dynamic generate the datatable (with variable numbers of rows and column ) containing the data ?

4

3 に答える 3

25

これがjsfiddleの実用的なソリューションです。

次の関数を見てください。これにより、データの配列が繰り返され、グラフが更新されます。

// function to update the chart with new data.
      function updateChart() {

          dataTable = new google.visualization.DataTable();

          var newData = [['Year', 'Sales', 'Expenses' , 'Other'],
            ['2004',  1000,      400     ,  232   ],
            ['2005',  1170,      460    ,  421   ],
            ['2006',  660,       1120    ,  4324  ],
            ['2007',  1030,      540     ,  4234  ],
            ['2008',  1530,      50     ,    234  ]];

          // determine the number of rows and columns.
          var numRows = newData.length;
          var numCols = newData[0].length;

          // in this case the first column is of type 'string'.
          dataTable.addColumn('string', newData[0][0]);

          // all other columns are of type 'number'.
          for (var i = 1; i < numCols; i++)
            dataTable.addColumn('number', newData[0][i]);           

          // now add the rows.
          for (var i = 1; i < numRows; i++)
            dataTable.addRow(newData[i]);            

          // redraw the chart.
          chart.draw(dataTable, options);        

      }
于 2012-09-14T00:26:58.790 に答える
1
var obj = JSON.parse(r.d);//Json data will come from any web service or any other source
 var data2 = new google.visualization.DataTable();    
 //To Add Column Dynamically

    for (var j = 0; j < array.length; j++) // this array contains columns
     {
      if (j == 0)
      {
          data2.addColumn(typeof (array[j]), array[j]);
      }
     else
       {
          data2.addColumn('number', array[j]);//if 2nd column must be integer
       } 
    }   
     //   To Add Rows Dynamically to a google chart  

                 data2.addRows(obj.length);\\Total Rows
                     for (var i = 0; i < obj.length; i++)
                     {
                        for (var k = 0; k < array.length; k++)//Total Column 
                         {
                           if (k == 0) 
                             {
                               data2.setCell(i, k, obj[i][array[k]].toString());//if first Column is String
                             } 
                              else
                             {
                               data2.setCell(i, k, parseInt([obj[i][array[k]]]));//if other columns are int type... for charts mostly we treat just first column as string
                             }
                         }
                    }
于 2015-11-20T10:27:43.940 に答える
0

データを1つの文字列に入れて、JSON.parse(stringData)を使用できます。年の列は二重引用符で囲む必要があります

var data = new google.visualization.DataTable();  
data.addColumn('string', 'Year');  
data.addColumn('number', 'Sales'); 
data.addColumn('number', 'Expenses'); 

var stringData = '[["2004", 1000 , 400], ["2005", 1170, 460], ["2006", 660, 1120], ["2007",1030,540]]';

data.addRows(JSON.parse(stringData));
于 2017-06-02T08:08:33.267 に答える