I have this block in my javascript code :
{
type: 'number',
label: 'AAA',
calc: function (dt, row) {
return (dt.getValue(row, 1) == 'AA') ? dt.getValue(row, 2) : null;
}
}
The value 'AA' is hard coded and I need to re-write it to loop over this set of values ( AA, BB, CC, ... ).
Written sequentially, it would run something like this :
var view = new google.visualization.DataView(data);
view.setColumns([0, {
type: 'number',
label: 'AA',
calc: function (dt, row) {
return (dt.getValue(row, 1) == 'AA') ? dt.getValue(row, 2) : null;
}
}, {
type: 'number',
label: 'BB',
calc: function (dt, row) {
return (dt.getValue(row, 1) == 'BB') ? dt.getValue(row, 2) : null;
}
}, {
type: 'number',
label: 'CC',
calc: function (dt, row) {
return (dt.getValue(row, 1) == 'CC') ? dt.getValue(row, 2) : null;
}
}]);
I tried to do it with this code but I don't know why it's not working :
var colEvn = ["AA","BB","CC"];
$viewObj = new Object();
for (var j = 0, m = colEvn.length; j < m; j++) {
view.type = 'number';
view.label = colEvn[j];
view.calc = function (dt, row) {
return (dt.getValue(row, 1) == colEvn[j]) ? dt.getValue(row, 2) : null;
}
}
And use collected object in :
var view = new google.visualization.DataView(data);
view.setColumns([0, $viewObj]);
How can I do it? Thanks in advance!
I changed my code like this :
function viewFunc(j) {
return function() { return (dt.getValue(row, 1) == colEvn[j]) ? dt.getValue(row, 2) : null; };
}
var viewObj = new Object();
for (var j = 0, m = colEvn.length; j < m; j++) {
viewObj.type = 'number';
viewObj.label = colEvn[j];
viewObj.calc = viewFunc(j);
}
var view = new google.visualization.DataView(data);
view.setColumns([0, viewObj]);
It is giving me error -> " dt is not defined "