@ohayojp/chart
Version:
Cache the dictionary, city data etc.
484 lines (478 loc) • 15.2 kB
JavaScript
import { __decorate, __metadata } from 'tslib';
import { Platform } from '@angular/cdk/platform';
import { EventEmitter, Component, ChangeDetectionStrategy, ViewEncapsulation, ElementRef, NgZone, ChangeDetectorRef, ViewChild, Input, Output, NgModule } from '@angular/core';
import { Chart } from '@antv/g2';
import { OhayoConfigService, InputNumber, InputBoolean, OhayoUtilModule } from '@ohayojp/util';
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,missingRequire,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;*/
}
/**
* @record
*/
function G2PieClickItem() { }
if (false) {
/** @type {?} */
G2PieClickItem.prototype.item;
/** @type {?} */
G2PieClickItem.prototype.ev;
}
class G2PieComponent {
/**
* @param {?} el
* @param {?} ngZone
* @param {?} cdr
* @param {?} configSrv
* @param {?} platform
*/
constructor(el, ngZone, cdr, configSrv, platform) {
this.el = el;
this.ngZone = ngZone;
this.cdr = cdr;
this.platform = platform;
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.blockMaxWidth = 380;
this.select = true;
this.data = [];
this.interaction = 'none';
this.clickItem = new EventEmitter();
configSrv.attachKey(this, 'chart', 'theme');
}
// #endregion
/**
* @return {?}
*/
get block() {
return this.hasLegend && this.el.nativeElement.clientWidth <= this.blockMaxWidth;
}
/**
* @return {?}
*/
get chart() {
return this._chart;
}
/**
* @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() {
const { node, height, padding, tooltip, inner, hasLegend, interaction, theme } = this;
/** @type {?} */
const chart = (this._chart = new Chart({
container: node.nativeElement,
autoFit: true,
height,
padding,
theme,
}));
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({});
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, 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);
_chart.render();
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();
}
/**
* @return {?}
*/
ngOnInit() {
if (!this.platform.isBrowser) {
return;
}
this.ngZone.runOutsideAngular((/**
* @return {?}
*/
() => setTimeout((/**
* @return {?}
*/
() => this.install()), this.delay)));
}
/**
* @return {?}
*/
ngOnChanges() {
this.fixData();
this.ngZone.runOutsideAngular((/**
* @return {?}
*/
() => this.attachChart()));
}
/**
* @return {?}
*/
ngOnDestroy() {
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\" class=\"g2-pie__total\">\n <h4 *ngIf=\"subTitle\" 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\" 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",
host: {
'[class.g2-pie]': 'true',
'[class.g2-pie__legend-has]': 'hasLegend',
'[class.g2-pie__legend-block]': 'block',
'[class.g2-pie__mini]': 'isPercent',
},
preserveWhitespaces: false,
changeDetection: ChangeDetectionStrategy.OnPush,
encapsulation: ViewEncapsulation.None
}] }
];
/** @nocollapse */
G2PieComponent.ctorParameters = () => [
{ type: ElementRef },
{ type: NgZone },
{ type: ChangeDetectorRef },
{ type: OhayoConfigService },
{ type: Platform }
];
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 }],
blockMaxWidth: [{ type: Input }],
select: [{ type: Input }],
valueFormat: [{ type: Input }],
data: [{ type: Input }],
colors: [{ type: Input }],
interaction: [{ type: Input }],
theme: [{ type: Input }],
clickItem: [{ type: Output }]
};
__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([
InputNumber(),
__metadata("design:type", Object)
], G2PieComponent.prototype, "blockMaxWidth", void 0);
__decorate([
InputBoolean(),
__metadata("design:type", Object)
], G2PieComponent.prototype, "select", void 0);
if (false) {
/** @type {?} */
G2PieComponent.ngAcceptInputType_delay;
/** @type {?} */
G2PieComponent.ngAcceptInputType_height;
/** @type {?} */
G2PieComponent.ngAcceptInputType_animate;
/** @type {?} */
G2PieComponent.ngAcceptInputType_hasLegend;
/** @type {?} */
G2PieComponent.ngAcceptInputType_percent;
/** @type {?} */
G2PieComponent.ngAcceptInputType_tooltip;
/** @type {?} */
G2PieComponent.ngAcceptInputType_lineWidth;
/** @type {?} */
G2PieComponent.ngAcceptInputType_blockMaxWidth;
/** @type {?} */
G2PieComponent.ngAcceptInputType_select;
/**
* @type {?}
* @private
*/
G2PieComponent.prototype.node;
/**
* @type {?}
* @private
*/
G2PieComponent.prototype._chart;
/**
* @type {?}
* @private
*/
G2PieComponent.prototype.percentColor;
/** @type {?} */
G2PieComponent.prototype.legendData;
/** @type {?} */
G2PieComponent.prototype.isPercent;
/** @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.blockMaxWidth;
/** @type {?} */
G2PieComponent.prototype.select;
/** @type {?} */
G2PieComponent.prototype.valueFormat;
/** @type {?} */
G2PieComponent.prototype.data;
/** @type {?} */
G2PieComponent.prototype.colors;
/** @type {?} */
G2PieComponent.prototype.interaction;
/** @type {?} */
G2PieComponent.prototype.theme;
/** @type {?} */
G2PieComponent.prototype.clickItem;
/**
* @type {?}
* @private
*/
G2PieComponent.prototype.el;
/**
* @type {?}
* @private
*/
G2PieComponent.prototype.ngZone;
/**
* @type {?}
* @private
*/
G2PieComponent.prototype.cdr;
/**
* @type {?}
* @private
*/
G2PieComponent.prototype.platform;
}
/**
* @fileoverview added by tsickle
* Generated from: pie.module.ts
* @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingRequire,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
/** @type {?} */
const COMPONENTS = [G2PieComponent];
class G2PieModule {
}
G2PieModule.decorators = [
{ type: NgModule, args: [{
imports: [CommonModule, OhayoUtilModule, NzDividerModule, 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: pie.ts
* @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingRequire,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
export { G2PieComponent, G2PieModule };
//# sourceMappingURL=pie.js.map