@lxlib/chart
Version:
This library was generated with [Angular CLI](https://github.com/angular/angular-cli) version 9.1.1.
310 lines (304 loc) • 8.35 kB
JavaScript
import { __decorate, __metadata } from 'tslib';
import { Component, ChangeDetectionStrategy, ViewEncapsulation, NgZone, ViewChild, Input, NgModule } from '@angular/core';
import { Chart } from '@antv/g2';
import { InputNumber, InputBoolean, LxlibUtilModule } from '@lxlib/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,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]: any;*/
}
class G2BarComponent {
// #endregion
/**
* @param {?} ngZone
*/
constructor(ngZone) {
this.ngZone = ngZone;
// #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';
}
/**
* @private
* @return {?}
*/
getHeight() {
return this.title ? this.height - TITLE_HEIGHT : this.height;
}
/**
* @private
* @return {?}
*/
install() {
const { node, padding, interaction } = this;
/** @type {?} */
const container = (/** @type {?} */ (node.nativeElement));
/** @type {?} */
const chart = (this.chart = new Chart({
container,
autoFit: true,
height: this.getHeight(),
padding,
}));
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 })));
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() {
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 }
];
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 }]
};
__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 {?}
* @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 {?}
* @private
*/
G2BarComponent.prototype.ngZone;
}
/**
* @fileoverview added by tsickle
* Generated from: bar.module.ts
* @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
/** @type {?} */
const COMPONENTS = [G2BarComponent];
class G2BarModule {
}
G2BarModule.decorators = [
{ type: NgModule, args: [{
imports: [CommonModule, LxlibUtilModule, NzOutletModule],
declarations: [...COMPONENTS],
exports: [...COMPONENTS],
},] }
];
/**
* @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: bar.ts
* @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
export { G2BarComponent, G2BarModule };
//# sourceMappingURL=bar.js.map