ohayolibs
Version:
Ohayo is a set of essential modules for ohayojp.
182 lines (155 loc) • 4.72 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, BooleanInput, InputBoolean, InputNumber, NumberInput } from '@ohayo/util';
export interface G2MiniAreaData {
x: any;
y: any;
[key: string]: any;
}
export interface G2MiniAreaClickItem {
item: G2MiniAreaData;
ev: Event;
}
export class G2MiniAreaComponent implements OnInit, OnChanges, OnDestroy {
static ngAcceptInputType_delay: NumberInput;
static ngAcceptInputType_borderWidth: NumberInput;
static ngAcceptInputType_height: NumberInput;
static ngAcceptInputType_fit: BooleanInput;
static ngAcceptInputType_line: BooleanInput;
static ngAcceptInputType_animate: BooleanInput;
private _chart: Chart;
get chart(): Chart {
return this._chart;
}
// #region fields
delay = 0;
color = 'rgba(24, 144, 255, 0.2)';
borderColor = '#1890FF';
borderWidth = 2;
height = 56;
fit = true;
line = false;
animate = true;
xAxis: any;
yAxis: any;
padding: number | number[] | 'auto' = [8, 8, 8, 8];
data: G2MiniAreaData[] = [];
yTooltipSuffix = '';
tooltipType: 'mini' | 'default' = 'default';
theme: string | Types.LooseObject;
clickItem = new EventEmitter<G2MiniAreaClickItem>();
// #endregion
constructor(private el: ElementRef, private ngZone: NgZone, configSrv: OhayoConfigService, private platform: Platform) {
configSrv.attachKey(this, 'chart', 'theme');
}
private install(): void {
const { el, fit, height, padding, xAxis, yAxis, yTooltipSuffix, tooltipType, line, theme } = this;
const chart = (this._chart = new Chart({
container: el.nativeElement,
autoFit: fit,
height,
padding,
theme,
}));
if (!xAxis && !yAxis) {
chart.axis(false);
}
if (xAxis) {
chart.axis('x', xAxis);
} else {
chart.axis('x', false);
}
if (yAxis) {
chart.axis('y', yAxis);
} else {
chart.axis('y', false);
}
chart.legend(false);
const tooltipOption: Types.TooltipOption = {
showTitle: false,
showMarkers: true,
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
.area()
.position('x*y')
.tooltip('x*y', (x, y) => ({ name: x, value: y + yTooltipSuffix }))
.shape('smooth');
if (line) {
chart.line().position('x*y').shape('smooth').tooltip(false);
}
chart.on(`plot:click`, (ev: Event) => {
const records = this._chart.getSnapRecords({ x: ev.x, y: ev.y });
this.ngZone.run(() => this.clickItem.emit({ item: records[0]._origin, ev }));
});
chart.render();
this.attachChart();
}
private attachChart(): void {
const { _chart, line, fit, height, animate, padding, data, color, borderColor, borderWidth } = this;
if (!_chart || !data || data.length <= 0) {
return;
}
const geoms = _chart.geometries;
geoms.forEach(g => g.color(color));
if (line) {
geoms[1].color(borderColor).size(borderWidth);
}
_chart.autoFit = fit;
_chart.height = height;
_chart.animate(animate);
_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());
}
}
}