1

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 "

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
Umidjon Urunov
  • 651
  • 1
  • 17
  • 39
  • You seem to be misinterpreting the parameter to setColumns; it is an array of 0 followed by objects, not an array of 0 followed by an object containing objects. – Dave Feb 02 '14 at 11:23
  • What results do you get? "It's not working" is not sufficient. Give the exact error. – Charles Goodwin Feb 02 '14 at 11:23
  • Also I don't see you assign anything in viewObj anywhere. – Dave Feb 02 '14 at 11:24
  • Also you will need to use a closure to assign your function to calc, because currently it's using the loop-scope j value, which will always be =m after the loop is done. – Dave Feb 02 '14 at 11:27
  • 1
    Apart from the errors in the API calls, the *real* problem seems to be a scoping issue. The value of `j` changes with each loop iteration, but all `view.calc` functions share that *same `j` variable*. You'll need to *"close over"* `j` (or `colEvn[j]`) so each `view.calc` function has its own variable with a distinct value. [See this answer about loop closures.](http://stackoverflow.com/a/750506/1321716) – Mattias Buelens Feb 02 '14 at 11:28
  • Dave can you write fully – Umidjon Urunov Feb 02 '14 at 11:28

0 Answers0