ohayolibs
Version:
Ohayo is a set of essential modules for ohayojp.
149 lines (131 loc) • 3.89 kB
text/typescript
import { Platform } from '@angular/cdk/platform';
import {
ChangeDetectionStrategy,
Component,
ElementRef,
EventEmitter,
Input,
NgZone,
OnChanges,
OnDestroy,
OnInit,
Output,
ViewEncapsulation,
} from '@angular/core';
import { Chart, Event, Types } from '@antv/g2';
import { OhayoConfigService, InputNumber, NumberInput } from '@ohayo/util';
import { NzSafeAny } from 'ng-zorro-antd/core/types';
export interface G2MiniBarData {
x: any;
y: any;
[key: string]: any;
}
export interface G2MiniBarClickItem {
item: G2MiniBarData;
ev: Event;
}
export class G2MiniBarComponent implements OnInit, OnChanges, OnDestroy {
static ngAcceptInputType_delay: NumberInput;
static ngAcceptInputType_height: NumberInput;
static ngAcceptInputType_borderWidth: NumberInput;
private _chart: Chart;
get chart(): Chart {
return this._chart;
}
// #region fields
delay = 0;
color = '#1890FF';
height = 0;
borderWidth = 5;
padding: number | number[] | 'auto' = [8, 8, 8, 8];
data: G2MiniBarData[] = [];
yTooltipSuffix = '';
tooltipType: 'mini' | 'default' = 'default';
theme: string | Types.LooseObject;
clickItem = new EventEmitter<G2MiniBarClickItem>();
// #endregion
constructor(private el: ElementRef, private ngZone: NgZone, configSrv: OhayoConfigService, private platform: Platform) {
configSrv.attachKey(this, 'chart', 'theme');
}
private install(): void {
const { el, height, padding, yTooltipSuffix, tooltipType, theme } = this;
const chart = (this._chart = new Chart({
container: el.nativeElement,
autoFit: true,
height,
padding,
theme,
}));
chart.scale({
x: {
type: 'cat',
},
y: {
min: 0,
},
});
chart.legend(false);
chart.axis(false);
const tooltipOption: Types.TooltipOption = {
showTitle: false,
showMarkers: true,
showCrosshairs: false,
enterable: true,
domStyles: {
'g2-tooltip': { padding: '0px' },
'g2-tooltip-title': { display: 'none' },
'g2-tooltip-list-item': { margin: '4px' },
},
};
if (tooltipType === 'mini') {
tooltipOption.position = 'top';
tooltipOption.domStyles!['g2-tooltip'] = { padding: '0px', backgroundColor: 'transparent', boxShadow: 'none' };
tooltipOption.itemTpl = `<li>{value}</li>`;
tooltipOption.offset = 0;
}
chart.tooltip(tooltipOption);
chart
.interval()
.position('x*y')
.tooltip('x*y', (x: NzSafeAny, y: NzSafeAny) => ({ name: x, value: y + yTooltipSuffix }));
chart.on(`interval:click`, (ev: Event) => {
this.ngZone.run(() => this.clickItem.emit({ item: ev.data?.data, ev }));
});
chart.render();
this.attachChart();
}
private attachChart(): void {
const { _chart, height, padding, data, color, borderWidth } = this;
if (!_chart || !data || data.length <= 0) return;
_chart.geometries[0].size(borderWidth).color(color);
_chart.height = height;
_chart.padding = padding;
_chart.changeData(data);
_chart.render();
}
ngOnInit(): void {
if (!this.platform.isBrowser) {
return;
}
this.ngZone.runOutsideAngular(() => setTimeout(() => this.install(), this.delay));
}
ngOnChanges(): void {
this.ngZone.runOutsideAngular(() => this.attachChart());
}
ngOnDestroy(): void {
if (this._chart) {
this.ngZone.runOutsideAngular(() => this._chart.destroy());
}
}
}