UNPKG

@lxlib/chart

Version:

This library was generated with [Angular CLI](https://github.com/angular/angular-cli) version 9.1.1.

500 lines (494 loc) 15.8 kB
import { __values, __decorate, __metadata, __spread } from 'tslib'; import { Component, ChangeDetectionStrategy, ViewEncapsulation, ElementRef, Renderer2, NgZone, ChangeDetectorRef, ViewChild, Input, NgModule } from '@angular/core'; import { Chart } from '@antv/g2'; import { updateHostClass, InputNumber, InputBoolean, LxlibUtilModule } from '@lxlib/util'; import { fromEvent } from 'rxjs'; import { debounceTime } from 'rxjs/operators'; import { CommonModule } from '@angular/common'; import { NzOutletModule } from 'ng-zorro-antd/core/outlet'; import { NzDividerModule } from 'ng-zorro-antd/divider'; /** * @fileoverview added by tsickle * Generated from: pie.component.ts * @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ /** * @record */ function G2PieData() { } if (false) { /** @type {?} */ G2PieData.prototype.x; /** @type {?} */ G2PieData.prototype.y; /* Skipping unhandled member: [key: string]: any;*/ } var G2PieComponent = /** @class */ (function () { // #endregion function G2PieComponent(el, rend, ngZone, cdr) { this.el = el; this.rend = rend; this.ngZone = ngZone; this.cdr = cdr; this.legendData = []; // #region fields this.delay = 0; this.animate = true; this.color = 'rgba(24, 144, 255, 0.85)'; this.height = 0; this.hasLegend = false; this.inner = 0.75; this.padding = [12, 0, 12, 0]; this.tooltip = true; this.lineWidth = 0; this.select = true; this.data = []; this.interaction = 'none'; } /** * @private * @return {?} */ G2PieComponent.prototype.setCls = /** * @private * @return {?} */ function () { var _a = this, el = _a.el, rend = _a.rend, hasLegend = _a.hasLegend, isPercent = _a.isPercent; /** @type {?} */ var ne = (/** @type {?} */ (el.nativeElement)); updateHostClass(ne, rend, { 'g2-pie': true, 'g2-pie__legend-has': hasLegend, 'g2-pie__legend-block': hasLegend && ne.clientWidth <= 380, 'g2-pie__mini': isPercent, }, true); }; /** * @private * @return {?} */ G2PieComponent.prototype.fixData = /** * @private * @return {?} */ function () { var _a = this, percent = _a.percent, color = _a.color; this.isPercent = percent != null; if (this.isPercent) { this.select = false; this.tooltip = false; this.percentColor = (/** * @param {?} value * @return {?} */ function (value) { return (value === '占比' ? color || 'rgba(24, 144, 255, 0.85)' : '#F0F2F5'); }); this.data = [ { x: '占比', y: percent, }, { x: '反比', y: 100 - percent, }, ]; } }; /** * @private * @return {?} */ G2PieComponent.prototype.install = /** * @private * @return {?} */ function () { this.setCls(); var _a = this, node = _a.node, height = _a.height, padding = _a.padding, tooltip = _a.tooltip, inner = _a.inner, hasLegend = _a.hasLegend, interaction = _a.interaction; /** @type {?} */ var chart = (this.chart = new Chart({ container: node.nativeElement, autoFit: true, height: height, padding: padding, })); if (!tooltip) { chart.tooltip(false); } else { chart.tooltip({ showTitle: false, showMarkers: false, }); } if (interaction !== 'none') { chart.interaction(interaction); } chart.axis(false).legend(false).coordinate('theta', { innerRadius: inner }); chart.filter('x', (/** * @param {?} _val * @param {?} item * @return {?} */ function (_val, item) { return item.checked !== false; })); chart .interval() .adjust('stack') .position('y') .tooltip('x*percent', (/** * @param {?} name * @param {?} p * @return {?} */ function (name, p) { return ({ name: name, value: (hasLegend ? p : (p * 100).toFixed(2)) + " %", }); })) .state({}); this.attachChart(); }; /** * @private * @return {?} */ G2PieComponent.prototype.attachChart = /** * @private * @return {?} */ function () { var e_1, _a; var _this = this; var _b = this, chart = _b.chart, height = _b.height, padding = _b.padding, animate = _b.animate, data = _b.data, lineWidth = _b.lineWidth, isPercent = _b.isPercent, percentColor = _b.percentColor, colors = _b.colors; if (!chart) return; chart.height = height; chart.padding = padding; chart.animate(animate); chart.geometries[0].style({ lineWidth: lineWidth, stroke: '#fff' }).color('x', isPercent ? percentColor : colors); chart.scale({ x: { type: 'cat', range: [0, 1], }, }); // 转化 percent /** @type {?} */ var totalSum = data.reduce((/** * @param {?} cur * @param {?} item * @return {?} */ function (cur, item) { return cur + item.y; }), 0); try { for (var data_1 = __values(data), data_1_1 = data_1.next(); !data_1_1.done; data_1_1 = data_1.next()) { var item = data_1_1.value; item.percent = totalSum === 0 ? 0 : item.y / totalSum; } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (data_1_1 && !data_1_1.done && (_a = data_1.return)) _a.call(data_1); } finally { if (e_1) throw e_1.error; } } chart.changeData(data); this.ngZone.run((/** * @return {?} */ function () { return _this.genLegend(); })); }; /** * @private * @return {?} */ G2PieComponent.prototype.genLegend = /** * @private * @return {?} */ function () { var _a = this, hasLegend = _a.hasLegend, isPercent = _a.isPercent, cdr = _a.cdr, chart = _a.chart; if (!hasLegend || isPercent) return; this.legendData = chart.geometries[0].dataArray.map((/** * @param {?} item * @return {?} */ function (item) { /** @type {?} */ var origin = item[0]._origin; origin.color = item[0].color; origin.checked = true; origin.percent = (origin.percent * 100).toFixed(2); return origin; })); cdr.detectChanges(); }; /** * @param {?} i * @return {?} */ G2PieComponent.prototype._click = /** * @param {?} i * @return {?} */ function (i) { var _a = this, legendData = _a.legendData, chart = _a.chart; legendData[i].checked = !legendData[i].checked; chart.render(); }; /** * @private * @return {?} */ G2PieComponent.prototype.installResizeEvent = /** * @private * @return {?} */ function () { var _this = this; if (this.resize$ || !this.hasLegend) return; this.resize$ = fromEvent(window, 'resize') .pipe(debounceTime(200)) .subscribe((/** * @return {?} */ function () { return _this.setCls(); })); }; /** * @return {?} */ G2PieComponent.prototype.ngOnInit = /** * @return {?} */ function () { var _this = this; this.ngZone.runOutsideAngular((/** * @return {?} */ function () { return setTimeout((/** * @return {?} */ function () { return _this.install(); }), _this.delay); })); }; /** * @return {?} */ G2PieComponent.prototype.ngOnChanges = /** * @return {?} */ function () { var _this = this; this.fixData(); this.setCls(); this.ngZone.runOutsideAngular((/** * @return {?} */ function () { return _this.attachChart(); })); this.installResizeEvent(); }; /** * @return {?} */ G2PieComponent.prototype.ngOnDestroy = /** * @return {?} */ function () { var _this = this; if (this.resize$) { this.resize$.unsubscribe(); } if (this.chart) { this.ngZone.runOutsideAngular((/** * @return {?} */ function () { return _this.chart.destroy(); })); } }; G2PieComponent.decorators = [ { type: Component, args: [{ selector: 'g2-pie', exportAs: 'g2Pie', template: "<div class=\"g2-pie__chart\">\n <div #container></div>\n <div *ngIf=\"subTitle || total\"\n class=\"g2-pie__total\">\n <h4 *ngIf=\"subTitle\"\n class=\"g2-pie__total-title\">\n <ng-container *nzStringTemplateOutlet=\"subTitle\">\n <div [innerHTML]=\"subTitle\"></div>\n </ng-container>\n </h4>\n <div *ngIf=\"total\" class=\"g2-pie__total-stat\">\n <ng-container *nzStringTemplateOutlet=\"total\">\n <div [innerHTML]=\"total\"></div>\n </ng-container>\n </div>\n </div>\n</div>\n<ul *ngIf=\"hasLegend && legendData?.length\"\n class=\"g2-pie__legend\">\n <li *ngFor=\"let item of legendData; let index = index\" (click)=\"_click(index)\" class=\"g2-pie__legend-item\">\n <span class=\"g2-pie__legend-dot\" [ngStyle]=\"{'background-color': !item.checked ? '#aaa' : item.color}\"></span>\n <span class=\"g2-pie__legend-title\">{{item.x}}</span>\n <nz-divider nzType=\"vertical\"></nz-divider>\n <span class=\"g2-pie__legend-percent\">{{item.percent}}%</span>\n <span class=\"g2-pie__legend-value\" [innerHTML]=\"valueFormat ? valueFormat(item.y) : item.y\"></span>\n </li>\n</ul>\n", preserveWhitespaces: false, changeDetection: ChangeDetectionStrategy.OnPush, encapsulation: ViewEncapsulation.None }] } ]; /** @nocollapse */ G2PieComponent.ctorParameters = function () { return [ { type: ElementRef }, { type: Renderer2 }, { type: NgZone }, { type: ChangeDetectorRef } ]; }; G2PieComponent.propDecorators = { node: [{ type: ViewChild, args: ['container', { static: true },] }], delay: [{ type: Input }], animate: [{ type: Input }], color: [{ type: Input }], subTitle: [{ type: Input }], total: [{ type: Input }], height: [{ type: Input }], hasLegend: [{ type: Input }], inner: [{ type: Input }], padding: [{ type: Input }], percent: [{ type: Input }], tooltip: [{ type: Input }], lineWidth: [{ type: Input }], select: [{ type: Input }], valueFormat: [{ type: Input }], data: [{ type: Input }], colors: [{ type: Input }], interaction: [{ type: Input }] }; __decorate([ InputNumber(), __metadata("design:type", Object) ], G2PieComponent.prototype, "delay", void 0); __decorate([ InputBoolean(), __metadata("design:type", Object) ], G2PieComponent.prototype, "animate", void 0); __decorate([ InputNumber(), __metadata("design:type", Object) ], G2PieComponent.prototype, "height", void 0); __decorate([ InputBoolean(), __metadata("design:type", Object) ], G2PieComponent.prototype, "hasLegend", void 0); __decorate([ InputNumber(), __metadata("design:type", Number) ], G2PieComponent.prototype, "percent", void 0); __decorate([ InputBoolean(), __metadata("design:type", Object) ], G2PieComponent.prototype, "tooltip", void 0); __decorate([ InputNumber(), __metadata("design:type", Object) ], G2PieComponent.prototype, "lineWidth", void 0); __decorate([ InputBoolean(), __metadata("design:type", Object) ], G2PieComponent.prototype, "select", void 0); return G2PieComponent; }()); if (false) { /** * @type {?} * @private */ G2PieComponent.prototype.resize$; /** * @type {?} * @private */ G2PieComponent.prototype.node; /** * @type {?} * @private */ G2PieComponent.prototype.chart; /** * @type {?} * @private */ G2PieComponent.prototype.isPercent; /** * @type {?} * @private */ G2PieComponent.prototype.percentColor; /** @type {?} */ G2PieComponent.prototype.legendData; /** @type {?} */ G2PieComponent.prototype.delay; /** @type {?} */ G2PieComponent.prototype.animate; /** @type {?} */ G2PieComponent.prototype.color; /** @type {?} */ G2PieComponent.prototype.subTitle; /** @type {?} */ G2PieComponent.prototype.total; /** @type {?} */ G2PieComponent.prototype.height; /** @type {?} */ G2PieComponent.prototype.hasLegend; /** @type {?} */ G2PieComponent.prototype.inner; /** @type {?} */ G2PieComponent.prototype.padding; /** @type {?} */ G2PieComponent.prototype.percent; /** @type {?} */ G2PieComponent.prototype.tooltip; /** @type {?} */ G2PieComponent.prototype.lineWidth; /** @type {?} */ G2PieComponent.prototype.select; /** @type {?} */ G2PieComponent.prototype.valueFormat; /** @type {?} */ G2PieComponent.prototype.data; /** @type {?} */ G2PieComponent.prototype.colors; /** @type {?} */ G2PieComponent.prototype.interaction; /** * @type {?} * @private */ G2PieComponent.prototype.el; /** * @type {?} * @private */ G2PieComponent.prototype.rend; /** * @type {?} * @private */ G2PieComponent.prototype.ngZone; /** * @type {?} * @private */ G2PieComponent.prototype.cdr; } /** * @fileoverview added by tsickle * Generated from: pie.module.ts * @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ /** @type {?} */ var COMPONENTS = [G2PieComponent]; var G2PieModule = /** @class */ (function () { function G2PieModule() { } G2PieModule.decorators = [ { type: NgModule, args: [{ imports: [CommonModule, LxlibUtilModule, NzDividerModule, NzOutletModule], declarations: __spread(COMPONENTS), exports: __spread(COMPONENTS), },] } ]; return G2PieModule; }()); /** * @fileoverview added by tsickle * Generated from: public_api.ts * @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ /** * @fileoverview added by tsickle * Generated from: pie.ts * @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ export { G2PieComponent, G2PieModule }; //# sourceMappingURL=pie.js.map