123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- 'use strict';
- function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
- /*
- *+------------------------------------------------------------------------+
- *| Licensed Materials - Property of IBM
- *| IBM Cognos Products: BI Dashboard
- *| (C) Copyright IBM Corp. 2019
- *|
- *| US Government Users Restricted Rights - Use, duplication or disclosure
- *| restricted by GSA ADP Schedule Contract with IBM Corp.
- *+------------------------------------------------------------------------+
- */
- define(['underscore'], function (_) {
- var GroupedRows = function () {
- /**
- * @param {Object} groupIndex A category index to group members by.
- * @param {Object} aggregateIndex Index of measure to aggregate on.
- */
- function GroupedRows(groupIndex, aggregateIndex) {
- _classCallCheck(this, GroupedRows);
- this.groupIndex = groupIndex; // The grouped column index to the data item array.
- this.aggregateIndex = aggregateIndex; // The index of the aggregate column in the data item array.
- this.bins = {};
- this.binKeys = [];
- }
- /**
- * Extracts from a single row the group attribute members
- * - this represents a single 'bin' to aggregate values by.
- * @param {Object} row A data point row from the query result data.
- * @return {Integer} A key which represent the group attribute members (index into the data item array).
- * Example: groupIndex = [0]
- * row.pt = [1, { v: 1234 }, 0]
- * _rowToKey = 1
- */
- GroupedRows.prototype._rowToKey = function _rowToKey(row) {
- return row.pt[this.groupIndex];
- };
- /**
- * Gets the bin for the supplied key.
- * If the bin doesn't already exist it will be created.
- * Example: key [1] and bins did not exist
- * bins (after creating them) = { 1: { rows: [], aggregate: 0 } }
- * returns { rows: [], aggregate: 0 }
- * @param {Object} key An index into the map of bins.
- * @return {Object} The bin for the given key.
- */
- GroupedRows.prototype._getBin = function _getBin(key) {
- var bin = this.bins;
- if (!bin[key]) {
- bin[key] = {};
- }
- bin = bin[key];
- if (!bin.rows) {
- // Initialize bin
- bin.rows = []; // Track all rows in this bin
- bin.aggregate = 0; // Keep a running aggregate for this bin
- this.binKeys.push(key); // Ensure the bin key is recorded
- }
- return bin;
- };
- /**
- * Add row to the grouped bin and update the aggregate for the bin.
- * @param {Object} bin A grouping of data point rows to aggregate on.
- * @param {Object} row A data point row from the query result data.
- */
- GroupedRows.prototype._aggregate = function _aggregate(bin, row) {
- var aggregate = row.pt[this.aggregateIndex],
- aggregateValue = aggregate && aggregate.v;
- aggregateValue = aggregateValue === null || isNaN(aggregateValue) ? 0 : Number(aggregateValue);
- bin.aggregate += aggregateValue;
- bin.rows.push(row);
- };
- /**
- * Adds a query result data point to the grouped rows.
- * This will put the item into the appropriated grouped bin and update the aggregrate of the bin based on the new row.
- * @param {Object} row A data point row from the query result data.
- */
- GroupedRows.prototype.addRow = function addRow(row) {
- var key = this._rowToKey(row);
- var bin = this._getBin(key);
- this._aggregate(bin, row);
- };
- /**
- * Sort the keys for the grouped bins using the aggregateIteratee function.
- * @param {function} aggregateIteratee Function used to compare aggregate bins during sort.
- */
- GroupedRows.prototype.sort = function sort(aggregateIteratee) {
- var _this = this;
- this.binKeys = _.sortBy(this.binKeys, function (key) {
- var bin = _this._getBin(key);
- return aggregateIteratee(bin.aggregate);
- });
- };
- /**
- * Re-Index the bin row/datapoint to the new value at the index.
- * Used to align the data row to the appropriate grouped data item it references.
- * @param {Object} bin A grouping of data point rows.
- * @param {Object} index The attribute member index to re-index.
- * @param {Object} value The new index value of the attribute member.
- */
- GroupedRows.prototype._reIndex = function _reIndex(bin, index, value) {
- bin.rows.forEach(function (row) {
- row.pt[index] = value;
- });
- };
- /**
- * Flatten the ordered grouped bins of rows into one array of data points.
- * The grouped data items for each binned row will also be updated.
- */
- GroupedRows.prototype.flatten = function flatten() {
- var _this2 = this;
- return _.chain(this.binKeys).map(function (key, index) {
- var bin = _this2._getBin(key);
- _this2._reIndex(bin, _this2.groupIndex, index);
- return bin.rows;
- }).flatten().value();
- };
- return GroupedRows;
- }();
- return GroupedRows;
- });
- //# sourceMappingURL=GroupedRows.js.map
|