UNPKG

primeng

Version:

PrimeNG is an open source UI library for Angular featuring a rich set of 80+ components, a theme designer, various theme alternatives such as Material, Bootstrap, Tailwind, premium templates and professional support. In addition, it integrates with PrimeB

246 lines (241 loc) 9.03 kB
import { isPlatformBrowser, CommonModule } from '@angular/common'; import * as i0 from '@angular/core'; import { EventEmitter, PLATFORM_ID, booleanAttribute, Output, Input, Inject, ViewEncapsulation, ChangeDetectionStrategy, Component, NgModule, Injectable } from '@angular/core'; import Chart from 'chart.js/auto'; import { SharedModule } from 'primeng/api'; import { BaseStyle } from 'primeng/base'; /** * Chart groups a collection of contents in tabs. * @group Components */ 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: "19.2.2", 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: "19.2.2", type: UIChart, isStandalone: true, 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" }, 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, dependencies: [{ kind: "ngmodule", type: CommonModule }, { kind: "ngmodule", type: SharedModule }], changeDetection: i0.ChangeDetectionStrategy.OnPush, encapsulation: i0.ViewEncapsulation.None }); } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.2", ngImport: i0, type: UIChart, decorators: [{ type: Component, args: [{ selector: 'p-chart', standalone: true, imports: [CommonModule, SharedModule], 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 }] }], 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 }] } }); class ChartModule { static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.2", ngImport: i0, type: ChartModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule }); static ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "19.2.2", ngImport: i0, type: ChartModule, imports: [UIChart, SharedModule], exports: [UIChart, SharedModule] }); static ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "19.2.2", ngImport: i0, type: ChartModule, imports: [UIChart, SharedModule, SharedModule] }); } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.2", ngImport: i0, type: ChartModule, decorators: [{ type: NgModule, args: [{ imports: [UIChart, SharedModule], exports: [UIChart, SharedModule] }] }] }); const inlineStyles = { root: { position: 'relative' } }; const classes = { root: 'p-chart' }; class ChartStyle extends BaseStyle { name = 'chart'; classes = classes; inlineStyles = inlineStyles; static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.2", ngImport: i0, type: ChartStyle, deps: null, target: i0.ɵɵFactoryTarget.Injectable }); static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "19.2.2", ngImport: i0, type: ChartStyle }); } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.2", ngImport: i0, type: ChartStyle, decorators: [{ type: Injectable }] }); /** * * Chart groups a collection of contents in tabs. * * [Live Demo](https://www.primeng.org/chart/) * * @module chartstyle * */ var ChartClasses; (function (ChartClasses) { /** * Class name of the root element */ ChartClasses["root"] = "p-chart"; })(ChartClasses || (ChartClasses = {})); /** * Generated bundle index. Do not edit. */ export { ChartClasses, ChartModule, ChartStyle, UIChart }; //# sourceMappingURL=primeng-chart.mjs.map