125 lines
3.5 KiB
JavaScript
125 lines
3.5 KiB
JavaScript
import { EventEmitter, Component, ChangeDetectionStrategy, ViewEncapsulation, ElementRef, Input, Output, NgModule } from '@angular/core';
|
|
import { CommonModule } from '@angular/common';
|
|
import * as Chart from 'chart.js';
|
|
|
|
class UIChart {
|
|
constructor(el) {
|
|
this.el = el;
|
|
this.plugins = [];
|
|
this.responsive = true;
|
|
this.onDataSelect = new EventEmitter();
|
|
this._options = {};
|
|
}
|
|
get data() {
|
|
return this._data;
|
|
}
|
|
set data(val) {
|
|
this._data = val;
|
|
this.reinit();
|
|
}
|
|
get options() {
|
|
return this._options;
|
|
}
|
|
set options(val) {
|
|
this._options = val;
|
|
this.reinit();
|
|
}
|
|
ngAfterViewInit() {
|
|
this.initChart();
|
|
this.initialized = true;
|
|
}
|
|
onCanvasClick(event) {
|
|
if (this.chart) {
|
|
let element = this.chart.getElementAtEvent(event);
|
|
let dataset = this.chart.getDatasetAtEvent(event);
|
|
if (element && element[0] && dataset) {
|
|
this.onDataSelect.emit({ originalEvent: event, element: element[0], dataset: dataset });
|
|
}
|
|
}
|
|
}
|
|
initChart() {
|
|
let opts = this.options || {};
|
|
opts.responsive = this.responsive;
|
|
// allows chart to resize in responsive mode
|
|
if (opts.responsive && (this.height || this.width)) {
|
|
opts.maintainAspectRatio = false;
|
|
}
|
|
this.chart = new Chart(this.el.nativeElement.children[0].children[0], {
|
|
type: this.type,
|
|
data: this.data,
|
|
options: this.options,
|
|
plugins: this.plugins
|
|
});
|
|
}
|
|
getCanvas() {
|
|
return this.el.nativeElement.children[0].children[0];
|
|
}
|
|
getBase64Image() {
|
|
return this.chart.toBase64Image();
|
|
}
|
|
generateLegend() {
|
|
if (this.chart) {
|
|
return this.chart.generateLegend();
|
|
}
|
|
}
|
|
refresh() {
|
|
if (this.chart) {
|
|
this.chart.update();
|
|
}
|
|
}
|
|
reinit() {
|
|
if (this.chart) {
|
|
this.chart.destroy();
|
|
this.initChart();
|
|
}
|
|
}
|
|
ngOnDestroy() {
|
|
if (this.chart) {
|
|
this.chart.destroy();
|
|
this.initialized = false;
|
|
this.chart = null;
|
|
}
|
|
}
|
|
}
|
|
UIChart.decorators = [
|
|
{ type: Component, args: [{
|
|
selector: 'p-chart',
|
|
template: `
|
|
<div style="position:relative" [style.width]="responsive && !width ? null : width" [style.height]="responsive && !height ? null : height">
|
|
<canvas [attr.width]="responsive && !width ? null : width" [attr.height]="responsive && !height ? null : height" (click)="onCanvasClick($event)"></canvas>
|
|
</div>
|
|
`,
|
|
changeDetection: ChangeDetectionStrategy.OnPush,
|
|
encapsulation: ViewEncapsulation.None
|
|
},] }
|
|
];
|
|
UIChart.ctorParameters = () => [
|
|
{ type: ElementRef }
|
|
];
|
|
UIChart.propDecorators = {
|
|
type: [{ type: Input }],
|
|
plugins: [{ type: Input }],
|
|
width: [{ type: Input }],
|
|
height: [{ type: Input }],
|
|
responsive: [{ type: Input }],
|
|
onDataSelect: [{ type: Output }],
|
|
data: [{ type: Input }],
|
|
options: [{ type: Input }]
|
|
};
|
|
class ChartModule {
|
|
}
|
|
ChartModule.decorators = [
|
|
{ type: NgModule, args: [{
|
|
imports: [CommonModule],
|
|
exports: [UIChart],
|
|
declarations: [UIChart]
|
|
},] }
|
|
];
|
|
|
|
/**
|
|
* Generated bundle index. Do not edit.
|
|
*/
|
|
|
|
export { ChartModule, UIChart };
|
|
//# sourceMappingURL=primeng-chart.js.map
|