@lxlib/chart
Version:
This library was generated with [Angular CLI](https://github.com/angular/angular-cli) version 9.1.1.
449 lines (443 loc) • 13.4 kB
JavaScript
import { __decorate, __metadata } 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;*/
}
class G2PieComponent {
// #endregion
/**
* @param {?} el
* @param {?} rend
* @param {?} ngZone
* @param {?} cdr
*/
constructor(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 {?}
*/
setCls() {
const { el, rend, hasLegend, isPercent } = this;
/** @type {?} */
const 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 {?}
*/
fixData() {
const { percent, color } = this;
this.isPercent = percent != null;
if (this.isPercent) {
this.select = false;
this.tooltip = false;
this.percentColor = (/**
* @param {?} value
* @return {?}
*/
(value) => (value === '占比' ? color || 'rgba(24, 144, 255, 0.85)' : '#F0F2F5'));
this.data = [
{
x: '占比',
y: percent,
},
{
x: '反比',
y: 100 - percent,
},
];
}
}
/**
* @private
* @return {?}
*/
install() {
this.setCls();
const { node, height, padding, tooltip, inner, hasLegend, interaction } = this;
/** @type {?} */
const chart = (this.chart = new Chart({
container: node.nativeElement,
autoFit: true,
height,
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 {?}
*/
(_val, item) => item.checked !== false));
chart
.interval()
.adjust('stack')
.position('y')
.tooltip('x*percent', (/**
* @param {?} name
* @param {?} p
* @return {?}
*/
(name, p) => ({
name,
value: `${hasLegend ? p : (p * 100).toFixed(2)} %`,
})))
.state({});
this.attachChart();
}
/**
* @private
* @return {?}
*/
attachChart() {
const { chart, height, padding, animate, data, lineWidth, isPercent, percentColor, colors } = this;
if (!chart)
return;
chart.height = height;
chart.padding = padding;
chart.animate(animate);
chart.geometries[0].style({ lineWidth, stroke: '#fff' }).color('x', isPercent ? percentColor : colors);
chart.scale({
x: {
type: 'cat',
range: [0, 1],
},
});
// 转化 percent
/** @type {?} */
const totalSum = data.reduce((/**
* @param {?} cur
* @param {?} item
* @return {?}
*/
(cur, item) => cur + item.y), 0);
for (const item of data) {
item.percent = totalSum === 0 ? 0 : item.y / totalSum;
}
chart.changeData(data);
this.ngZone.run((/**
* @return {?}
*/
() => this.genLegend()));
}
/**
* @private
* @return {?}
*/
genLegend() {
const { hasLegend, isPercent, cdr, chart } = this;
if (!hasLegend || isPercent)
return;
this.legendData = chart.geometries[0].dataArray.map((/**
* @param {?} item
* @return {?}
*/
(item) => {
/** @type {?} */
const 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 {?}
*/
_click(i) {
const { legendData, chart } = this;
legendData[i].checked = !legendData[i].checked;
chart.render();
}
/**
* @private
* @return {?}
*/
installResizeEvent() {
if (this.resize$ || !this.hasLegend)
return;
this.resize$ = fromEvent(window, 'resize')
.pipe(debounceTime(200))
.subscribe((/**
* @return {?}
*/
() => this.setCls()));
}
/**
* @return {?}
*/
ngOnInit() {
this.ngZone.runOutsideAngular((/**
* @return {?}
*/
() => setTimeout((/**
* @return {?}
*/
() => this.install()), this.delay)));
}
/**
* @return {?}
*/
ngOnChanges() {
this.fixData();
this.setCls();
this.ngZone.runOutsideAngular((/**
* @return {?}
*/
() => this.attachChart()));
this.installResizeEvent();
}
/**
* @return {?}
*/
ngOnDestroy() {
if (this.resize$) {
this.resize$.unsubscribe();
}
if (this.chart) {
this.ngZone.runOutsideAngular((/**
* @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 = () => [
{ 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);
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 {?} */
const COMPONENTS = [G2PieComponent];
class G2PieModule {
}
G2PieModule.decorators = [
{ type: NgModule, args: [{
imports: [CommonModule, LxlibUtilModule, NzDividerModule, 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: pie.ts
* @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
export { G2PieComponent, G2PieModule };
//# sourceMappingURL=pie.js.map