angular-google-charts
Version:
A wrapper of the Google Charts library written with Angular 6
2 lines • 10.2 kB
JavaScript
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("@angular/core"),require("rxjs"),require("rxjs/operators")):"function"==typeof define&&define.amd?define("angular-google-charts",["exports","@angular/core","rxjs","rxjs/operators"],e):e((t=t||self)["angular-google-charts"]={},t.ng.core,t.rxjs,t.rxjs.operators)}(this,function(t,e,r,a){"use strict";var o=function(t,e){return(o=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var r in e)e.hasOwnProperty(r)&&(t[r]=e[r])})(t,e)};var n=function(){return(n=Object.assign||function(t){for(var e,r=1,a=arguments.length;r<a;r++)for(var o in e=arguments[r])Object.prototype.hasOwnProperty.call(e,o)&&(t[o]=e[o]);return t}).apply(this,arguments)};function i(t,e){var r="function"==typeof Symbol&&t[Symbol.iterator];if(!r)return t;var a,o,n=r.call(t),i=[];try{for(;(void 0===e||e-- >0)&&!(a=n.next()).done;)i.push(a.value)}catch(s){o={error:s}}finally{try{a&&!a.done&&(r=n["return"])&&r.call(n)}finally{if(o)throw o.error}}return i}var s=new e.InjectionToken("CHART_VERSION"),c=new e.InjectionToken("GOOGLE_API_KEY"),p=function(){function t(t,e,a){this.localeId=t,this.googleApiKey=e,this.chartVersion=a,this.scriptSource="https://www.gstatic.com/charts/loader.js",this.onLoadSubject=new r.Subject,this.initialize()}return Object.defineProperty(t.prototype,"onReady",{get:function(){return this.doneLoading?r.of(!0):this.onLoadSubject.asObservable()},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"doneLoading",{get:function(){return"undefined"!=typeof google&&"undefined"!=typeof google.charts},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"isLoading",{get:function(){var t=this;return!this.doneLoading&&Array.from(document.getElementsByTagName("script")).find(function(e){return e.src===t.scriptSource})!==undefined},enumerable:!0,configurable:!0}),t.prototype.loadChartPackages=function(t){var e=this;return new r.Observable(function(r){var a={packages:t,language:e.localeId,mapsApiKey:e.googleApiKey};google.charts.load(e.chartVersion,a),google.charts.setOnLoadCallback(function(){r.next(),r.complete()})})},t.prototype.initialize=function(){var t=this;if(!this.doneLoading&&!this.isLoading){var e=this.createScriptElement();e.onload=function(){t.onLoadSubject.next(!0),t.onLoadSubject.complete()},e.onerror=function(){console.error("Failed to load the google chart script!"),t.onLoadSubject.error("Failed to load the google chart script!"),t.onLoadSubject.complete()}}},t.prototype.createScriptElement=function(){var t=document.createElement("script");return t.type="text/javascript",t.src=this.scriptSource,t.async=!0,document.getElementsByTagName("head")[0].appendChild(t),t},t.decorators=[{type:e.Injectable}],t.ctorParameters=function(){return[{type:String,decorators:[{type:e.Inject,args:[e.LOCALE_ID]}]},{type:String,decorators:[{type:e.Inject,args:[c]},{type:e.Optional}]},{type:String,decorators:[{type:e.Inject,args:[s]},{type:e.Optional}]}]},t}(),h=function(){function t(){}return t.getPackageForChartName=function(e){return t.ChartTypesToPackages[e]},t.ChartTypesToPackages={AnnotationChart:"annotationchart",AreaChart:"corechart",Bar:"bar",BarChart:"corechart",BubbleChart:"corechart",Calendar:"calendar",CandlestickChart:"corechart",ColumnChart:"corechart",ComboChart:"corechart",PieChart:"corechart",Gantt:"gantt",Gauge:"gauge",GeoChart:"geochart",Histogram:"corechart",Line:"line",LineChart:"corechart",Map:"map",OrgChart:"orgchart",Sankey:"sankey",Scatter:"scatter",ScatterChart:"corechart",SteppedAreaChart:"corechart",Table:"table",Timeline:"timeline",TreeMap:"treemap",WordTree:"wordtree"},t}(),u=function(){function t(t,r){this.element=t,this.loaderService=r,this.dynamicResize=!1,this.firstRowIsData=!1,this.error=new e.EventEmitter,this.ready=new e.EventEmitter,this.select=new e.EventEmitter,this.mouseenter=new e.EventEmitter,this.mouseleave=new e.EventEmitter}return t.prototype.ngOnInit=function(){var t=this;if(null==this.chartData)throw new Error("Can't create a Google Chart without data!");this.loaderService.onReady.subscribe(function(){t.createChart()})},t.prototype.ngAfterViewInit=function(){this.addResizeListener()},t.prototype.ngOnChanges=function(){this.wrapper&&this.updateChart()},t.prototype.getChartElement=function(){return this.element.nativeElement.firstElementChild},t.prototype.createChart=function(){var t=this;this.loadNeededPackages().subscribe(function(){t.wrapper=new google.visualization.ChartWrapper,t.updateChart()})},t.prototype.loadNeededPackages=function(){return this.loaderService.loadChartPackages([h.getPackageForChartName(this.chartData.chartType)])},t.prototype.updateChart=function(){this.chartData.dataTable instanceof google.visualization.DataTable?this.dataTable=this.chartData.dataTable:this.dataTable=google.visualization.arrayToDataTable(this.chartData.dataTable,this.firstRowIsData),this.wrapper.setDataTable(this.dataTable),this.wrapper.setChartType(this.chartData.chartType),this.wrapper.setOptions(this.chartData.options),this.wrapper.setDataSourceUrl(this.chartData.dataSourceUrl),this.wrapper.setQuery(this.chartData.query),this.wrapper.setRefreshInterval(this.chartData.refreshInterval),this.wrapper.setView(this.chartData.view),this.removeChartEvents(),this.registerChartEvents(),this.formatter&&this.formatData(this.dataTable),this.wrapper.draw(this.element.nativeElement)},t.prototype.formatData=function(t){if(this.formatter instanceof Array)this.formatter.forEach(function(e){e.formatter.format(t,e.colIndex)});else for(var e=0;e<t.getNumberOfColumns();e++)this.formatter.format(t,e)},t.prototype.addResizeListener=function(){var t=this;this.dynamicResize&&r.fromEvent(window,"resize").pipe(a.debounceTime(100)).subscribe(function(){t.ngOnChanges()})},t.prototype.removeChartEvents=function(){google.visualization.events.removeAllListeners(this.wrapper)},t.prototype.registerChartEvents=function(){var t=this;this.registerChartEvent(this.wrapper,"ready",function(){t.registerChartEvent(t.wrapper.getChart(),"onmouseover",function(e){return t.mouseenter.emit(e)}),t.registerChartEvent(t.wrapper.getChart(),"onmouseout",function(e){return t.mouseleave.emit(e)}),t.ready.emit("Chart Ready")}),this.registerChartEvent(this.wrapper,"error",function(e){return t.error.emit(e)}),this.registerChartEvent(this.wrapper,"select",function(){var e=t.wrapper.getChart().getSelection();t.select.emit(e)})},t.prototype.registerChartEvent=function(t,e,r){google.visualization.events.addListener(t,e,r)},t.decorators=[{type:e.Component,args:[{selector:"raw-chart",template:"",exportAs:"raw-chart",changeDetection:e.ChangeDetectionStrategy.OnPush,styles:[":host { width: fit-content; display: block; }"]}]}],t.ctorParameters=function(){return[{type:e.ElementRef},{type:p}]},t.propDecorators={chartData:[{type:e.Input}],formatter:[{type:e.Input}],dynamicResize:[{type:e.Input}],firstRowIsData:[{type:e.Input}],error:[{type:e.Output}],ready:[{type:e.Output}],select:[{type:e.Output}],mouseenter:[{type:e.Output}],mouseleave:[{type:e.Output}]},t}(),l=function(t){function r(e,r){var a=t.call(this,e,r)||this;return a.roles=new Array,a.width=undefined,a.height=undefined,a.options={},a}return function(t,e){function r(){this.constructor=t}o(t,e),t.prototype=null===e?Object.create(e):(r.prototype=e.prototype,new r)}(r,t),r.prototype.ngOnInit=function(){var t=this;if(null==this.type)throw new Error("Can't create a Google Chart without specifying a type!");if(null==this.data)throw new Error("Can't create a Google Chart without data!");this.chartData={chartType:this.type},this.loaderService.onReady.subscribe(function(){t.createChart()})},r.prototype.ngOnChanges=function(){this.wrapper&&(this.chartData={chartType:this.type,dataTable:this.getDataTable(),options:this.parseOptions()}),t.prototype.ngOnChanges.call(this)},r.prototype.parseOptions=function(){return n({title:this.title,width:this.width,height:this.height},this.options)},r.prototype.createChart=function(){var t=this;this.loadNeededPackages().subscribe(function(){t.chartData={chartType:t.type,dataTable:t.getDataTable(),options:t.parseOptions()},t.wrapper=new google.visualization.ChartWrapper,t.updateChart()})},r.prototype.getDataTable=function(){if(this.columnNames){var t=this.parseRoles(this.columnNames);return this.firstRowIsData=!1,function(){for(var t=[],e=0;e<arguments.length;e++)t=t.concat(i(arguments[e]));return t}([t],this.data)}return this.firstRowIsData=!0,this.data},r.prototype.parseRoles=function(t){var e=t.slice();if(this.roles){var r=this.roles.map(function(t){return Object.assign({},t)});r.forEach(function(t){var a,o,n={type:t.type,role:t.role};if(t.p&&(n.p=t.p),null!=t.index){e.splice(t.index+1,0,n);try{for(var i=function(t){var e="function"==typeof Symbol&&t[Symbol.iterator],r=0;return e?e.call(t):{next:function(){return t&&r>=t.length&&(t=void 0),{value:t&&t[r++],done:!t}}}}(r),s=i.next();!s.done;s=i.next()){var c=s.value;c!==t&&(c.index>t.index&&c.index++)}}catch(p){a={error:p}}finally{try{s&&!s.done&&(o=i["return"])&&o.call(i)}finally{if(a)throw a.error}}}else e.push(n)})}return e},r.decorators=[{type:e.Component,args:[{selector:"google-chart",template:"",exportAs:"google-chart",changeDetection:e.ChangeDetectionStrategy.OnPush,styles:[":host { width: fit-content; display: block; }"]}]}],r.ctorParameters=function(){return[{type:e.ElementRef},{type:p}]},r.propDecorators={data:[{type:e.Input}],columnNames:[{type:e.Input}],roles:[{type:e.Input}],title:[{type:e.Input}],width:[{type:e.Input}],height:[{type:e.Input}],options:[{type:e.Input}],type:[{type:e.Input}]},r}(u),d=[{provide:p,useFactory:g,deps:[e.LOCALE_ID,c,s]}],f=function(){function t(){}return t.forRoot=function(e,r){return{ngModule:t,providers:[d,{provide:c,useValue:e||""},{provide:s,useValue:r||"46"}]}},t.decorators=[{type:e.NgModule,args:[{providers:[p],declarations:[l,u],exports:[l,u]}]}],t}();function g(t,e,r){return new p(t,e,r)}t.GoogleChartComponent=l,t.GoogleChartPackagesHelper=h,t.GoogleChartsModule=f,t.RawChartComponent=u,t.ScriptLoaderService=p,t.ɵa=d,t.ɵb=g,t.ɵc=s,t.ɵd=c,Object.defineProperty(t,"__esModule",{value:!0})});
//# sourceMappingURL=angular-google-charts.umd.min.js.map