205 lines
19 KiB
JavaScript
205 lines
19 KiB
JavaScript
|
import { NgModule, Component, Input, Output, EventEmitter, ChangeDetectionStrategy, ViewEncapsulation, Inject, PLATFORM_ID, booleanAttribute } from '@angular/core';
|
||
|
import { CommonModule, isPlatformBrowser } from '@angular/common';
|
||
|
import Chart from 'chart.js/auto';
|
||
|
import * as i0 from "@angular/core";
|
||
|
/**
|
||
|
* Chart groups a collection of contents in tabs.
|
||
|
* @group Components
|
||
|
*/
|
||
|
export class UIChart {
|
||
|
platformId;
|
||
|
el;
|
||
|
zone;
|
||
|
/**
|
||
|
* Type of the chart.
|
||
|
* @group Props
|
||
|
*/
|
||
|
type;
|
||
|
/**
|
||
|
* Array of per-chart plugins to customize the chart behaviour.
|
||
|
* @group Props
|
||
|
*/
|
||
|
plugins = [];
|
||
|
/**
|
||
|
* Width of the chart.
|
||
|
* @group Props
|
||
|
*/
|
||
|
width;
|
||
|
/**
|
||
|
* Height of the chart.
|
||
|
* @group Props
|
||
|
*/
|
||
|
height;
|
||
|
/**
|
||
|
* Whether the chart is redrawn on screen size change.
|
||
|
* @group Props
|
||
|
*/
|
||
|
responsive = true;
|
||
|
/**
|
||
|
* Used to define a string that autocomplete attribute the current element.
|
||
|
* @group Props
|
||
|
*/
|
||
|
ariaLabel;
|
||
|
/**
|
||
|
* Establishes relationships between the component and label(s) where its value should be one or more element IDs.
|
||
|
* @group Props
|
||
|
*/
|
||
|
ariaLabelledBy;
|
||
|
/**
|
||
|
* Data to display.
|
||
|
* @group Props
|
||
|
*/
|
||
|
get data() {
|
||
|
return this._data;
|
||
|
}
|
||
|
set data(val) {
|
||
|
this._data = val;
|
||
|
this.reinit();
|
||
|
}
|
||
|
/**
|
||
|
* Options to customize the chart.
|
||
|
* @group Props
|
||
|
*/
|
||
|
get options() {
|
||
|
return this._options;
|
||
|
}
|
||
|
set options(val) {
|
||
|
this._options = val;
|
||
|
this.reinit();
|
||
|
}
|
||
|
/**
|
||
|
* Callback to execute when an element on chart is clicked.
|
||
|
* @group Emits
|
||
|
*/
|
||
|
onDataSelect = new EventEmitter();
|
||
|
isBrowser = false;
|
||
|
initialized;
|
||
|
_data;
|
||
|
_options = {};
|
||
|
chart;
|
||
|
constructor(platformId, el, zone) {
|
||
|
this.platformId = platformId;
|
||
|
this.el = el;
|
||
|
this.zone = zone;
|
||
|
}
|
||
|
ngAfterViewInit() {
|
||
|
this.initChart();
|
||
|
this.initialized = true;
|
||
|
}
|
||
|
onCanvasClick(event) {
|
||
|
if (this.chart) {
|
||
|
const element = this.chart.getElementsAtEventForMode(event, 'nearest', { intersect: true }, false);
|
||
|
const dataset = this.chart.getElementsAtEventForMode(event, 'dataset', { intersect: true }, false);
|
||
|
if (element && element[0] && dataset) {
|
||
|
this.onDataSelect.emit({ originalEvent: event, element: element[0], dataset: dataset });
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
initChart() {
|
||
|
if (isPlatformBrowser(this.platformId)) {
|
||
|
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.zone.runOutsideAngular(() => {
|
||
|
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;
|
||
|
}
|
||
|
}
|
||
|
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "17.3.7", ngImport: i0, type: UIChart, deps: [{ token: PLATFORM_ID }, { token: i0.ElementRef }, { token: i0.NgZone }], target: i0.ɵɵFactoryTarget.Component });
|
||
|
static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "16.1.0", version: "17.3.7", type: UIChart, selector: "p-chart", inputs: { type: "type", plugins: "plugins", width: "width", height: "height", responsive: ["responsive", "responsive", booleanAttribute], ariaLabel: "ariaLabel", ariaLabelledBy: "ariaLabelledBy", data: "data", options: "options" }, outputs: { onDataSelect: "onDataSelect" }, host: { classAttribute: "p-element" }, ngImport: i0, template: `
|
||
|
<div style="position:relative" [style.width]="responsive && !width ? null : width" [style.height]="responsive && !height ? null : height">
|
||
|
<canvas role="img" [attr.aria-label]="ariaLabel" [attr.aria-labelledby]="ariaLabelledBy" [attr.width]="responsive && !width ? null : width" [attr.height]="responsive && !height ? null : height" (click)="onCanvasClick($event)"></canvas>
|
||
|
</div>
|
||
|
`, isInline: true, changeDetection: i0.ChangeDetectionStrategy.OnPush, encapsulation: i0.ViewEncapsulation.None });
|
||
|
}
|
||
|
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "17.3.7", ngImport: i0, type: 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 role="img" [attr.aria-label]="ariaLabel" [attr.aria-labelledby]="ariaLabelledBy" [attr.width]="responsive && !width ? null : width" [attr.height]="responsive && !height ? null : height" (click)="onCanvasClick($event)"></canvas>
|
||
|
</div>
|
||
|
`,
|
||
|
changeDetection: ChangeDetectionStrategy.OnPush,
|
||
|
encapsulation: ViewEncapsulation.None,
|
||
|
host: {
|
||
|
class: 'p-element'
|
||
|
}
|
||
|
}]
|
||
|
}], ctorParameters: () => [{ type: undefined, decorators: [{
|
||
|
type: Inject,
|
||
|
args: [PLATFORM_ID]
|
||
|
}] }, { type: i0.ElementRef }, { type: i0.NgZone }], propDecorators: { type: [{
|
||
|
type: Input
|
||
|
}], plugins: [{
|
||
|
type: Input
|
||
|
}], width: [{
|
||
|
type: Input
|
||
|
}], height: [{
|
||
|
type: Input
|
||
|
}], responsive: [{
|
||
|
type: Input,
|
||
|
args: [{ transform: booleanAttribute }]
|
||
|
}], ariaLabel: [{
|
||
|
type: Input
|
||
|
}], ariaLabelledBy: [{
|
||
|
type: Input
|
||
|
}], data: [{
|
||
|
type: Input
|
||
|
}], options: [{
|
||
|
type: Input
|
||
|
}], onDataSelect: [{
|
||
|
type: Output
|
||
|
}] } });
|
||
|
export class ChartModule {
|
||
|
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "17.3.7", ngImport: i0, type: ChartModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
|
||
|
static ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "17.3.7", ngImport: i0, type: ChartModule, declarations: [UIChart], imports: [CommonModule], exports: [UIChart] });
|
||
|
static ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "17.3.7", ngImport: i0, type: ChartModule, imports: [CommonModule] });
|
||
|
}
|
||
|
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "17.3.7", ngImport: i0, type: ChartModule, decorators: [{
|
||
|
type: NgModule,
|
||
|
args: [{
|
||
|
imports: [CommonModule],
|
||
|
exports: [UIChart],
|
||
|
declarations: [UIChart]
|
||
|
}]
|
||
|
}] });
|
||
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiY2hhcnQuanMiLCJzb3VyY2VSb290IjoiIiwic291c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