Manually pivoted DataTable

by Alex Azuero

HTML

<script src="https://www.google.com/jsapi?fake=.js"></script>
Table 1:
<div id="table1"></div>
<br/>Table 2:
<div id="table2"></div>

JavaScript

// origional http://jsfiddle.net/asgallant/HkjDe/

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

function drawChart() {
    var table1 = new google.visualization.Table($('#table1')[0]);
    var table2 = new google.visualization.Table($('#table2')[0]);
    
    var data = new google.visualization.DataTable({
        cols: [
            {id:'day', label:'Day', type:'string'},
            {id:'hits', label:'All Hits', type:'number'},
            {id:'uniVis', label:'Visitors', type:'number'},
            {id:'uniPag', label:'Pages', type:'number'}
        ]
    });
    data.addRows([
        ['Sunday', 16195, 9437, 768],
        ['Monday', 17624, 10407, 896],
        ['Tuesday', 19557, 11995, 956],
        ['Wednesday', 23249, 14494, 1009],
        ['Thursday', 32819, 20879, 1237],
        ['Friday', 23135, 13747, 1033],
        ['Saturday', 16176, 9232, 802]
    ]);
    
    table1.draw(data, {});
    
    console.log(new Date().getMilliseconds());
    var rowInds = data.getSortedRows([{column: 2, desc: true}]); // set column to sort by
    console.log(rowInds);
    for (var i = 0; i < rowInds.length; i++) {
        if (data.getValue(rowInds[i],0) == 'Sunday') {
            var v = data.getValue(rowInds[i], 2);
            console.log(v + ' ' + new Date().getMilliseconds());
            data.setValue(rowInds[i], 2, v + 1);
            i = rowInds.length;
        }
    }
    // note for most time/date tables, with the use of a sort column (sunday=0, monday=1, ...) row could be computed simply, others are not as luckey
    
    var view = new google.visualization.DataView(data);
    table2.draw(view, {});
}