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
252 lines (247 loc) • 9.23 kB
JavaScript
import { isPlatformBrowser, CommonModule } from '@angular/common';
import * as i0 from '@angular/core';
import { EventEmitter, PLATFORM_ID, booleanAttribute, Component, ChangeDetectionStrategy, ViewEncapsulation, Inject, Input, Output, 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 = function UIChart_Factory(__ngFactoryType__) { return new (__ngFactoryType__ || UIChart)(i0.ɵɵdirectiveInject(PLATFORM_ID), i0.ɵɵdirectiveInject(i0.ElementRef), i0.ɵɵdirectiveInject(i0.NgZone)); };
static ɵcmp = /*@__PURE__*/ i0.ɵɵdefineComponent({ type: UIChart, selectors: [["p-chart"]], inputs: { type: "type", plugins: "plugins", width: "width", height: "height", responsive: [2, "responsive", "responsive", booleanAttribute], ariaLabel: "ariaLabel", ariaLabelledBy: "ariaLabelledBy", data: "data", options: "options" }, outputs: { onDataSelect: "onDataSelect" }, features: [i0.ɵɵInputTransformsFeature], decls: 2, vars: 8, consts: [[2, "position", "relative"], ["role", "img", 3, "click"]], template: function UIChart_Template(rf, ctx) { if (rf & 1) {
i0.ɵɵelementStart(0, "div", 0)(1, "canvas", 1);
i0.ɵɵlistener("click", function UIChart_Template_canvas_click_1_listener($event) { return ctx.onCanvasClick($event); });
i0.ɵɵelementEnd()();
} if (rf & 2) {
i0.ɵɵstyleProp("width", ctx.responsive && !ctx.width ? null : ctx.width)("height", ctx.responsive && !ctx.height ? null : ctx.height);
i0.ɵɵadvance();
i0.ɵɵattribute("aria-label", ctx.ariaLabel)("aria-labelledby", ctx.ariaLabelledBy)("width", ctx.responsive && !ctx.width ? null : ctx.width)("height", ctx.responsive && !ctx.height ? null : ctx.height);
} }, dependencies: [CommonModule, SharedModule], encapsulation: 2, changeDetection: 0 });
}
(() => { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(UIChart, [{
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
}]
}], () => [{ type: undefined, decorators: [{
type: Inject,
args: [PLATFORM_ID]
}] }, { type: i0.ElementRef }, { type: i0.NgZone }], { 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
}] }); })();
(() => { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassDebugInfo(UIChart, { className: "UIChart", filePath: "chart.ts", lineNumber: 22 }); })();
class ChartModule {
static ɵfac = function ChartModule_Factory(__ngFactoryType__) { return new (__ngFactoryType__ || ChartModule)(); };
static ɵmod = /*@__PURE__*/ i0.ɵɵdefineNgModule({ type: ChartModule });
static ɵinj = /*@__PURE__*/ i0.ɵɵdefineInjector({ imports: [UIChart, SharedModule, SharedModule] });
}
(() => { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(ChartModule, [{
type: NgModule,
args: [{
imports: [UIChart, SharedModule],
exports: [UIChart, SharedModule]
}]
}], null, null); })();
(function () { (typeof ngJitMode === "undefined" || ngJitMode) && i0.ɵɵsetNgModuleScope(ChartModule, { 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 = /*@__PURE__*/ (() => { let ɵChartStyle_BaseFactory; return function ChartStyle_Factory(__ngFactoryType__) { return (ɵChartStyle_BaseFactory || (ɵChartStyle_BaseFactory = i0.ɵɵgetInheritedFactory(ChartStyle)))(__ngFactoryType__ || ChartStyle); }; })();
static ɵprov = /*@__PURE__*/ i0.ɵɵdefineInjectable({ token: ChartStyle, factory: ChartStyle.ɵfac });
}
(() => { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(ChartStyle, [{
type: Injectable
}], null, null); })();
/**
*
* 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