Re: Substituting variable in addRows() doesnot work .

600 views
Skip to first unread message

asgallant

unread,
Dec 10, 2012, 12:47:10 PM12/10/12
to google-visua...@googlegroups.com
You are creating rowData as a string, not an array, which is why it doesn't work.  Try this instead:

var rowData = ['a', 10];
for (var i=0;i<=10;i++){
    c[i]= id.firstChild.getElementsByTagName('th')[i].innerHTML;
    rowData.push([c[i], 10]);
}

On Monday, December 10, 2012 3:21:21 AM UTC-5, Logesh waran wrote:
Hi , 
I am using a javascript function to get the row data and storing that in a variable called 'rowData' .
Now the value of rowData is ['CVS update Issue',10],['Testbed Issue - Ping Failure',10],['Testbed Issue - Software Missing',10]
But when substituted in the addRows([rowData]) it is not creating any chart .
But when the values is directly given without using a variable , the chart is produced .

Please see my script below .Kindly help .

<script type="text/javascript">

      google.load('visualization', '1.0', {'packages':['corechart']});
      google.setOnLoadCallback(drawChart);

      function drawChart() {
          var data = new google.visualization.DataTable();

          var rowData="['a',10]";
          
          var id= document.getElementById('summary');

          var c =  new Array();

          for(var i=0;i<=10;i++){
              c[i]= id.firstChild.getElementsByTagName('th')[i].innerHTML;
              rowData="['"+c[i]+"',10]"+','+ rowData;
          }

          
           data.addColumn('string', 'Suite');
           data.addColumn('number', 'TestCases');
           data.addRows(rowData);                                                                    // Here is where the problem is .how can i handle this with a variable .
           var options = {'title':'FA','width':400,'height':300};
           var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
           chart.draw(data, options);
      }
 </script>

Andy Gan

unread,
Jan 18, 2013, 7:30:56 PM1/18/13
to google-visua...@googlegroups.com
Hi asgallant,

I'm having a similar issue, and I have tried your technique but failed. What's wrong here? The array looks fine, as evidenced by the testing, but for some reason it refuses to push the data. It works if I comment out the section in blue though.

Any help would be tremendous! 

Cheers.

Andy

// Create the data table.
var data = new google.visualization.DataTable();
data.addColumn('string', 'RAG');
data.addColumn('number', 'Cost');

// initiates array, and tries push into it
var rowdata = [ ['a',1000] ];
rowdata.push(['b',200]);    
alert('2nd row ' + rowdata[1][0] + ' | '+rowdata[1][1]);        // this is OK, returns '2nd row b | 200'

// iterate through data
act({id:itcap}).each(function (record,recordnumber) {
    alert('test 3rd row ' + record["rag"] + ' | '+record["cost"]);  // this is also ok, returns 'test 3rd row A | 60'
                                                                    // this proves that record["rag"] and record["cost"] is OK
    rowdata.push([record["rag"],record["cost"]]);
});
alert('3rd row ' + rowdata[2][0] + ' | '+rowdata[2][1]);        // this is OK, returns '3rd row A | 60' as expected

// adds into the data table
data.addRows(rowdata);                                          // no data appears

asgallant

unread,
Jan 18, 2013, 9:54:03 PM1/18/13
to google-visua...@googlegroups.com
Try it in Chrome and look in the developer's console for error messages to see what might be going on.  At a guess, I would say that record["cost"] is being input as a string instead of a number (test with alert(typeof rowdata[2][1]);)..

Andy Gan

unread,
Jan 19, 2013, 6:27:10 PM1/19/13
to google-visua...@googlegroups.com
Worked like a charm. Thanks a bunch mate for the right answer and for the quick response!

asgallant

unread,
Jan 20, 2013, 1:04:09 AM1/20/13
to google-visua...@googlegroups.com
You're welcome.
Reply all
Reply to author
Forward
0 new messages