forked from nicolaskruchten/pivottable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pivot.coffee
560 lines (468 loc) · 18.4 KB
/
pivot.coffee
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
$ = jQuery
###
Utilities
###
addCommas = (nStr) ->
nStr += ''
x = nStr.split('.')
x1 = x[0]
x2 = if x.length > 1 then '.' + x[1] else ''
rgx = /(\d+)(\d{3})/
x1 = x1.replace(rgx, '$1' + ',' + '$2') while rgx.test(x1)
return x1 + x2
numberFormat = (sigfig=3, scaler=1) ->
(x) ->
if x==0 or isNaN(x) or not isFinite(x) then ""
else addCommas (scaler*x).toFixed(sigfig)
#technically these are aggregator constructor generator generators (!)
aggregatorTemplates =
sum: (sigfig=3, scaler=1) -> ([field]) -> ->
sum: 0
push: (row) -> @sum += parseFloat(row[field]) if not isNaN parseFloat(row[field])
value: -> @sum
format: numberFormat(sigfig, scaler)
average: (sigfig=3, scaler=1) -> ([field]) -> ->
sum: 0
len: 0
push: (row) ->
if not isNaN parseFloat(row[field])
@sum += parseFloat(row[field])
@len++
value: -> @sum/@len
format: numberFormat(sigfig, scaler)
sumOverSum: (sigfig=3, scaler=1) -> ([num, denom]) -> ->
sumNum: 0
sumDenom: 0
push: (row) ->
@sumNum += parseFloat(row[num]) if not isNaN parseFloat(row[num])
@sumDenom += parseFloat(row[denom]) if not isNaN parseFloat(row[denom])
value: -> @sumNum/@sumDenom
format: numberFormat(sigfig, scaler)
sumOverSumBound80: (sigfig=3, scaler=1, upper=true) -> ([num, denom]) -> ->
sumNum: 0
sumDenom: 0
push: (row) ->
@sumNum += parseFloat(row[num]) if not isNaN parseFloat(row[num])
@sumDenom += parseFloat(row[denom]) if not isNaN parseFloat(row[denom])
value: ->
sign = if upper then 1 else -1
(0.821187207574908/@sumDenom + @sumNum/@sumDenom + 1.2815515655446004*sign*
Math.sqrt(0.410593603787454/ (@sumDenom*@sumDenom) + (@sumNum*(1 - @sumNum/ @sumDenom))/ (@sumDenom*@sumDenom)))/
(1 + 1.642374415149816/@sumDenom)
format: numberFormat(sigfig, scaler)
#technically these are aggregator constructor generators (!)
aggregators =
count: -> ->
count: 0
push: -> @count++
value: -> @count
format: numberFormat(0)
countUnique: ([field]) -> ->
uniq: []
push: (row) -> @uniq.push(row[field]) if row[field] not in @uniq
value: -> @uniq.length
format: numberFormat(0)
listUnique: ([field]) -> ->
uniq: []
push: (row) -> @uniq.push(row[field]) if row[field] not in @uniq
value: -> @uniq.join ", "
format: (x) -> x
intSum: aggregatorTemplates.sum(0)
sum: aggregatorTemplates.sum(3)
average: aggregatorTemplates.average(3)
sumOverSum: aggregatorTemplates.sumOverSum(3)
ub80: aggregatorTemplates.sumOverSumBound80(3, 1, true)
lb80: aggregatorTemplates.sumOverSumBound80(3, 1, false)
effects =
"Row Barchart": (x) -> x.barchart()
"Heatmap": (x) -> x.heatmap()
"Row Heatmap": (x) -> x.heatmap("rowheatmap")
"Col Heatmap": (x) -> x.heatmap("colheatmap")
derivers =
bin: (selector, binWidth) ->
if "string" == typeof selector
select = (x) -> x[selector]
else
select = selector
(row) -> "#{select(row) - select(row) % binWidth}"
$.pivotUtilities = {aggregatorTemplates, aggregators, effects, derivers}
###
functions for accessing input
###
deriveAttributes = (row, derivedAttributes, f) ->
row[k] = v(row) ? row[k] for k, v of derivedAttributes
row[k] ?= "null" for k of row
f(row)
#can handle arrays or jQuery selections of tables
forEachRow = (input, derivedAttributes, f) ->
if Array.isArray(input)
deriveAttributes(row, derivedAttributes, f) for row in input
else
tblCols = []
$("thead > tr > th", input).each (i) -> tblCols.push $(this).text()
$("tbody > tr", input).each (i) ->
row = {}
$("td", this).each (j) -> row[tblCols[j]] = $(this).text()
deriveAttributes(row, derivedAttributes, f)
#converts to [{field:val, field:val},{field:val, field:val}] using method above
convertToArray = (input) ->
return input if Array.isArray input
result = []
forEachRow input, {}, (row) -> result.push row
return result
###
Pivot Table
###
$.fn.pivot = (input, opts) ->
defaults =
filter: -> true
aggregator: aggregators.count()
derivedAttributes: {},
postProcessor: ->
opts = $.extend defaults, opts
# iterate through input, accumulating data for cells
rows = []
rowAs = []
cols = []
colAs = []
tree = {}
totals = {rows:{}, cols:{}, all: opts.aggregator()}
forEachRow input, opts.derivedAttributes, (row) ->
if opts.filter(row)
cA = (row[x] for x in opts.cols)
c = cA.join("-")
rA = (row[x] for x in opts.rows)
r = rA.join("-")
totals.all.push row
if r != ""
if r not in rows
rowAs.push rA
rows.push r
totals.rows[r] = opts.aggregator() if not totals.rows[r]
totals.rows[r].push row
if c != ""
if c not in cols
colAs.push cA
cols.push c
totals.cols[c] = opts.aggregator() if not totals.cols[c]
totals.cols[c].push row
if c != "" and r != ""
tree[r] = {} if r not of tree
tree[r][c] = opts.aggregator() if c not of tree[r]
tree[r][c].push row
#sort row/col axes for proper row/col-spanning
strSort = (a,b) ->
return 1 if a > b
return -1 if a < b
return 0
arrSort = (a,b) -> strSort a.join(), b.join()
rowAs = rowAs.sort arrSort
colAs = colAs.sort arrSort
#helper function for setting row/col-span
spanSize = (arr, i, j) ->
if i != 0
noDraw = true
for x in [0..j]
if arr[i-1][x] != arr[i][x]
noDraw = false
if noDraw
return -1 #do not draw cell
len = 0
while i+len < arr.length
stop = false
for x in [0..j]
stop = true if arr[i][x] != arr[i+len][x]
break if stop
len++
return len
#now actually build the output
result = $("<table class='pvtTable'>")
#the first few rows are for col headers
for j, c of opts.cols
tr = $("<tr>")
if parseInt(j) == 0 and opts.rows.length != 0
tr.append $("<th>")
.attr("colspan", opts.rows.length)
.attr("rowspan", opts.cols.length)
tr.append $("<th class='pvtAxisLabel'>").text(c)
for i, cA of colAs
x = spanSize(colAs, parseInt(i), parseInt(j))
if x != -1
th = $("<th class='pvtColLabel'>").text(cA[j]).attr("colspan", x)
if parseInt(j) == opts.cols.length-1 and opts.rows.length != 0
th.attr("rowspan", 2)
tr.append th
if parseInt(j) == 0
tr.append $("<th class='pvtTotalLabel'>").text("Totals")
.attr("rowspan", opts.cols.length + (if opts.rows.length ==0 then 0 else 1))
result.append tr
#then a row for row header headers
if opts.rows.length !=0
tr = $("<tr>")
for i, r of opts.rows
tr.append $("<th class='pvtAxisLabel'>").text(r)
th = $("<th>")
if opts.cols.length ==0
th.addClass("pvtTotalLabel").text("Totals")
tr.append th
result.append tr
nullAggregator =
value: -> null
format: -> ""
#now the actual data rows, with their row headers and totals
for i, rA of rowAs
tr = $("<tr>")
for j, txt of rA
x = spanSize(rowAs, parseInt(i), parseInt(j))
if x != -1
th = $("<th class='pvtRowLabel'>").text(txt).attr("rowspan", x)
if parseInt(j) == opts.rows.length-1 and opts.cols.length !=0
th.attr("colspan",2)
tr.append th
for j, cA of colAs
aggregator = (tree[rA.join("-")][cA.join("-")] ? nullAggregator)
val = aggregator.value()
tr.append $("<td class='pvtVal row#{i} col#{j}'>")
.text(aggregator.format val)
.data("value", val)
totalAggregator = (totals.rows[rA.join("-")] ? nullAggregator)
val = totalAggregator.value()
tr.append $("<td class='pvtTotal rowTotal'>")
.text(totalAggregator.format val)
.data("value", val)
.data("for", "row"+i)
result.append tr
#finally, the row for col totals, and a grand total
tr = $("<tr>")
th = $("<th class='pvtTotalLabel'>").text("Totals")
th.attr("colspan", opts.rows.length + (if opts.cols.length == 0 then 0 else 1))
tr.append th
for j, ca of colAs
totalAggregator = (totals.cols[ca.join("-")] ? nullAggregator)
val = totalAggregator.value()
tr.append $("<td class='pvtTotal colTotal'>")
.text(totalAggregator.format val)
.data("value", val)
.data("for", "col"+j)
val = totals.all.value()
tr.append $("<td class='pvtGrandTotal'>")
.text(totals.all.format val)
.data("value", val)
result.append tr
#squirrel this away for later
result.data "dimensions", [rowAs.length, colAs.length]
@html(result)
opts.postProcessor result
return this
###
UI code, calls pivot table above
###
$.fn.pivotUI = (input, opts) ->
defaults =
derivedAttributes: {}
aggregators: aggregators
effects: effects
hiddenAxes: []
cols: [], rows: [], vals: []
opts = $.extend defaults, opts
#cache the input in some useful form
input = convertToArray(input)
tblCols = (k for k of input[0])
tblCols.push c for c of opts.derivedAttributes when (c not in tblCols)
#figure out the cardinality and some stats
axisValues = {}
axisValues[x] = {} for x in tblCols
forEachRow input, opts.derivedAttributes, (row) ->
for k, v of row
v ?= "null"
axisValues[k][v] ?= 0
axisValues[k][v]++
#start building the output
uiTable = $("<table cellpadding='5'>")
#effects controls, if desired
effectNames = (x for x, y of opts.effects)
if effectNames.length != 0
effectNames.unshift "None"
controls = $("<td colspan='2' align='center'>")
controls.append $("<strong>").text("Effects:")
for x in effectNames
radio = $("<input type='radio' name='effects' id='effects_#{x.replace(/\s/g, "")}'>")
.css("margin-left":"15px", "margin-right": "5px").val(x)
radio.attr("checked", "checked") if x=="None"
controls.append(radio).append $("<label for='effects_#{x.replace(/\s/g, "")}'>").text(x)
uiTable.append $("<tr>").append controls
#axis list, including the double-click menu
colList = $("<td colspan='2' id='unused' class='pvtAxisContainer pvtHorizList'>")
for c in tblCols when c not in opts.hiddenAxes
do (c) ->
numKeys = Object.keys(axisValues[c]).length
colLabel = $("<nobr>").text(c)
valueList = $("<div>")
.css
"z-index": 100
"width": "280px"
"height": "350px"
"overflow": "scroll"
"border": "1px solid gray"
"background": "white"
"display": "none"
"position": "absolute"
"padding": "20px"
valueList.append $("<strong>").text "#{numKeys} values for #{c}"
if numKeys > 20
valueList.append $("<p>").text "(too many to list)"
else
btns = $("<p>")
btns.append $("<button>").text("Select All").bind "click", ->
valueList.find("input").attr "checked", true
btns.append $("<button>").text("Select None").bind "click", ->
valueList.find("input").attr "checked", false
valueList.append btns
for k in Object.keys(axisValues[c]).sort()
v = axisValues[c][k]
filterItem = $("<label>")
filterItem.append $("<input type='checkbox' class='pvtFilter'>")
.attr("checked", true).data("filter", [c,k])
filterItem.append $("<span>").text "#{k} (#{v})"
valueList.append $("<p>").append(filterItem)
colLabel.bind "dblclick", (e) ->
valueList.css(left: e.pageX, top: e.pageY).toggle()
valueList.bind "click", (e) -> e.stopPropagation()
$(document).one "click", ->
refresh()
valueList.toggle()
colList.append $("<li id='axis_#{c.replace(/\s/g, "")}'>").append(colLabel).append(valueList)
uiTable.append $("<tr>").append colList
tr1 = $("<tr>")
#aggregator menu and value area
aggregator = $("<select id='aggregator'>")
.css("margin-bottom", "5px")
.bind "change", -> refresh() #capture reference
for x of opts.aggregators
aggregator.append $("<option>").val(x).text(x)
tr1.append $("<td id='vals' class='pvtAxisContainer pvtHorizList'>")
.css("text-align", "center")
.append(aggregator).append($("<br>"))
#column axes
tr1.append $("<td id='cols' class='pvtAxisContainer pvtHorizList'>")
uiTable.append tr1
tr2 = $("<tr>")
#row axes
tr2.append $("<td valign='top' id='rows' class='pvtAxisContainer'>")
#the actual pivot table container
pivotTable = $("<td valign='top'>")
tr2.append pivotTable
uiTable.append tr2
#render the UI in its default state
@html uiTable
#set up the UI initial state as requested by moving elements around
for x in opts.cols
$("#cols").append $("#axis_#{x.replace(/\s/g, "")}")
for x in opts.rows
$("#rows").append $("#axis_#{x.replace(/\s/g, "")}")
for x in opts.vals
$("#vals").append $("#axis_#{x.replace(/\s/g, "")}")
if opts.aggregatorName?
$("#aggregator").val opts.aggregatorName
if opts.effectsName?
$("#effects_#{opts.effectsName.replace(/\s/g, "")}").attr('checked',true)
#set up for refreshing
refresh = ->
subopts = {derivedAttributes: opts.derivedAttributes}
subopts.cols = []
subopts.rows = []
vals = []
$("#rows li nobr").each -> subopts.rows.push $(this).text()
$("#cols li nobr").each -> subopts.cols.push $(this).text()
$("#vals li nobr").each -> vals.push $(this).text()
subopts.aggregator = opts.aggregators[aggregator.val()](vals)
#construct filter here
exclusions = []
$('input.pvtFilter').not(':checked').each ->
exclusions.push $(this).data("filter")
subopts.filter = (row) ->
for [k,v] in exclusions
return false if row[k] == v
return true
if effectNames.length != 0
effect = $('input[name=effects]:checked').val()
if effect != "None"
subopts.postProcessor = opts.effects[effect]
pivotTable.pivot(input,subopts)
#the very first refresh will actually display the table
refresh()
#finally we attach the event handlers
$('input[name=effects]').bind "change", refresh
$(".pvtAxisContainer")
.sortable({connectWith:".pvtAxisContainer", items: 'li'})
.disableSelection()
.bind "sortstop", refresh
return this
###
Heatmap post-processing
###
$.fn.heatmap = (scope = "heatmap") ->
[numRows, numCols] = @data "dimensions"
colorGen = (color, min, max) ->
hexGen = switch color
when "red" then (hex) -> "ff#{hex}#{hex}"
when "green" then (hex) -> "#{hex}ff#{hex}"
when "blue" then (hex) -> "#{hex}#{hex}ff"
return (x) ->
intensity = 255 - Math.round 255*(x-min)/(max-min)
hex = intensity.toString(16).split(".")[0]
hex = 0+hex if hex.length == 1
return hexGen(hex)
heatmapper = (scope, color) =>
forEachCell = (f) =>
@find(scope).each ->
x = $(this).data("value")
f(x, $(this)) if x? and isFinite(x)
values = []
forEachCell (x) -> values.push x
colorFor = colorGen color, Math.min(values...), Math.max(values...)
forEachCell (x, elem) -> elem.css "background-color", colorFor(x)
switch scope
when "heatmap"
heatmapper ".pvtVal", "red"
when "rowheatmap"
heatmapper ".pvtVal.row#{i}", "red" for i in [0...numRows]
when "colheatmap"
heatmapper ".pvtVal.col#{j}", "red" for j in [0...numCols]
heatmapper ".pvtTotal.rowTotal", "red"
heatmapper ".pvtTotal.colTotal", "red"
return this
###
Barchart post-processing
###
$.fn.barchart = ->
[numRows, numCols] = @data "dimensions"
barcharter = (scope) =>
forEachCell = (f) =>
@find(scope).each ->
x = $(this).data("value")
f(x, $(this)) if x? and isFinite(x)
values = []
forEachCell (x) -> values.push x
max = Math.max(values...)
scaler = (x) -> 100*x/(1.4*max)
forEachCell (x, elem) ->
text = elem.text()
wrapper = $("<div>").css
"position": "relative"
"height": "55px"
wrapper.append $("<div>").css
"position": "absolute"
"bottom": 0
"left": 0
"right": 0
"height": scaler(x) + "%"
"background-color": "gray"
wrapper.append $("<div>").text(text).css
"position":"relative"
"padding-left":"5px"
"padding-right":"5px"
elem.css("padding": 0,"padding-top": "5px", "text-align": "center").html wrapper
barcharter ".pvtVal.row#{i}" for i in [0...numRows]
barcharter ".pvtTotal.colTotal"
return this