UNPKG

react-cohort-graph-2

Version:

Cohort Analysis Graph with ReactJS. Updated by Mikhail Monchak

311 lines (253 loc) 11.5 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var _constants = require("./constants"); function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); } function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); } function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); } function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } var VALUE = _constants.VALUE_KEYS.VALUE, PERCENT = _constants.VALUE_KEYS.PERCENT; var DataStore = /** * * @param data {Object} * @param options {Object} * options = { * shadeColor: {string} HEX * headerColor * } */ function DataStore(_data) { var _this = this; var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; _classCallCheck(this, DataStore); _defineProperty(this, "_checkValidity", function (data) { if (_typeof(data) === 'object' && !Array.isArray(data)) { for (var key in data) { if (data.hasOwnProperty(key) && _typeof(data[key]) === 'object' && !Array.isArray(data[key])) { for (var anotherKey in data[key]) { if (data[key].hasOwnProperty(anotherKey) && !Array.isArray(data[key][anotherKey])) { _this.isValid = false; return; } } } else { _this.isValid = false; return; } } } else { _this.isValid = false; } }); _defineProperty(this, "_buildStore", function (data) { var _loop = function _loop(key) { if (data.hasOwnProperty(key)) { _this.store[key] = []; var _loop2 = function _loop2(anotherKey) { if (data[key].hasOwnProperty(anotherKey)) { var cellData = {}; cellData.type = key; cellData[VALUE] = anotherKey; cellData.valueFor = anotherKey; cellData.total = data[key][anotherKey].length > 0 ? data[key][anotherKey][0] : 0; cellData.color = _this.options.keyCellColor; cellData.isDate = true; cellData.index = -1; cellData.isHeader = false; _this.store[key].push([cellData].concat(_toConsumableArray(data[key][anotherKey].map(function (value, index) { var _ref; var percent = _this._getPercentage(cellData.total, value); return _ref = { isHeader: false, index: index, type: key }, _defineProperty(_ref, VALUE, value), _defineProperty(_ref, "valueFor", anotherKey), _defineProperty(_ref, "total", cellData.total), _defineProperty(_ref, "isTotal", index === 0), _defineProperty(_ref, "isCell", index > 0), _defineProperty(_ref, PERCENT, percent), _defineProperty(_ref, "color", index === 0 ? _this.options.bodyCellColor : _this._shadeCellWithColor(percent, _this.options.shadeColor)), _ref; })))); } }; for (var anotherKey in data[key]) { _loop2(anotherKey); } } }; for (var key in data) { _loop(key); } }); _defineProperty(this, "_buildHeaders", function () { var _loop3 = function _loop3(key) { if (_this.store.hasOwnProperty(key)) { var _this$headers$key$pus; var labelPrefix = _this._turnCamelCase(key.slice(0, -1)); _this.headers[key] = []; _this.headers[key].push((_this$headers$key$pus = {}, _defineProperty(_this$headers$key$pus, VALUE, ""), _defineProperty(_this$headers$key$pus, PERCENT, ""), _defineProperty(_this$headers$key$pus, "color", _this.options.headerCellColor), _defineProperty(_this$headers$key$pus, "isLabel", true), _defineProperty(_this$headers$key$pus, "label", _this._turnCamelCase(key)), _defineProperty(_this$headers$key$pus, "index", 0), _this$headers$key$pus)); var cellData = {}; cellData.isHeader = true; cellData.index = 1; cellData.type = key; cellData[VALUE] = _this._sumOfColumnWithIndex(_this.store[key], 1); cellData.valueFor = key; cellData.total = cellData.value; cellData[PERCENT] = 100; cellData.color = _this.options.headerCellColor; cellData.label = labelPrefix + ' ' + 0; _this.headers[key].push(cellData); //second cell (Initial Count) var totalRows = _this.store[key].length; var largeRow = totalRows > 0 ? _this.store[key][0] : []; largeRow.forEach(function (el, index) { var _this$headers$key$pus2; if (index < 2) return; var value = _this._sumOfColumnWithIndex(_this.store[key], index); var percent = _this._getPercentage(_this._sumOfFirstColumnUpToIndex(_this.store[key], totalRows, index), value); _this.headers[key].push((_this$headers$key$pus2 = { isHeader: true, index: index, type: key }, _defineProperty(_this$headers$key$pus2, VALUE, value), _defineProperty(_this$headers$key$pus2, "valueFor", largeRow[0]), _defineProperty(_this$headers$key$pus2, "total", cellData.total), _defineProperty(_this$headers$key$pus2, PERCENT, percent), _defineProperty(_this$headers$key$pus2, "color", _this._shadeCellWithColor(percent, _this.options.shadeColor)), _defineProperty(_this$headers$key$pus2, "label", labelPrefix + ' ' + (index - 1)), _this$headers$key$pus2)); }); } }; //TODO: can also take custom headers for (var key in _this.store) { _loop3(key); } }); _defineProperty(this, "_sumOfArrayElements", function (arr) { return arr.reduce(function (a, b) { return a + b; }); }); _defineProperty(this, "_sumOfColumnWithIndex", function (arr, index) { var sum = 0; arr.forEach(function (el) { try { sum += el[index].value; } catch (e) { sum += 0; } }); return sum; }); _defineProperty(this, "_sumOfFirstColumnUpToIndex", function (arr, index, baseIndex) { var sum = 0; for (var i = 0; i <= index; i++) { try { if (arr[i][baseIndex]) { //If value exists upto this index FIXME: need better understanding than this sum += arr[i][1].value; } else { break; } } catch (e) { //if no further index break the loop break; } } return sum; }); _defineProperty(this, "getTypeData", function (type) { if (_this.store.hasOwnProperty(type)) { return _this.store[type]; //returns [][] } else { console.error("No Data Found for type => ".concat(type)); } }); _defineProperty(this, "getHighestRowSize", function (type) { if (_this.store.hasOwnProperty(type)) { return _this.store[type][0].length; //returns [][] } else { console.error("No Columns Found for type => ".concat(type)); } }); _defineProperty(this, "getCellData", function (type, row, col) { if (_this.store.hasOwnProperty(type)) { try { return _this.store[type][row][col]; } catch (e) { console.error("No Data Found for cell with type => ".concat(type, ", row => ").concat(row, ", col => ").concat(col)); } } else { console.error("No Data Found for cell with type => ".concat(type, ", row => ").concat(row, ", col => ").concat(col)); } }); _defineProperty(this, "getHeaderCellData", function (type, col) { if (_this.headers.hasOwnProperty(type)) { try { return _this.headers[type][col]; } catch (e) { console.error("No Data Found for cell with type => ".concat(type, ", col => ").concat(col)); } } else { console.error("No Data Found for cell with type => ".concat(type, ", col => ").concat(col)); } }); _defineProperty(this, "getHeader", function (type) { if (_this.headers.hasOwnProperty(type)) { return _this.headers[type]; //returns [][] } else { console.error("No Headers Found for type => ".concat(type)); return []; } }); _defineProperty(this, "getRows", function (type) { if (_this.store.hasOwnProperty(type)) { return _this.store[type]; //returns [][] } else { console.error("No Headers Found for type => ".concat(type)); return []; } }); _defineProperty(this, "_shadeCellWithColor", function (percent) { var color = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "#3f83a3"; var rate = 1.0 - Math.ceil(percent / 10) / 10; var f = parseInt(color.slice(1), 16), t = rate < 0 ? 0 : 255, p = rate < 0 ? rate * -1 : rate, R = f >> 16, G = f >> 8 & 0x00FF, B = f & 0x0000FF; return "#".concat((0x1000000 + (Math.round((t - R) * p) + R) * 0x10000 + (Math.round((t - G) * p) + G) * 0x100 + (Math.round((t - B) * p) + B)).toString(16).slice(1)); }); _defineProperty(this, "_getPercentage", function (total, value) { return total ? Math.round(value / total * 100 * 100) / 100 : total; }); _defineProperty(this, "_isValidHex", function (color) { return /(^#[0-9A-F]{6}$)|(^#[0-9A-F]{3}$)/i.test(color); }); _defineProperty(this, "_turnCamelCase", function (text) { var defaultReturnValue = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : ""; if (typeof text === 'string') { return text.toLowerCase().replace(/\b\w/g, function (replaced) { return replaced.toUpperCase(); }); } }); this.isValid = true; this._checkValidity(_data); this.rawStore = _data; this.store = {}; this.headers = {}; this.options = options; if (this.isValid) { this._buildStore(_data); this._buildHeaders(); } else { console.error("Invalid Data for cohort graph..!"); } } /** * * @param data * @private */ ; exports["default"] = DataStore;