UNPKG

ng2-charts

Version:

Reactive, responsive, beautiful charts for Angular2 based on Chart.js

16 lines (14 loc) 18 kB
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("@angular/core"),require("rxjs"),require("lodash-es"),require("chart.js")):"function"==typeof define&&define.amd?define("@valor-software/ng2-charts",["exports","@angular/core","rxjs","lodash-es","chart.js"],e):e(((t="undefined"!=typeof globalThis?globalThis:t||self)["valor-software"]=t["valor-software"]||{},t["valor-software"]["ng2-charts"]={}),t.ng.core,t.rxjs,t._,t.chart_js)}(this,(function(t,e,r,o,a){"use strict"; /*! ***************************************************************************** Copyright (c) Microsoft Corporation. Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ***************************************************************************** */Object.create;function i(t,e){var r="function"==typeof Symbol&&t[Symbol.iterator];if(!r)return t;var o,a,i=r.call(t),n=[];try{for(;(void 0===e||e-- >0)&&!(o=i.next()).done;)n.push(o.value)}catch(t){a={error:t}}finally{try{o&&!o.done&&(r=i.return)&&r.call(i)}finally{if(a)throw a.error}}return n}Object.create;var n=[[255,99,132],[54,162,235],[255,206,86],[231,233,237],[75,192,192],[151,187,205],[220,220,220],[247,70,74],[70,191,189],[253,180,92],[148,159,177],[77,83,96]];function s(t,e,r){if("pie"===t||"doughnut"===t)return d(p(r));if("polarArea"===t)return{backgroundColor:(o=p(r)).map((function(t){return l(t,.6)})),borderColor:o.map((function(t){return l(t,1)})),hoverBackgroundColor:o.map((function(t){return l(t,.8)})),hoverBorderColor:o.map((function(t){return l(t,1)}))};var o;if("line"===t||"radar"===t)return function(t){return{backgroundColor:l(t,.4),borderColor:l(t,1),pointBackgroundColor:l(t,1),pointBorderColor:"#fff",pointHoverBackgroundColor:"#fff",pointHoverBorderColor:l(t,.8)}}(c(e));if("bar"===t||"horizontalBar"===t)return function(t){return{backgroundColor:l(t,.6),borderColor:l(t,1),hoverBackgroundColor:l(t,.8),hoverBorderColor:l(t,1)}}(c(e));if("bubble"===t)return d(p(r));if("scatter"===t)return d(p(r));throw new Error("getColors - Unsupported chart type "+t)}function l(t,e){return"rgba("+t.concat(e).join(",")+")"}function h(t,e){return Math.floor(Math.random()*(e-t+1))+t}function d(t){return{backgroundColor:t.map((function(t){return l(t,.6)})),borderColor:t.map((function(){return"#fff"})),pointBackgroundColor:t.map((function(t){return l(t,1)})),pointBorderColor:t.map((function(){return"#fff"})),pointHoverBackgroundColor:t.map((function(t){return l(t,1)})),pointHoverBorderColor:t.map((function(t){return l(t,1)}))}}function u(){return[h(0,255),h(0,255),h(0,255)]}function c(t){return n[t]||u()}function p(t){for(var e=new Array(t),r=0;r<t;r++)e[r]=n[r]||u();return e}var f=function(){function t(){this.pColorschemesOptions={},this.colorschemesOptions=new r.BehaviorSubject({})}return t.prototype.setColorschemesOptions=function(t){this.pColorschemesOptions=t,this.colorschemesOptions.next(t)},t.prototype.getColorschemesOptions=function(){return this.pColorschemesOptions},t}();f.decorators=[{type:e.Injectable,args:[{providedIn:"root"}]}],f.ctorParameters=function(){return[]},f.ɵprov=e.ɵɵdefineInjectable({factory:function(){return new f},token:f,providedIn:"root"});var g={Default:0,Update:1,Refresh:2};g[g.Default]="Default",g[g.Update]="Update",g[g.Refresh]="Refresh";var y=function(){function t(t,r){this.element=t,this.themeService=r,this.options={},this.chartClick=new e.EventEmitter,this.chartHover=new e.EventEmitter,this.old={dataExists:!1,dataLength:0,datasetsExists:!1,datasetsLength:0,datasetsDataObjects:[],datasetsDataLengths:[],colorsExists:!1,colors:[],labelsExist:!1,labels:[],legendExists:!1,legend:{}},this.subs=[]}return t.registerPlugin=function(t){a.pluginService.register(t)},t.unregisterPlugin=function(t){a.pluginService.unregister(t)},t.prototype.ngOnInit=function(){var t=this;this.ctx=this.element.nativeElement.getContext("2d"),this.refresh(),this.subs.push(this.themeService.colorschemesOptions.subscribe((function(e){return t.themeChanged(e)})))},t.prototype.themeChanged=function(t){this.refresh()},t.prototype.ngDoCheck=function(){var t=this;if(this.chart){var e=g.Default,r=function(t){e=t>e?t:e};switch(!!this.data!==this.old.dataExists&&(this.propagateDataToDatasets(this.data),this.old.dataExists=!!this.data,r(g.Update)),this.data&&this.data.length!==this.old.dataLength&&(this.old.dataLength=this.data&&this.data.length||0,r(g.Update)),!!this.datasets!==this.old.datasetsExists&&(this.old.datasetsExists=!!this.datasets,r(g.Update)),this.datasets&&this.datasets.length!==this.old.datasetsLength&&(this.old.datasetsLength=this.datasets&&this.datasets.length||0,r(g.Update)),this.datasets&&this.datasets.filter((function(e,r){return e.data!==t.old.datasetsDataObjects[r]})).length&&(this.old.datasetsDataObjects=this.datasets.map((function(t){return t.data})),r(g.Update)),this.datasets&&this.datasets.filter((function(e,r){return e.data.length!==t.old.datasetsDataLengths[r]})).length&&(this.old.datasetsDataLengths=this.datasets.map((function(t){return t.data.length})),r(g.Update)),!!this.colors!==this.old.colorsExists&&(this.old.colorsExists=!!this.colors,this.updateColors(),r(g.Update)),this.colors&&this.colors.filter((function(e,r){return!t.colorsEqual(e,t.old.colors[r])})).length&&(this.old.colors=this.colors.map((function(e){return t.copyColor(e)})),this.updateColors(),r(g.Update)),!!this.labels!==this.old.labelsExist&&(this.old.labelsExist=!!this.labels,r(g.Update)),this.labels&&this.labels.filter((function(e,r){return!t.labelsEqual(e,t.old.labels[r])})).length&&(this.old.labels=this.labels.map((function(e){return t.copyLabel(e)})),r(g.Update)),!!this.options.legend!==this.old.legendExists&&(this.old.legendExists=!!this.options.legend,r(g.Refresh)),this.options.legend&&this.options.legend.position!==this.old.legend.position&&(this.old.legend.position=this.options.legend.position,r(g.Refresh)),e){case g.Default:break;case g.Update:this.update();break;case g.Refresh:this.refresh()}}},t.prototype.copyLabel=function(t){return Array.isArray(t)?function(){for(var t=[],e=0;e<arguments.length;e++)t=t.concat(i(arguments[e]));return t}(t):t},t.prototype.labelsEqual=function(t,e){return Array.isArray(t)===Array.isArray(e)&&(Array.isArray(t)||t===e)&&(!Array.isArray(t)||t.length===e.length)&&(!Array.isArray(t)||0===t.filter((function(t,r){return t!==e[r]})).length)},t.prototype.copyColor=function(t){return{backgroundColor:t.backgroundColor,borderWidth:t.borderWidth,borderColor:t.borderColor,borderCapStyle:t.borderCapStyle,borderDash:t.borderDash,borderDashOffset:t.borderDashOffset,borderJoinStyle:t.borderJoinStyle,pointBorderColor:t.pointBorderColor,pointBackgroundColor:t.pointBackgroundColor,pointBorderWidth:t.pointBorderWidth,pointRadius:t.pointRadius,pointHoverRadius:t.pointHoverRadius,pointHitRadius:t.pointHitRadius,pointHoverBackgroundColor:t.pointHoverBackgroundColor,pointHoverBorderColor:t.pointHoverBorderColor,pointHoverBorderWidth:t.pointHoverBorderWidth,pointStyle:t.pointStyle,hoverBackgroundColor:t.hoverBackgroundColor,hoverBorderColor:t.hoverBorderColor,hoverBorderWidth:t.hoverBorderWidth}},t.prototype.colorsEqual=function(t,e){return!t==!e&&(!t||t.backgroundColor===e.backgroundColor&&t.borderWidth===e.borderWidth&&t.borderColor===e.borderColor&&t.borderCapStyle===e.borderCapStyle&&t.borderDash===e.borderDash&&t.borderDashOffset===e.borderDashOffset&&t.borderJoinStyle===e.borderJoinStyle&&t.pointBorderColor===e.pointBorderColor&&t.pointBackgroundColor===e.pointBackgroundColor&&t.pointBorderWidth===e.pointBorderWidth&&t.pointRadius===e.pointRadius&&t.pointHoverRadius===e.pointHoverRadius&&t.pointHitRadius===e.pointHitRadius&&t.pointHoverBackgroundColor===e.pointHoverBackgroundColor&&t.pointHoverBorderColor===e.pointHoverBorderColor&&t.pointHoverBorderWidth===e.pointHoverBorderWidth&&t.pointStyle===e.pointStyle&&t.hoverBackgroundColor===e.hoverBackgroundColor&&t.hoverBorderColor===e.hoverBorderColor&&t.hoverBorderWidth===e.hoverBorderWidth)},t.prototype.updateColors=function(){var t=this;this.datasets.forEach((function(e,r){t.colors&&t.colors[r]?Object.assign(e,t.colors[r]):Object.assign(e,s(t.chartType,r,e.data.length),Object.assign({},e))}))},t.prototype.ngOnChanges=function(t){var e=g.Default,r=function(t){e=t>e?t:e};switch(t.hasOwnProperty("data")&&t.data.currentValue&&(this.propagateDataToDatasets(t.data.currentValue),r(g.Update)),t.hasOwnProperty("datasets")&&t.datasets.currentValue&&(this.propagateDatasetsToData(t.datasets.currentValue),r(g.Update)),t.hasOwnProperty("labels")&&(this.chart&&(this.chart.data.labels=t.labels.currentValue),r(g.Update)),t.hasOwnProperty("legend")&&(this.chart&&(this.chart.config.options.legend.display=t.legend.currentValue,this.chart.generateLegend()),r(g.Update)),t.hasOwnProperty("options")&&r(g.Refresh),e){case g.Update:this.update();break;case g.Refresh:case g.Default:this.refresh()}},t.prototype.ngOnDestroy=function(){this.chart&&(this.chart.destroy(),this.chart=void 0),this.subs.forEach((function(t){return t.unsubscribe()}))},t.prototype.update=function(t){if(this.chart)return this.chart.update(t)},t.prototype.hideDataset=function(t,e){this.chart.getDatasetMeta(t).hidden=e,this.chart.update()},t.prototype.isDatasetHidden=function(t){return this.chart.getDatasetMeta(t).hidden},t.prototype.toBase64Image=function(){return this.chart.toBase64Image()},t.prototype.getChartConfiguration=function(){var t=this,e=this.getDatasets(),r=Object.assign({},this.options);!1===this.legend&&(r.legend={display:!1}),r.hover=r.hover||{},r.hover.onHover||(r.hover.onHover=function(e,r){r&&!r.length||t.chartHover.emit({event:e,active:r})}),r.onClick||(r.onClick=function(e,r){t.chartClick.emit({event:e,active:r})});var o=this.smartMerge(r,this.themeService.getColorschemesOptions());return{type:this.chartType,data:{labels:this.labels||[],datasets:e},plugins:this.plugins,options:o}},t.prototype.getChartBuilder=function(t){var e=this.getChartConfiguration();return new a.Chart(t,e)},t.prototype.smartMerge=function(t,e,r){var a=this;if(void 0===r&&(r=0),0===r&&(t=o.cloneDeep(t)),Object.keys(e).forEach((function(o){if(Array.isArray(e[o])){var i=t[o];i&&i.forEach((function(t){a.smartMerge(t,e[o][0],r+1)}))}else"object"==typeof e[o]?(o in t||(t[o]={}),a.smartMerge(t[o],e[o],r+1)):t[o]=e[o]})),0===r)return t},t.prototype.isMultiLineLabel=function(t){return Array.isArray(t)},t.prototype.joinLabel=function(t){return t?this.isMultiLineLabel(t)?t.join(" "):t:null},t.prototype.propagateDatasetsToData=function(t){this.data=this.datasets.map((function(t){return t.data})),this.chart&&(this.chart.data.datasets=t),this.updateColors()},t.prototype.propagateDataToDatasets=function(t){var e=this;this.isMultiDataSet(t)?this.datasets&&t.length===this.datasets.length?this.datasets.forEach((function(e,r){e.data=t[r]})):(this.datasets=t.map((function(t,r){return{data:t,label:e.joinLabel(e.labels[r])||"Label "+r}})),this.chart&&(this.chart.data.datasets=this.datasets)):this.datasets?(this.datasets[0].data=t,this.datasets.splice(1)):(this.datasets=[{data:t}],this.chart&&(this.chart.data.datasets=this.datasets)),this.updateColors()},t.prototype.isMultiDataSet=function(t){return Array.isArray(t[0])},t.prototype.getDatasets=function(){if(!this.datasets&&!this.data)throw new Error("ng-charts configuration error, data or datasets field are required to render chart "+this.chartType);return this.datasets?(this.propagateDatasetsToData(this.datasets),this.datasets):this.data?(this.propagateDataToDatasets(this.data),this.datasets):void 0},t.prototype.refresh=function(){this.chart&&(this.chart.destroy(),this.chart=void 0),this.ctx&&(this.chart=this.getChartBuilder(this.ctx))},t}();y.decorators=[{type:e.Directive,args:[{selector:"canvas[baseChart]",exportAs:"base-chart"}]}],y.ctorParameters=function(){return[{type:e.ElementRef},{type:f}]},y.propDecorators={data:[{type:e.Input}],datasets:[{type:e.Input}],labels:[{type:e.Input}],options:[{type:e.Input}],chartType:[{type:e.Input}],colors:[{type:e.Input}],legend:[{type:e.Input}],plugins:[{type:e.Input}],chartClick:[{type:e.Output}],chartHover:[{type:e.Output}]};var b=function(){};b.decorators=[{type:e.NgModule,args:[{declarations:[y],imports:[],exports:[y]}]}],t.BaseChartDirective=y,t.ChartsModule=b,t.ThemeService=f,t.defaultColors=n,t.monkeyPatchChartJsLegend=function(){if("undefined"!=typeof Chart){var t=Chart.plugins.getAll().filter((function(t){return"legend"===t.id}))[0];t._element.prototype.fit=function(){var t=this,r=t.options,o=r.labels,i=r.display,n=t.ctx,s=e.options._parseFont(o),l=s.size,h=t.legendHitBoxes=[],d=t.minSize,u=t.isHorizontal();u?(d.width=t.maxWidth,d.height=i?10:0):(d.width=i?10:0,d.height=t.maxHeight);var c=function(t){return t.map((function(t){return n.measureText(t).width})).reduce((function(t,e){return e>t?e:t}),0)};if(i)if(n.font=s.string,u){var p=t.lineWidths=[0],f=t.lineHeights=[],g=0,y=0;n.textAlign="left",n.textBaseline="top",e.each(t.legendItems,(function(t,r){var i,s;e.isArray(t.text)?(i=c(t.text),s=l*t.text.length+o.padding):(i=n.measureText(t.text).width,s=l+o.padding),i+=a(o,l)+l/2,p[p.length-1]+i+2*o.padding>d.width&&(f.push(g),g=0,p[p.length-(r>0?0:1)]=0,y++),t.lineOrColumnIndex=y,s>g&&(g=s),h[r]={left:0,top:0,width:i,height:s},p[p.length-1]+=i+o.padding})),f.push(g),d.height+=f.reduce((function(t,e){return t+e}),0)}else{var b=o.padding,v=t.columnWidths=[],C=t.columnHeights=[],x=o.padding,m=0,B=0,D=0;e.each(t.legendItems,(function(t,r){var i,s;e.isArray(t.text)?(i=c(t.text),s=l*t.text.length):(i=n.measureText(t.text).width,s=l),i+=a(o,l)+l/2,B+l+2*b>d.height&&(x+=m+o.padding,v.push(m),C.push(B),m=0,B=0,D++),t.lineOrColumnIndex=D,m=Math.max(m,i),B+=s+b,h[r]={left:0,top:0,width:i,height:s}})),x+=m,v.push(m),C.push(B),d.width+=x}t.width=d.width,t.height=d.height},t._element.prototype.draw=function(){var t=this,i=t.options,n=i.labels,s=r.global,l=s.defaultColor,h=s.elements.line,d=t.height,u=t.columnHeights,c=t.columnWidths,p=t.width,f=t.lineWidths,g=t.lineHeights;if(i.display){var y,b=t.ctx,v=o(n.fontColor,s.defaultFontColor),C=e.options._parseFont(n),x=C.size;b.textAlign="left",b.textBaseline="middle",b.lineWidth=.5,b.strokeStyle=v,b.fillStyle=v,b.font=C.string;var m=a(n,x),B=t.legendHitBoxes,D=function(t,r,o,a){var i=x/2,n=m+i+t,s=r+i;e.isArray(o.text)?e.each(o.text,(function(t,e){var r=e*x;b.fillText(t,n,s+r)})):b.fillText(o.text,n,s),o.hidden&&(e.isArray(o.text)?function(t,e,r,o){b.beginPath(),b.lineWidth=2,b.moveTo(t,e),b.lineTo(t+r,e+o),b.moveTo(t,e+o),b.lineTo(t+r,e),b.stroke()}(n,s,a,(o.text.length-1)*(x-1)):function(t,e,r){b.beginPath(),b.lineWidth=2,b.moveTo(t,e),b.lineTo(t+r,e),b.stroke()}(n,s,a))},k=function(t,e){switch(i.align){case"start":return n.padding;case"end":return t-e;default:return(t-e+n.padding)/2}},w=t.isHorizontal();y=w?{x:t.left+k(p,f[0]),y:t.top+n.padding,line:0}:{x:t.left+n.padding,y:t.top+k(d,u[0]),line:0},e.each(t.legendItems,(function(r,a){var s,v,C;r.lineOrColumnIndex>y.line&&(w?(y.y+=g[y.line],y.line=r.lineOrColumnIndex,y.x=t.left+k(p,f[y.line])):(y.x+=c[y.line]+n.padding,y.line=r.lineOrColumnIndex,y.y=t.top+k(d,u[y.line]))),e.isArray(r.text)?(s=r.text.map((function(t){return b.measureText(t).width})).reduce((function(t,e){return e>t?e:t}),0),C=x/2*(r.text.length-1),v=x*r.text.length):(s=b.measureText(r.text).width,C=0,v=x);var S=m+x/2+s,O=y.x,H=y.y,A=w?Math.trunc((g[y.line]-B[a].height)/2):0;!function(t,r,a){if(!(isNaN(m)||m<=0)){b.save();var n=o(a.lineWidth,h.borderWidth);if(b.fillStyle=o(a.fillStyle,l),b.lineCap=o(a.lineCap,h.borderCapStyle),b.lineDashOffset=o(a.lineDashOffset,h.borderDashOffset),b.lineJoin=o(a.lineJoin,h.borderJoinStyle),b.lineWidth=n,b.strokeStyle=o(a.strokeStyle,l),b.setLineDash&&b.setLineDash(o(a.lineDash,h.borderDash)),i.labels&&i.labels.usePointStyle){var s=m*Math.SQRT2/2,d=t+m/2,u=r+x/2;e.canvas.drawPoint(b,a.pointStyle,s,d,u)}else 0!==n&&b.strokeRect(t,r,m,x),b.fillRect(t,r,m,x);b.restore()}}(O,H+C+A,r),B[a].left=O,B[a].top=H,D(O,H+A,r,s),w?y.x+=S+n.padding:y.y+=v+n.padding}))}};var e=Chart.helpers,r=Chart.defaults,o=e.valueOrDefault}else console.log("Chart not defined (guessing this is a universal build, and I don't know why this happens -- Aviad)");function a(t,e){return t.usePointStyle&&t.boxWidth>e?e:t.boxWidth}},t.monkeyPatchChartJsTooltip=function(){if("undefined"!=typeof Chart){Chart.Tooltip.prototype.drawBody=function(r,o,a){var i,n=o.bodyFontSize,s=o.bodySpacing,l=o._bodyAlign,h=o.body,d=o.displayColors,u=o.labelColors,c=0,p=d?e(o,"left"):0;a.textAlign=l,a.textBaseline="top",a.font=t.fontString(n,o._bodyFontStyle,o._bodyFontFamily),r.x=e(o,l);var f=function(t){a.fillText(t,r.x+c,r.y),r.y+=n+s};a.fillStyle=o.bodyFontColor,t.each(o.beforeBody,f),c=d&&"right"!==l?"center"===l?n/2+1:n+2:0,t.each(h,(function(e,s){i=o.labelTextColors[s],a.fillStyle=i,t.each(e.before,f),d&&(a.fillStyle=o.legendColorBackground,a.fillRect(p,r.y,n,n),a.lineWidth=1,a.strokeStyle=u[s].borderColor,a.strokeRect(p,r.y,n,n),a.fillStyle=u[s].backgroundColor,a.fillRect(p+1,r.y+1,n-2,n-2),a.fillStyle=i),t.each(e.lines,f),t.each(e.after,f)})),c=0,t.each(o.afterBody,f),r.y-=s};var t=Chart.helpers}else console.log("Chart not defined (guessing this is a universal build, and I don't know why this happens -- Aviad)");function e(t,e){return"center"===e?t.x+t.width/2:"right"===e?t.x+t.width-t.xPadding:t.x+t.xPadding}},Object.defineProperty(t,"__esModule",{value:!0})})); //# sourceMappingURL=valor-software-ng2-charts.umd.min.js.map