Skip to content Skip to sidebar Skip to footer

Taking Only Last Value Of Object

I am trying to implement Google Graph but What is the wrong in this code, it is taking last value of object. var colEvn = ['AAA','BBB','CCC']; var viewObj = new Object(); var

Solution 1:

The iteration is overwriting the same object every time, and it seems like an array of objects should be passed visualization API.

Reading the documentation, it also seems like an object should be passed with the index of the column to the setColumns function, so you probably have to do that in the loop as well

var view = new google.visualization.DataView(data);

for (var j = 0, m = colEvn.length; j < m; j++) {
    var viewObj = {
        type:  'number',
        label: colEvn[j],
        calc:  func
    };
    view.setColumns([j, viewObj]);
 }

Solution 2:

ok then, first point is you can't create a single function for all your columns, I have changed it like:

var colEvn = ["AAA", "BBB", "CCC"];
var columns = [0];
for (var j = 0, m = colEvn.length; j < m; j++) {
    var viewObj = {};
    viewObj.type = 'number';
    viewObj.label = colEvn[j];
    viewObj.calc = function viewFunc(dt, row) {
        alert("column label : " + viewFunc._colEvn);
        return (dt.getValue(row, 1) == viewFunc._colEvn) ? dt.getValue(row, 2) : null;
    };
    viewObj.calc._colEvn = colEvn[j];
    columns.push(viewObj);
}

var view = new google.visualization.DataView(data);
view.setColumns(columns);

Let me know, if the alerts execute in your code and what they show.


Post a Comment for "Taking Only Last Value Of Object"