DataView - calculate dynamic columns from a given column id.

539 views
Skip to first unread message

James Patrick-Evans

unread,
Aug 13, 2013, 3:49:06 PM8/13/13
to google-visua...@googlegroups.com
I'm using a DataView object to only select specific columns from a DataTable and draw then on a chart. I want to be able to create new columns in the DataView that are functions of the values already in the DataView. So far i have managed to produce a cumulative number from the code below (copy into google playground https://code.google.com/apis/ajax/playground/?type=visualization#table) however i want to know the column id of the column created in the dataview/pass the column id as well as the row number. For example I ultimately want to be able to have this:

|   Number 1   |    Number 2   |   Cumulative Number 1   |   Cumulative Number 2   |   Other functions
|        4          |           3        |                 4                   |                   3                |
|        2          |           1        |                 6                   |                   4                |
|        1          |           4        |                 7                   |                   8                |
|        0          |           2        |                 7                   |                  10                |
|        3          |           1        |                 10                 |                  11                |
|        5          |           0        |                 15                 |                  11                |


  // Create and populate the data table.
  var data google.visualization.arrayToDataTable([
    ['Name''Foo Distance''Bar Distance''Smokes'],
    ['Tong Ning mu'43,  true],
    ['Huang Ang fa'26false],
    ['Huang Ang fa'10false],
    ['Huang Ang fa'02false],
    ['Huang Ang fa'35false],
    ['Huang Ang fa'57false],
    ['Huang Ang fa'82false],
    ['Huang Ang fa'01false],
    ['Teng nu'20true]
    
  ]);
  
  var dataview new google.visualization.DataView(data);
    
  dataview.setColumns([1,2,{calc:cumulativetype:'number'label:'Foo :: Total Distance'}{calc:cumulativetype:'number'label:'Bar :: Total Distance'}]);
 

function cumulative(dataTablerowNum){
  if(rowNum 0){
    return (dataview.getValue(rowNum-12dataTable.getValue(rowNum1));
  }  
    return dataTable.getValue(rowNum1);
}
  // Create and draw the visualization.
  visualization new google.visualization.Table(document.getElementById('table'));
  visualization.draw(dataviewnull);
}
 function drawVisualization({

The only way I could do this without column id is have a cumulative function for each column, however this needs to be dynamic.






tl;dr Basically i need a way to pass the column id into the calc function when calculating new columns.
 
i.e. 

 dataview.setColumns([1,2,{calc:cumulative(column_id_to_calc_cumulative_from)type:'number'label:'Foo :: Total Distance'}]);

James Patrick-Evans

unread,
Aug 13, 2013, 3:56:00 PM8/13/13
to google-visua...@googlegroups.com
Copying the playground code messed up, here's the full version: http://pastebin.com/q8Rxhut0

asgallant

unread,
Aug 13, 2013, 5:10:06 PM8/13/13
to google-visua...@googlegroups.com
There's no built-in way to use column ID's to get data from the DataTable (you have to parse the table to find the index of a column with the given ID; not difficult, but it is an extra step).  If your cumulative function can take a parameter for the column number, then you can use a method like this to create your calculated columns:

dataview.setColumns([1, 2, {
    calc: function (dt, row) {
        return sum(dt, row, 1);
    },

    type: 'number',
    label: 'Foo :: Total Distance'
}, {
    calc: function (dt, row) {
        return sum(dt, row, 2);
    },

    type: 'number',
    label: 'Bar :: Total Distance'
}]);

James Patrick-Evans

unread,
Aug 13, 2013, 6:18:37 PM8/13/13
to google-visua...@googlegroups.com
Thanks, that's exactly what i was looking for, ill try it out tomorrow.
Reply all
Reply to author
Forward
0 new messages