diff --git a/examples/gchart_renderers.js b/examples/gchart_renderers.js index 204433bd..91c3ce54 100644 --- a/examples/gchart_renderers.js +++ b/examples/gchart_renderers.js @@ -1,9 +1,12 @@ +// Generated by CoffeeScript 1.6.3 (function() { var $, makeGoogleChart; + $ = jQuery; + makeGoogleChart = function(chartType, extraOptions) { return function(pivotData, opts) { - var agg, colKey, colKeys, dataArray, dataTable, defaults, groupByTitle, h, hAxisTitle, headers, k, numCharsInHAxis, options, result, row, rowKey, rowKeys, title, v, vAxisTitle, wrapper, _i, _j, _len, _len2; + var agg, colKey, colKeys, dataArray, dataTable, defaults, groupByTitle, h, hAxisTitle, headers, k, numCharsInHAxis, options, result, row, rowKey, rowKeys, title, v, vAxisTitle, wrapper, _i, _j, _len, _len1; defaults = { localeStrings: { vs: "vs", @@ -35,7 +38,7 @@ colKey = colKeys[_i]; row = [colKey.join("-")]; numCharsInHAxis += row[0].length; - for (_j = 0, _len2 = rowKeys.length; _j < _len2; _j++) { + for (_j = 0, _len1 = rowKeys.length; _j < _len1; _j++) { rowKey = rowKeys[_j]; agg = pivotData.getAggregator(rowKey, colKey); if (agg.value() != null) { @@ -95,6 +98,7 @@ return result; }; }; + $.pivotUtilities.gchart_renderers = { "Line Chart": makeGoogleChart("LineChart"), "Bar Chart": makeGoogleChart("ColumnChart"), @@ -105,4 +109,5 @@ isStacked: true }) }; + }).call(this); diff --git a/examples/pivot.js b/examples/pivot.js index 5fd653f5..7c9d9072 100644 --- a/examples/pivot.js +++ b/examples/pivot.js @@ -1,15 +1,19 @@ +// Generated by CoffeeScript 1.6.3 (function() { - var $, PivotData, addCommas, aggregatorTemplates, aggregators, convertToArray, dayNames, deriveAttributes, derivers, forEachRecord, getPivotData, mthNames, numberFormat, pivotTableRenderer, renderers, spanSize, zeroPad; - var __slice = Array.prototype.slice, __indexOf = Array.prototype.indexOf || function(item) { - for (var i = 0, l = this.length; i < l; i++) { - if (this[i] === item) return i; - } - return -1; - }, __hasProp = Object.prototype.hasOwnProperty, __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }; + var $, PivotData, addCommas, aggregatorTemplates, aggregators, convertToArray, dayNames, deriveAttributes, derivers, forEachRecord, getPivotData, mthNames, naturalSort, numberFormat, pivotTableRenderer, renderers, spanSize, zeroPad, + __slice = [].slice, + __indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; }, + _this = this, + __hasProp = {}.hasOwnProperty, + __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }; + $ = jQuery; + /* Utilities */ + + addCommas = function(nStr) { var rgx, x, x1, x2; nStr += ''; @@ -22,6 +26,7 @@ } return x1 + x2; }; + numberFormat = function(sigfig, scaler) { if (sigfig == null) { sigfig = 3; @@ -37,6 +42,7 @@ } }; }; + aggregatorTemplates = { sum: function(sigfig, scaler) { if (sigfig == null) { @@ -191,6 +197,7 @@ }; } }; + aggregators = { count: function() { return function() { @@ -256,12 +263,19 @@ ub80: aggregatorTemplates.sumOverSumBound80(3, 1, true), lb80: aggregatorTemplates.sumOverSumBound80(3, 1, false) }; + aggregators.sumAsFractionOfTotal = aggregatorTemplates.fractionOf(aggregators.sum); + aggregators.sumAsFractionOfRow = aggregatorTemplates.fractionOf(aggregators.sum, "row"); + aggregators.sumAsFractionOfCol = aggregatorTemplates.fractionOf(aggregators.sum, "col"); + aggregators.countAsFractionOfTotal = aggregatorTemplates.fractionOf(aggregators.count); + aggregators.countAsFractionOfRow = aggregatorTemplates.fractionOf(aggregators.count, "row"); + aggregators.countAsFractionOfCol = aggregatorTemplates.fractionOf(aggregators.count, "col"); + renderers = { "Table": function(pvtData, opts) { return pivotTableRenderer(pvtData, opts); @@ -279,11 +293,15 @@ return pivotTableRenderer(pvtData, opts).heatmap("colheatmap"); } }; + mthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]; + dayNames = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]; + zeroPad = function(number) { return ("0" + number).substr(-2, 2); }; + derivers = { bin: function(col, binWidth) { return function(record) { @@ -321,31 +339,75 @@ }; } }; + + naturalSort = function(as, bs) { + var a, a1, b, b1, rd, rx, rz; + rx = /(\d+)|(\D+)/g; + rd = /\d/; + rz = /^0/; + if (typeof as === "number" || typeof bs === "number") { + if (isNaN(as)) { + return 1; + } + if (isNaN(bs)) { + return -1; + } + return as - bs; + } + a = String(as).toLowerCase(); + b = String(bs).toLowerCase(); + if (a === b) { + return 0; + } + if (!(rd.test(a) && rd.test(b))) { + return (a > b ? 1 : -1); + } + a = a.match(rx); + b = b.match(rx); + while (a.length && b.length) { + a1 = a.shift(); + b1 = b.shift(); + if (a1 !== b1) { + if (rd.test(a1) && rd.test(b1)) { + return a1.replace(rz, ".0") - b1.replace(rz, ".0"); + } else { + return (a1 > b1 ? 1 : -1); + } + } + } + return a.length - b.length; + }; + $.pivotUtilities = { aggregatorTemplates: aggregatorTemplates, aggregators: aggregators, renderers: renderers, - derivers: derivers + derivers: derivers, + naturalSort: naturalSort }; + /* functions for accessing input */ + + deriveAttributes = function(record, derivedAttributes, f) { - var k, v, _ref, _ref2; + var k, v, _ref; for (k in derivedAttributes) { v = derivedAttributes[k]; record[k] = (_ref = v(record)) != null ? _ref : record[k]; } for (k in record) { if (!__hasProp.call(record, k)) continue; - if ((_ref2 = record[k]) == null) { + if (record[k] == null) { record[k] = "null"; } } return f(record); }; + forEachRecord = function(input, derivedAttributes, f) { - var addRecord, compactRecord, i, j, k, record, tblCols, _i, _len, _ref, _results, _results2; + var addRecord, compactRecord, i, j, k, record, tblCols, _i, _len, _ref, _results, _results1; addRecord = function(record) { return deriveAttributes(record, derivedAttributes, f); }; @@ -357,25 +419,26 @@ for (i in input) { if (!__hasProp.call(input, i)) continue; compactRecord = input[i]; - if (i > 0) { - record = {}; - _ref = input[0]; - for (j in _ref) { - if (!__hasProp.call(_ref, j)) continue; - k = _ref[j]; - record[k] = compactRecord[j]; - } - _results.push(addRecord(record)); + if (!(i > 0)) { + continue; } + record = {}; + _ref = input[0]; + for (j in _ref) { + if (!__hasProp.call(_ref, j)) continue; + k = _ref[j]; + record[k] = compactRecord[j]; + } + _results.push(addRecord(record)); } return _results; } else { - _results2 = []; + _results1 = []; for (_i = 0, _len = input.length; _i < _len; _i++) { record = input[_i]; - _results2.push(addRecord(record)); + _results1.push(addRecord(record)); } - return _results2; + return _results1; } } else if (input instanceof jQuery) { tblCols = []; @@ -393,6 +456,7 @@ throw new Error("unknown input format"); } }; + convertToArray = function(input) { var result; result = []; @@ -401,6 +465,7 @@ }); return result; }; + PivotData = (function() { function PivotData(aggregator, colAttrs, rowAttrs) { this.aggregator = aggregator; @@ -423,54 +488,15 @@ this.allTotal = this.aggregator(this, [], []); this.sorted = false; } + PivotData.prototype.natSort = function(as, bs) { - var a, a1, b, b1, rd, rx, rz; - rx = /(\d+)|(\D+)/g; - rd = /\d/; - rz = /^0/; - if (typeof as === "number" || typeof bs === "number") { - if (isNaN(as)) { - return 1; - } - if (isNaN(bs)) { - return -1; - } - return as - bs; - } - a = String(as).toLowerCase(); - b = String(bs).toLowerCase(); - if (a === b) { - return 0; - } - if (!(rd.test(a) && rd.test(b))) { - if (a > b) { - return 1; - } else { - return -1; - } - } - a = a.match(rx); - b = b.match(rx); - while (a.length && b.length) { - a1 = a.shift(); - b1 = b.shift(); - if (a1 !== b1) { - if (rd.test(a1) && rd.test(b1)) { - return a1.replace(rz, ".0") - b1.replace(rz, ".0"); - } else { - if (a1 > b1) { - return 1; - } else { - return -1; - } - } - } - } - return a.length - b.length; + return naturalSort(as, bs); }; + PivotData.prototype.arrSort = function(a, b) { return this.natSort(a.join(), b.join()); }; + PivotData.prototype.sortKeys = function() { if (!this.sorted) { this.rowKeys.sort(this.arrSort); @@ -478,17 +504,21 @@ } return this.sorted = true; }; + PivotData.prototype.getColKeys = function() { this.sortKeys(); return this.colKeys; }; + PivotData.prototype.getRowKeys = function() { this.sortKeys(); return this.rowKeys; }; + PivotData.prototype.flattenKey = function(x) { return x.join(String.fromCharCode(0)); }; + PivotData.prototype.processRecord = function(record) { var colKey, flatColKey, flatRowKey, rowKey, x; colKey = (function() { @@ -544,6 +574,7 @@ return this.tree[flatRowKey][flatColKey].push(record); } }; + PivotData.prototype.getAggregator = function(rowKey, colKey) { var agg, flatColKey, flatRowKey; flatRowKey = this.flattenKey(rowKey); @@ -566,8 +597,11 @@ } }; }; + return PivotData; + })(); + getPivotData = function(input, cols, rows, aggregator, filter, derivedAttributes) { var pivotData; pivotData = new PivotData(aggregator, cols, rows); @@ -578,11 +612,12 @@ }); return pivotData; }; + spanSize = function(arr, i, j) { - var len, noDraw, stop, x; + var len, noDraw, stop, x, _i, _j; if (i !== 0) { noDraw = true; - for (x = 0; 0 <= j ? x <= j : x >= j; 0 <= j ? x++ : x--) { + for (x = _i = 0; 0 <= j ? _i <= j : _i >= j; x = 0 <= j ? ++_i : --_i) { if (arr[i - 1][x] !== arr[i][x]) { noDraw = false; } @@ -594,7 +629,7 @@ len = 0; while (i + len < arr.length) { stop = false; - for (x = 0; 0 <= j ? x <= j : x >= j; 0 <= j ? x++ : x--) { + for (x = _j = 0; 0 <= j ? _j <= j : _j >= j; x = 0 <= j ? ++_j : --_j) { if (arr[i][x] !== arr[i + len][x]) { stop = true; } @@ -606,6 +641,7 @@ } return len; }; + pivotTableRenderer = function(pivotData, opts) { var aggregator, c, colAttrs, colKey, colKeys, defaults, i, j, r, result, rowAttrs, rowKey, rowKeys, th, totalAggregator, tr, txt, val, x; defaults = { @@ -704,11 +740,14 @@ result.data("dimensions", [rowKeys.length, colKeys.length]); return result; }; + /* Pivot Table */ + + $.fn.pivot = function(input, opts) { - var defaults, pivotData, result; + var defaults, e, pivotData, result; defaults = { cols: [], rows: [], @@ -730,22 +769,28 @@ pivotData = getPivotData(input, opts.cols, opts.rows, opts.aggregator, opts.filter, opts.derivedAttributes); try { result = opts.renderer(pivotData, opts.rendererOptions); - } catch (e) { + } catch (_error) { + e = _error; console.error(e.stack); result = opts.localeStrings.renderError; } - } catch (e) { + } catch (_error) { + e = _error; console.error(e.stack); result = opts.localeStrings.computeError; } this.html(result); return this; }; + /* UI code, calls pivot table above */ + + $.fn.pivotUI = function(input, inputOpts, overwrite) { - var aggregator, axisValues, c, colList, defaults, existingOpts, i, k, opts, pivotTable, refresh, renderer, rendererControl, shownAttributes, tblCols, tr1, tr2, uiTable, x, _fn, _i, _j, _k, _l, _len, _len2, _len3, _len4, _ref, _ref2, _ref3, _ref4, _ref5, _ref6; + var aggregator, axisValues, c, colList, defaults, e, existingOpts, i, k, opts, pivotTable, refresh, renderer, rendererControl, shownAttributes, tblCols, tr1, tr2, uiTable, x, _fn, _i, _j, _k, _l, _len, _len1, _len2, _len3, _ref, _ref1, _ref2, _ref3, _ref4, _ref5, + _this = this; if (overwrite == null) { overwrite = false; } @@ -758,6 +803,8 @@ cols: [], rows: [], vals: [], + unusedAttrsVertical: false, + autoSortUnusedAttrs: false, rendererOptions: null, localeStrings: { renderError: "An error occurred rendering the PivotTable results.", @@ -769,7 +816,7 @@ } }; existingOpts = this.data("pivotUIOptions"); - if (!(existingOpts != null) || overwrite) { + if ((existingOpts == null) || overwrite) { opts = $.extend(defaults, inputOpts); } else { opts = existingOpts; @@ -799,7 +846,7 @@ axisValues[x] = {}; } forEachRecord(input, opts.derivedAttributes, function(record) { - var k, v, _base, _ref2, _results; + var v, _base, _results; _results = []; for (k in record) { if (!__hasProp.call(record, k)) continue; @@ -807,7 +854,7 @@ if (v == null) { v = "null"; } - if ((_ref2 = (_base = axisValues[k])[v]) == null) { + if ((_base = axisValues[k])[v] == null) { _base[v] = 0; } _results.push(axisValues[k][v]++); @@ -819,17 +866,22 @@ renderer = $("").attr("checked", true).data("filter", [c, k])); @@ -908,14 +960,13 @@ c = shownAttributes[i]; _fn(c); } - uiTable.append($("").append(rendererControl).append(colList)); tr1 = $(""); aggregator = $("