UNPKG

cohort-graph

Version:
1 lines 9.67 kB
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.CohortGraph=t():e.CohortGraph=t()}(this,function(){return function(e){function t(n){if(r[n])return r[n].exports;var o=r[n]={i:n,l:!1,exports:{}};return e[n].call(o.exports,o,o.exports,t),o.l=!0,o.exports}var r={};return t.m=e,t.c=r,t.i=function(e){return e},t.d=function(e,r,n){t.o(e,r)||Object.defineProperty(e,r,{configurable:!1,enumerable:!0,get:n})},t.n=function(e){var r=e&&e.__esModule?function(){return e.default}:function(){return e};return t.d(r,"a",r),r},t.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},t.p="",t(t.s=5)}([function(e,t,r){"use strict";Object.defineProperty(t,"__esModule",{value:!0});t.VALUE_KEYS={VALUE:"value",PERCENT:"percent"}},function(e,t,r){"use strict";Object.defineProperty(t,"__esModule",{value:!0});var n=(t.DEFAULT_CELL_COLOR="#F5F5F5",t.DEFAULT_KEY_CELL_COLOR="#EEE",t.DEFAULT_HEADER_CELL_COLOR="#DDD"),o=t.DEFAULT_BORDER="1px solid #CCC";t.TableStyles={display:"table",width:"100%",borderCollapse:"collapse",textAlign:"center",borderLeft:o},t.TableRowStyles={display:"table-row"},t.TableHeaderStyles={display:"table-header-group",backgroundColor:n,fontWeight:"bold",padding:"5px 10px",borderBottom:o,borderTop:o},t.TableBodyStyles={display:"table-row-group"},t.TableCellStyles={display:"table-cell",backgroundColor:n,padding:"5px 10px",borderBottom:"1px solid #DDD",borderRight:o},t.HeaderValueStyles={fontSize:"12px"},t.HeaderLabelStyles={fontSize:"16px",padding:"0",margin:"0"}},function(e,t,r){"use strict";function n(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}Object.defineProperty(t,"__esModule",{value:!0});var o=function(){function e(e,t){for(var r=0;r<t.length;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}return function(t,r,n){return r&&e(t.prototype,r),n&&e(t,n),t}}(),a=(r(1),r(3)),i=function(e){return e&&e.__esModule?e:{default:e}}(a),l=r(4),u=r(0),s=function(){function e(t,r,o){n(this,e),this.root=t,this.options=o,this.dataStore=new i.default({}),this.currentType="",this.valueType=u.VALUE_KEYS.PERCENT,this.init(r)}return o(e,[{key:"init",value:function(e){Object.keys(e).length>0&&(this.currentType=Object.keys(e)[0],this.dataStore=new i.default(e||{}),this.render())}},{key:"render",value:function(){var e=this.root,t=(this.options,this.dataStore),r=this.currentType,n=this.valueType,o=t.getHeader(r),a=t.getRows(r);e.appendChild((0,l.Table)({header:o,body:a},n))}}]),e}();t.default=s},function(e,t,r){"use strict";function n(e,t,r){return t in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}function o(e){if(Array.isArray(e)){for(var t=0,r=Array(e.length);t<e.length;t++)r[t]=e[t];return r}return Array.from(e)}function a(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}Object.defineProperty(t,"__esModule",{value:!0});var i="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},l=r(1),u=r(0),s=u.VALUE_KEYS.VALUE,c=u.VALUE_KEYS.PERCENT,d=function e(t,r){if(a(this,e),f.call(this),this.isValid=!0,this._checkValidity(t),this.rawStore=t,this.store={},this.headers={},!this.isValid)throw new Error("Invalid Data for cohort graph..!");this._buildStore(t),this._buildHeaders()},f=function(){var e=this;this._checkValidity=function(t){if("object"!==(void 0===t?"undefined":i(t))||Array.isArray(t))e.isValid=!1;else for(var r in t){if(!t.hasOwnProperty(r)||"object"!==i(t[r])||Array.isArray(t[r]))return void(e.isValid=!1);for(var n in t[r])if(t[r].hasOwnProperty(n)&&!Array.isArray(t[r][n]))return void(e.isValid=!1)}},this._buildStore=function(t){for(var r in t)!function(r){if(t.hasOwnProperty(r)){e.store[r]=[];for(var a in t[r])!function(a){if(t[r].hasOwnProperty(a)){var i={};i.type=r,i[s]=a,i.valueFor=a,i.total=t[r][a][0],i[c]=100,i.color=l.DEFAULT_KEY_CELL_COLOR,i.isLabel=!0,e.store[r].push([i].concat(o(t[r][a].map(function(t,o){var u,d=e._getPercentage(i.total,t);return u={type:r},n(u,s,t),n(u,"valueFor",a),n(u,"total",i.total),n(u,"isTotal",0===o),n(u,c,d),n(u,"color",0===o?l.DEFAULT_CELL_COLOR:e._shadeCellWithColor(d)),u}))))}}(a)}}(r)},this._buildHeaders=function(){for(var t in e.store)!function(t){if(e.store.hasOwnProperty(t)){var r=e._turnCamelCase(t.slice(0,-1));e.headers[t]=[],e.headers[t].push({value:"",color:l.DEFAULT_HEADER_CELL_COLOR,isLabel:!0,label:e._turnCamelCase(t)});var o={};o.isHeader=!0,o.index=0,o.type=t,o[s]=e._sumOfColumnWithIndex(e.store[t],1),o.valueFor=t,o.total=o.value,o[c]=100,o.isTotal=!0,o.color=l.DEFAULT_HEADER_CELL_COLOR,o.label=r+" 0",e.headers[t].push(o);var a=e.store[t][0],i=e.store[t].length;a.forEach(function(l,u){var d;if(!(u<2)){var f=e._sumOfColumnWithIndex(e.store[t],u),h=e._getPercentage(e._sumOfFirstColumnUpToIndex(e.store[t],i),f);e.headers[t].push((d={isHeader:!0,index:u,type:t},n(d,s,f),n(d,"valueFor",a[0]),n(d,"total",o.total),n(d,c,h),n(d,"color",e._shadeCellWithColor(h)),n(d,"label",r+" "+(u-1)),d))}})}}(t)},this._sumOfArrayElements=function(e){return e.reduce(function(e,t){return e+t})},this._sumOfColumnWithIndex=function(e,t){var r=0;return e.forEach(function(e){try{r+=e[t].value}catch(e){r+=0}}),r},this._sumOfFirstColumnUpToIndex=function(e,t,r){for(var n=0,o=0;o<=t;o++)try{if(!e[o][r])break;n+=e[o][1].value}catch(e){break}return n},this.getTypeData=function(t){if(e.store.hasOwnProperty(t))return e.store[t];throw new Error("No Data Found for type => "+t)},this.getHighestRowSize=function(t){if(e.store.hasOwnProperty(t))return e.store[t][0].length;throw new Error("No Columns Found for type => "+t)},this.getCellData=function(t,r,n){if(!e.store.hasOwnProperty(t))throw new Error("No Data Found for cell with type => "+t+", row => "+r+", col => "+n);try{return e.store[t][r][n]}catch(e){throw new Error("No Data Found for cell with type => "+t+", row => "+r+", col => "+n)}},this.getHeaderCellData=function(t,r){if(!e.headers.hasOwnProperty(t))throw new Error("No Data Found for cell with type => "+t+", col => "+r);try{return e.headers[t][r]}catch(e){throw new Error("No Data Found for cell with type => "+t+", col => "+r)}},this.getHeader=function(t){if(e.headers.hasOwnProperty(t))return e.headers[t];throw new Error("No Headers Found for type => "+t)},this.getRows=function(t){if(e.store.hasOwnProperty(t))return e.store[t];throw new Error("No Headers Found for type => "+t)},this._shadeCellWithColor=function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"#3f83a3",r=1-Math.ceil(e/10)/10,n=parseInt(t.slice(1),16),o=r<0?0:255,a=r<0?-1*r:r,i=n>>16,l=n>>8&255,u=255&n;return"#"+(16777216+65536*(Math.round((o-i)*a)+i)+256*(Math.round((o-l)*a)+l)+(Math.round((o-u)*a)+u)).toString(16).slice(1)},this._getPercentage=function(e,t){return e?Math.round(t/e*100*100)/100:e},this._isValidHex=function(e){return/(^#[0-9A-F]{6}$)|(^#[0-9A-F]{3}$)/i.test(e)},this._turnCamelCase=function(e){arguments.length>1&&void 0!==arguments[1]&&arguments[1];if("string"==typeof e)return e.toLowerCase().replace(/\b\w/g,function(e){return e.toUpperCase()})}};t.default=d},function(e,t,r){"use strict";function n(e){if(null==e)throw new TypeError("Cannot destructure undefined")}Object.defineProperty(t,"__esModule",{value:!0}),t.Container=t.Table=t.Body=t.Header=t.Tr=t.Td=t.Th=void 0;var o=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(e[n]=r[n])}return e},a=r(1),i=r(0),l=i.VALUE_KEYS.VALUE,u=i.VALUE_KEYS.PERCENT,s=function(e,t){var r=e.isTotal,n=e.isLabel;return r||n?e[l]:t===u?e[u]+" %":e[l]},c=function(e,t){for(var r in t)t.hasOwnProperty(r)&&(e.style[r]=t[r])},d=t.Th=function(e,t){var r=document.createElement("div"),n=document.createElement("p"),i=document.createElement("span");return n.style=o({},n.style,a.HeaderLabelStyles),n.innerHTML=e.label,c(i,a.HeaderValueStyles),i.innerHTML=s(e,t),c(r,o({},a.TableCellStyles,{backgroundColor:e.color})),r.appendChild(n),r.appendChild(i),r},f=t.Td=function(e,t){var r=document.createElement("div");return c(r,o({},a.TableCellStyles,{backgroundColor:e.color})),r.setAttribute("title","Out of "+e.total+" on "+e.valueFor),r.innerHTML=s(e,t),r},h=t.Tr=function(e,t){var r=document.createElement("div");return c(r,a.TableRowStyles),e.forEach(function(e){r.appendChild(f(e,t))}),r},p=t.Header=function(e,t){var r=document.createElement("div");return c(r,a.TableHeaderStyles),e.forEach(function(e){r.appendChild(d(e,t))}),r},y=t.Body=function(e,t){var r=document.createElement("div");return c(r,a.TableBodyStyles),e.forEach(function(e){r.appendChild(h(e,t))}),r};t.Table=function(e,t){var r=document.createElement("div");return c(r,a.TableStyles),r.appendChild(p(e.header,t)),r.appendChild(y(e.body,t)),r},t.Container=function(e,t){n(e),document.createElement("div").setAttribute("style",null);var r=document.createElement("div");r.setAttribute("style",null),r.appendChild(o);var o=document.createElement("p");o.setAttribute("style",null),o.setAttribute("title",e.title);document.createElement("div");if(r.setAttribute("style",null),r.appendChild(o),e.enableDateRange){var a=document.createElement("a");a.setAttribute("type","text"),a.setAttribute("id","retention-date-range"),a.setAttribute("style",null);var i=document.createElement("a");i.setAttribute("style",null),i.appendChild(a)}}},function(e,t,r){"use strict";e.exports=r(2).default}])});