UNPKG

@ohayojp/chart

Version:

Cache the dictionary, city data etc.

365 lines (359 loc) 10.3 kB
import { __decorate, __metadata } from 'tslib'; import { Platform } from '@angular/cdk/platform'; import { EventEmitter, Component, ChangeDetectionStrategy, ViewEncapsulation, NgZone, ViewChild, Input, Output, NgModule } from '@angular/core'; import { Chart } from '@antv/g2'; import { OhayoConfigService, InputNumber, InputBoolean, OhayoUtilModule } from '@ohayojp/util'; import { fromEvent } from 'rxjs'; import { filter, debounceTime } from 'rxjs/operators'; import { CommonModule } from '@angular/common'; import { NzOutletModule } from 'ng-zorro-antd/core/outlet'; /** * @fileoverview added by tsickle * Generated from: bar.component.ts * @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingRequire,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ /** @type {?} */ const TITLE_HEIGHT = 41; /** * @record */ function G2BarData() { } if (false) { /** @type {?} */ G2BarData.prototype.x; /** @type {?} */ G2BarData.prototype.y; /** @type {?|undefined} */ G2BarData.prototype.color; /* Skipping unhandled member: [key: string]: NzSafeAny;*/ } /** * @record */ function G2BarClickItem() { } if (false) { /** @type {?} */ G2BarClickItem.prototype.item; /** @type {?} */ G2BarClickItem.prototype.ev; } class G2BarComponent { // #endregion /** * @param {?} ngZone * @param {?} configSrv * @param {?} platform */ constructor(ngZone, configSrv, platform) { this.ngZone = ngZone; this.platform = platform; // #region fields this.delay = 0; this.color = 'rgba(24, 144, 255, 0.85)'; this.height = 0; this.padding = 'auto'; this.data = []; this.autoLabel = true; this.interaction = 'none'; this.clickItem = new EventEmitter(); configSrv.attachKey(this, 'chart', 'theme'); } /** * @return {?} */ get chart() { return this._chart; } /** * @private * @return {?} */ getHeight() { return this.title ? this.height - TITLE_HEIGHT : this.height; } /** * @private * @return {?} */ install() { const { node, padding, interaction, theme } = this; /** @type {?} */ const container = (/** @type {?} */ (node.nativeElement)); /** @type {?} */ const chart = (this._chart = new Chart({ container, autoFit: true, height: this.getHeight(), padding, theme, })); this.updatelabel(); chart.axis('y', { title: null, line: null, tickLine: null, }); chart.scale({ x: { type: 'cat', }, y: { min: 0, }, }); chart.tooltip({ showTitle: false, }); if (interaction !== 'none') { chart.interaction(interaction); } chart.legend(false); chart .interval() .position('x*y') .color('x*y', (/** * @param {?} x * @param {?} y * @return {?} */ (x, y) => { /** @type {?} */ const colorItem = this.data.find((/** * @param {?} w * @return {?} */ w => w.x === x && w.y === y)); return colorItem && colorItem.color ? colorItem.color : this.color; })) .tooltip('x*y', (/** * @param {?} x * @param {?} y * @return {?} */ (x, y) => ({ name: x, value: y }))); chart.on(`interval:click`, (/** * @param {?} ev * @return {?} */ (ev) => { this.ngZone.run((/** * @return {?} */ () => { var _a; return this.clickItem.emit({ item: (_a = ev.data) === null || _a === void 0 ? void 0 : _a.data, ev }); })); })); this.attachChart(); } /** * @private * @return {?} */ attachChart() { const { _chart, padding, data } = this; if (!_chart || !data || data.length <= 0) return; this.installResizeEvent(); /** @type {?} */ const height = this.getHeight(); if (_chart.height !== height) { _chart.height = height; } _chart.padding = padding; _chart.data(data); _chart.render(); } /** * @private * @return {?} */ updatelabel() { const { node, data, _chart } = this; /** @type {?} */ const canvasWidth = node.nativeElement.clientWidth; /** @type {?} */ const minWidth = data.length * 30; _chart.axis('x', canvasWidth > minWidth).render(); } /** * @private * @return {?} */ installResizeEvent() { if (!this.autoLabel || this.resize$) return; this.resize$ = fromEvent(window, 'resize') .pipe(filter((/** * @return {?} */ () => !!this._chart)), debounceTime(200)) .subscribe((/** * @return {?} */ () => this.ngZone.runOutsideAngular((/** * @return {?} */ () => this.updatelabel())))); } /** * @return {?} */ ngOnInit() { if (!this.platform.isBrowser) { return; } this.ngZone.runOutsideAngular((/** * @return {?} */ () => setTimeout((/** * @return {?} */ () => this.install()), this.delay))); } /** * @return {?} */ ngOnChanges() { this.ngZone.runOutsideAngular((/** * @return {?} */ () => this.attachChart())); } /** * @return {?} */ ngOnDestroy() { if (this.resize$) { this.resize$.unsubscribe(); } if (this._chart) { this.ngZone.runOutsideAngular((/** * @return {?} */ () => this._chart.destroy())); } } } G2BarComponent.decorators = [ { type: Component, args: [{ selector: 'g2-bar', exportAs: 'g2Bar', template: "<ng-container *nzStringTemplateOutlet=\"title\">\n <h4 style=\"margin-bottom: 20px;\">{{ title }}</h4>\n</ng-container>\n<div #container></div>\n", host: { '[style.height.px]': 'height', }, preserveWhitespaces: false, changeDetection: ChangeDetectionStrategy.OnPush, encapsulation: ViewEncapsulation.None }] } ]; /** @nocollapse */ G2BarComponent.ctorParameters = () => [ { type: NgZone }, { type: OhayoConfigService }, { type: Platform } ]; G2BarComponent.propDecorators = { node: [{ type: ViewChild, args: ['container', { static: true },] }], delay: [{ type: Input }], title: [{ type: Input }], color: [{ type: Input }], height: [{ type: Input }], padding: [{ type: Input }], data: [{ type: Input }], autoLabel: [{ type: Input }], interaction: [{ type: Input }], theme: [{ type: Input }], clickItem: [{ type: Output }] }; __decorate([ InputNumber(), __metadata("design:type", Object) ], G2BarComponent.prototype, "delay", void 0); __decorate([ InputNumber(), __metadata("design:type", Object) ], G2BarComponent.prototype, "height", void 0); __decorate([ InputBoolean(), __metadata("design:type", Object) ], G2BarComponent.prototype, "autoLabel", void 0); if (false) { /** @type {?} */ G2BarComponent.ngAcceptInputType_delay; /** @type {?} */ G2BarComponent.ngAcceptInputType_height; /** @type {?} */ G2BarComponent.ngAcceptInputType_autoLabel; /** * @type {?} * @private */ G2BarComponent.prototype.resize$; /** * @type {?} * @private */ G2BarComponent.prototype._chart; /** * @type {?} * @private */ G2BarComponent.prototype.node; /** @type {?} */ G2BarComponent.prototype.delay; /** @type {?} */ G2BarComponent.prototype.title; /** @type {?} */ G2BarComponent.prototype.color; /** @type {?} */ G2BarComponent.prototype.height; /** @type {?} */ G2BarComponent.prototype.padding; /** @type {?} */ G2BarComponent.prototype.data; /** @type {?} */ G2BarComponent.prototype.autoLabel; /** @type {?} */ G2BarComponent.prototype.interaction; /** @type {?} */ G2BarComponent.prototype.theme; /** @type {?} */ G2BarComponent.prototype.clickItem; /** * @type {?} * @private */ G2BarComponent.prototype.ngZone; /** * @type {?} * @private */ G2BarComponent.prototype.platform; } /** * @fileoverview added by tsickle * Generated from: bar.module.ts * @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingRequire,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ /** @type {?} */ const COMPONENTS = [G2BarComponent]; class G2BarModule { } G2BarModule.decorators = [ { type: NgModule, args: [{ imports: [CommonModule, OhayoUtilModule, NzOutletModule], declarations: [...COMPONENTS], exports: [...COMPONENTS], },] } ]; /** * @fileoverview added by tsickle * Generated from: public_api.ts * @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingRequire,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ /** * @fileoverview added by tsickle * Generated from: bar.ts * @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingRequire,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ export { G2BarComponent, G2BarModule }; //# sourceMappingURL=bar.js.map