nl_tokens: return a pivot data for a dataset
pl_tokens: function pivotTable ( dataset ) { var column_names = [ ] ; var xvalues = [ ] ; var values = { } ; var rows = { } ; for ( rn in dataset . data ) { // at first run fill the existing data (rows/columns/values) if ( rn == 0 ) { // skip first row as it contains column names we don't need for pivot continue ; } var val1 = dataset . data [ rn ] [ 0 ] ; var val2 = dataset . data [ rn ] [ 1 ] ; if ( column_names . indexOf ( val2 ) == - 1 ) { column_names . push ( val2 ) ; } if ( xvalues . indexOf ( val1 ) == - 1 ) { xvalues . push ( val1 ) ; } if ( ! ( val1 in values ) ) { values [ val1 ] = [ ] ; } <mask> [ val1 ] [ val2 ] = dataset . data [ rn ] [ 2 ] ; } for ( n in xvalues ) { // at second run fill the missing values with nulls var val1 = xvalues [ n ] ; rows [ val1 ] = [ ] ; for ( m in column_names ) { var val2 = column_names [ m ] ; if ( val1 in values && val2 in values [ val1 ] ) { rows [ val1 ] . push ( values [ val1 ] [ val2 ] ) ; } else { rows [ val1 ] . push ( null ) ; } } } var res = [ ] ; column_names . unshift ( dataset . fields [ 0 ] . name ) ; res . push ( column_names ) ; // first row is pivot column names xvalues . forEach ( function ( item ) { var r = [ item ] . concat ( rows [ item ] ) ; res . push ( r ) ; } ) ; return res ; }