Reputation: 53
I'm attempting to create a chart with the Google Visualisation API using a javascript calculation that sums the values from checked radio buttons, however I have been unable to get the API to accept the output as data for a scatter chart.
Javascript:
$(document).ready(function(){
var radios = $('input[type=radio]');
var evalResult = function(){
var numQuestions = 80;
var sum1 = 0;
var sum2 = 0;
var expr_str = '';
for(var i = 1; i <= numQuestions; i++){
var q = $("input[type=radio][name=q"+i+"]:checked").val();
var value = q.split(',');
sum1 += parseInt(value[0]);
sum2 += parseInt(value[1]);
}
var total = eval("['',''],[" + sum1 + "," + sum2 + "]");
}
evalResult();
radios.change(evalResult);
});
The impression I had was that all I needed to do was format the data in such a way that the API would accept it, then call it like this:
var data = new google.visualization.arrayToDataTable(total);
I've tried various different solutions, some suggested by the documentation, others that were examples of similar approaches I found elsewhere, however nothing seems to be working. Being a bit of a newbie at this sort of thing I'm obviously missing something simple or just doing it wrong. Any help would be appreciated, thanks.
Upvotes: 3
Views: 121
Reputation: 53
Figured out why it wasn't working, needed to use a format that the DataTable could understand.
total = [['',''],[sum1, sum2]];
Now it works as is should.
Upvotes: 0
Reputation: 24001
you need to define total
before your function
$(document).ready(function(){
var radios = $('input[type=radio]');
var total = 0; //<<<<<<<<<<<<<<<<<< here
var evalResult = function(){
var numQuestions = 80;
var sum1 = 0;
var sum2 = 0;
var expr_str = '';
for(var i = 1; i <= numQuestions; i++){
var q = $("input[type=radio][name=q"+i+"]:checked").val();
var value = q.split(',');
sum1 += parseInt(value[0]);
sum2 += parseInt(value[1]);
}
total = eval("['',''],[" + sum1 + "," + sum2 + "]"); // <<< remove var from here
}
alert(total); // output should be 0
evalResult();
alert(total); // output should be your total
radios.change(evalResult);
});
Upvotes: 1