-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsgvizler.datatable.pack.js
More file actions
118 lines (102 loc) · 4.13 KB
/
sgvizler.datatable.pack.js
File metadata and controls
118 lines (102 loc) · 4.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/**
* Contains functions for datatable manipulation.
*
* @class sgvizler.datatable
* @static
*/
S.datatable = (function () {
// Module dependencies:
var namespace = S.namespace,
datatables = S.datatables,
DT = {}, // sgvizler.datatable
modST = "sgvizler.datatable";
/**
* This function is a modified version of
* http://jsfiddle.net/asgallant/HkjDe/ A discussion is here
* https://groups.google.com/forum/?fromgroups=#!topic/google-visualization-api/jl_5JpODmw8
*
* pivot gets in input a Google DataTable. Table must have 3
* columns. Pivoting the table will swap all the rows of the
* first colum as columns for the new table and all the rows
* of the second column as the rows of the new table. The
* aggregate function is the sum. So, borrowing data form the
* discussion above:
*
* Equipment Location Count
* e1 c1 6
* e2 c1 2
* e3 c1 1
* e4 c1 3
* e1 c2 7
* e2 c2 3
* e1 c3 8
* e2 c3 4
*
* E1 E2 E3 E4
* C1 XX xx xx xx
* C2 xx xx 0 xx
* C3 0 xx xx xx
*
* @method pivot
* @public
* @param {google.visualization.DataTable} data
* @return {google.visualization.DataTable}
* @since 0.6.0
*/
DT.pivot = datatables.add(modST, "pivot", function (dataTable) {
var columns = dataTable.getDistinctValues(0), // Get elements which will become columns.
dataView = new google.visualization.DataView(dataTable),
// Manually pivot the data table. First, we have to separate
// out the "equipment" values into their own columns using a
// DataView with calculated columns.
cary = columns.map(function (item) {
return {
type: 'number',
label: item,
calc: function (dt, row) {
// return values only for the rows where first element === item
return (dt.getValue(row, 0) === item) ? dt.getValue(row, 2) : null;
}
};
}),
// Next, we group the view on the location column, which gets
// us the pivoted data.
rary = columns.map(function (item, index) {
return {
column: index + 1,
type: 'number',
label: item,
aggregation: google.visualization.data.sum
};
});
// Put 1 in front of everything.
cary.unshift(1);
dataView.setColumns(cary);
return google.visualization.data.group(dataView, [0], rary);
});
/**
* Walks through all cells of columns with datatype string and
* runs prefixify, which replaces namespace with its prefix.
*
* @method prefixify
* @public
* @param {google.visualization.DataTable} data
* @return {google.visualization.DataTable}
* @since 0.6.0
*/
DT.prefixify = datatables.add(modST, 'prefixify', function (dataTable) {
var c, clen = dataTable.getNumberOfColumns(),
r, rlen = dataTable.getNumberOfRows();
for (c = 0; c < clen; c += 1) {
if (dataTable.getColumnType(c) === 'string') {
for (r = 0; r < rlen; r += 1) {
dataTable.setValue(r, c,
namespace.prefixify(dataTable.getValue(r, c))
);
}
}
}
return dataTable;
});
return DT;
}());