@ohayojp/chart
Version:
Cache the dictionary, city data etc.
394 lines (388 loc) • 11.8 kB
JavaScript
import { __decorate, __metadata } from 'tslib';
import { Platform } from '@angular/cdk/platform';
import { EventEmitter, Component, ChangeDetectionStrategy, ViewEncapsulation, ChangeDetectorRef, NgZone, 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 { NzGridModule } from 'ng-zorro-antd/grid';
/**
* @fileoverview added by tsickle
* Generated from: radar.component.ts
* @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingRequire,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
/**
* @record
*/
function G2RadarData() { }
if (false) {
/** @type {?} */
G2RadarData.prototype.name;
/** @type {?} */
G2RadarData.prototype.label;
/** @type {?} */
G2RadarData.prototype.value;
/* Skipping unhandled member: [key: string]: any;*/
}
/**
* @record
*/
function G2RadarClickItem() { }
if (false) {
/** @type {?} */
G2RadarClickItem.prototype.item;
/** @type {?} */
G2RadarClickItem.prototype.ev;
}
class G2RadarComponent {
// #endregion
/**
* @param {?} cdr
* @param {?} ngZone
* @param {?} configSrv
* @param {?} platform
*/
constructor(cdr, ngZone, configSrv, platform) {
this.cdr = cdr;
this.ngZone = ngZone;
this.platform = platform;
this.legendData = [];
// #region fields
this.delay = 0;
this.height = 0;
this.padding = [44, 30, 16, 30];
this.hasLegend = true;
this.tickCount = 4;
this.data = [];
this.colors = ['#1890FF', '#FACC14', '#2FC25B', '#8543E0', '#F04864', '#13C2C2', '#fa8c16', '#a0d911'];
this.clickItem = new EventEmitter();
configSrv.attachKey(this, 'chart', 'theme');
}
/**
* @return {?}
*/
get chart() {
return this._chart;
}
/**
* @private
* @return {?}
*/
getHeight() {
return this.height - (this.hasLegend ? 80 : 22);
}
/**
* @private
* @return {?}
*/
install() {
const { node, padding, theme } = this;
/** @type {?} */
const chart = (this._chart = new Chart({
container: node.nativeElement,
autoFit: true,
height: this.getHeight(),
padding,
theme,
}));
chart.coordinate('polar');
chart.legend(false);
chart.axis('label', {
line: null,
label: {
offset: 8,
},
grid: {
line: {
style: {
stroke: '#e9e9e9',
lineWidth: 1,
lineDash: [0, 0],
},
},
},
});
chart.axis('value', {
grid: {
line: {
type: 'polygon',
style: {
stroke: '#e9e9e9',
lineWidth: 1,
lineDash: [0, 0],
},
},
},
});
chart.filter('name', (/**
* @param {?} name
* @return {?}
*/
(name) => {
/** @type {?} */
const legendItem = this.legendData.find((/**
* @param {?} w
* @return {?}
*/
w => w.name === name));
return legendItem ? legendItem.checked !== false : true;
}));
chart.line().position('label*value');
chart.point().position('label*value').shape('circle').size(3);
chart.render();
chart.on(`point: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, colors, tickCount } = this;
if (!_chart || !data || data.length <= 0)
return;
_chart.height = this.getHeight();
_chart.padding = padding;
_chart.scale({
value: {
min: 0,
tickCount,
},
});
_chart.geometries.forEach((/**
* @param {?} g
* @return {?}
*/
g => g.color('name', colors)));
_chart.changeData(data);
_chart.render();
this.ngZone.run((/**
* @return {?}
*/
() => this.genLegend()));
}
/**
* @private
* @return {?}
*/
genLegend() {
const { hasLegend, cdr, _chart } = this;
if (!hasLegend)
return;
this.legendData = _chart.geometries[0].dataArray.map((/**
* @param {?} item
* @return {?}
*/
item => {
/** @type {?} */
const origin = item[0]._origin;
/** @type {?} */
const result = {
name: origin.name,
color: item[0].color,
checked: true,
value: item.reduce((/**
* @param {?} p
* @param {?} n
* @return {?}
*/
(p, n) => p + n._origin.value), 0),
};
return result;
}));
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.legendData.forEach((/**
* @param {?} i
* @return {?}
*/
i => (i.checked = true)));
this.ngZone.runOutsideAngular((/**
* @return {?}
*/
() => this.attachChart()));
}
/**
* @return {?}
*/
ngOnDestroy() {
if (this._chart) {
this.ngZone.runOutsideAngular((/**
* @return {?}
*/
() => this._chart.destroy()));
}
}
}
G2RadarComponent.decorators = [
{ type: Component, args: [{
selector: 'g2-radar',
exportAs: 'g2Radar',
template: "<ng-container *nzStringTemplateOutlet=\"title\">\n <h4>{{ title }}</h4>\n</ng-container>\n<div #container></div>\n<div nz-row class=\"g2-radar__legend\" *ngIf=\"hasLegend\">\n <div nz-col [nzSpan]=\"24 / legendData.length\" *ngFor=\"let i of legendData; let idx = index\" (click)=\"_click(idx)\" class=\"g2-radar__legend-item\">\n <i class=\"g2-radar__legend-dot\" [ngStyle]=\"{ 'background-color': !i.checked ? '#aaa' : i.color }\"></i>\n {{ i.name }}\n <h6 class=\"g2-radar__legend-title\">{{ i.value }}</h6>\n </div>\n</div>\n",
host: {
'[style.height.px]': 'height',
'[class.g2-radar]': 'true',
},
preserveWhitespaces: false,
changeDetection: ChangeDetectionStrategy.OnPush,
encapsulation: ViewEncapsulation.None
}] }
];
/** @nocollapse */
G2RadarComponent.ctorParameters = () => [
{ type: ChangeDetectorRef },
{ type: NgZone },
{ type: OhayoConfigService },
{ type: Platform }
];
G2RadarComponent.propDecorators = {
node: [{ type: ViewChild, args: ['container', { static: true },] }],
delay: [{ type: Input }],
title: [{ type: Input }],
height: [{ type: Input }],
padding: [{ type: Input }],
hasLegend: [{ type: Input }],
tickCount: [{ type: Input }],
data: [{ type: Input }],
colors: [{ type: Input }],
theme: [{ type: Input }],
clickItem: [{ type: Output }]
};
__decorate([
InputNumber(),
__metadata("design:type", Object)
], G2RadarComponent.prototype, "delay", void 0);
__decorate([
InputNumber(),
__metadata("design:type", Object)
], G2RadarComponent.prototype, "height", void 0);
__decorate([
InputBoolean(),
__metadata("design:type", Object)
], G2RadarComponent.prototype, "hasLegend", void 0);
__decorate([
InputNumber(),
__metadata("design:type", Object)
], G2RadarComponent.prototype, "tickCount", void 0);
if (false) {
/** @type {?} */
G2RadarComponent.ngAcceptInputType_delay;
/** @type {?} */
G2RadarComponent.ngAcceptInputType_height;
/** @type {?} */
G2RadarComponent.ngAcceptInputType_hasLegend;
/** @type {?} */
G2RadarComponent.ngAcceptInputType_tickCount;
/**
* @type {?}
* @private
*/
G2RadarComponent.prototype.node;
/**
* @type {?}
* @private
*/
G2RadarComponent.prototype._chart;
/** @type {?} */
G2RadarComponent.prototype.legendData;
/** @type {?} */
G2RadarComponent.prototype.delay;
/** @type {?} */
G2RadarComponent.prototype.title;
/** @type {?} */
G2RadarComponent.prototype.height;
/** @type {?} */
G2RadarComponent.prototype.padding;
/** @type {?} */
G2RadarComponent.prototype.hasLegend;
/** @type {?} */
G2RadarComponent.prototype.tickCount;
/** @type {?} */
G2RadarComponent.prototype.data;
/** @type {?} */
G2RadarComponent.prototype.colors;
/** @type {?} */
G2RadarComponent.prototype.theme;
/** @type {?} */
G2RadarComponent.prototype.clickItem;
/**
* @type {?}
* @private
*/
G2RadarComponent.prototype.cdr;
/**
* @type {?}
* @private
*/
G2RadarComponent.prototype.ngZone;
/**
* @type {?}
* @private
*/
G2RadarComponent.prototype.platform;
}
/**
* @fileoverview added by tsickle
* Generated from: radar.module.ts
* @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingRequire,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
/** @type {?} */
const COMPONENTS = [G2RadarComponent];
class G2RadarModule {
}
G2RadarModule.decorators = [
{ type: NgModule, args: [{
imports: [CommonModule, OhayoUtilModule, NzGridModule, 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: radar.ts
* @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingRequire,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
export { G2RadarComponent, G2RadarModule };
//# sourceMappingURL=radar.js.map