diff --git a/src/app/components/chart/chart.interface.ts b/src/app/components/chart/chart.interface.ts new file mode 100644 index 00000000000..5beab80e0b5 --- /dev/null +++ b/src/app/components/chart/chart.interface.ts @@ -0,0 +1,16 @@ +import { TemplateRef } from '@angular/core'; + +/** + * Defines valid templates in Chart. + * @group Templates + */ +export interface ChartTemplates { + /** + * Custom template of header. + */ + header(): TemplateRef; + /** + * Custom template of footer. + */ + footer(): TemplateRef; +} diff --git a/src/app/components/chart/chart.ts b/src/app/components/chart/chart.ts index 25a0e7a8b11..f4422f238e1 100755 --- a/src/app/components/chart/chart.ts +++ b/src/app/components/chart/chart.ts @@ -1,5 +1,7 @@ -import { NgModule, Component, ElementRef, AfterViewInit, OnDestroy, Input, Output, EventEmitter, ChangeDetectionStrategy, ViewEncapsulation, Inject, PLATFORM_ID, NgZone, booleanAttribute } from '@angular/core'; +import { ContentChildren, QueryList, NgModule, Component, ElementRef, AfterViewInit, OnDestroy, Input, Output, EventEmitter, ChangeDetectionStrategy, ViewEncapsulation, Inject, PLATFORM_ID, NgZone, booleanAttribute, TemplateRef, AfterContentInit } from '@angular/core'; import { CommonModule, isPlatformBrowser } from '@angular/common'; +import { PrimeTemplate, SharedModule } from 'primeng/api'; +import { ButtonModule } from 'primeng/button'; import Chart from 'chart.js/auto'; /** * Chart groups a collection of contents in tabs. @@ -9,7 +11,15 @@ import Chart from 'chart.js/auto'; selector: 'p-chart', template: `
+
+ + +
+
+ + +
`, changeDetection: ChangeDetectionStrategy.OnPush, @@ -18,7 +28,7 @@ import Chart from 'chart.js/auto'; class: 'p-element' } }) -export class UIChart implements AfterViewInit, OnDestroy { +export class UIChart implements AfterContentInit, AfterViewInit, OnDestroy { /** * Type of the chart. * @group Props @@ -82,6 +92,12 @@ export class UIChart implements AfterViewInit, OnDestroy { */ @Output() onDataSelect: EventEmitter = new EventEmitter(); + @ContentChildren(PrimeTemplate) templates: QueryList | undefined; + + headerTemplate: TemplateRef | undefined; + + footerTemplate: TemplateRef | undefined; + isBrowser: boolean = false; initialized: boolean | undefined; @@ -103,6 +119,20 @@ export class UIChart implements AfterViewInit, OnDestroy { this.initialized = true; } + ngAfterContentInit() { + (this.templates as QueryList).forEach((item) => { + switch (item.getType()) { + case 'header': + this.headerTemplate = item.template; + break; + + case 'footer': + this.footerTemplate = item.template; + break; + } + }); + } + onCanvasClick(event: Event) { if (this.chart) { const element = this.chart.getElementsAtEventForMode(event, 'nearest', { intersect: true }, false); @@ -125,7 +155,7 @@ export class UIChart implements AfterViewInit, OnDestroy { } this.zone.runOutsideAngular(() => { - this.chart = new Chart(this.el.nativeElement.children[0].children[0], { + this.chart = new Chart(this.el.nativeElement.children[0].children[1], { type: this.type, data: this.data, options: this.options, @@ -172,8 +202,8 @@ export class UIChart implements AfterViewInit, OnDestroy { } @NgModule({ - imports: [CommonModule], - exports: [UIChart], + imports: [CommonModule, ButtonModule], + exports: [UIChart, SharedModule, ButtonModule], declarations: [UIChart] }) export class ChartModule {} diff --git a/src/app/showcase/doc/apidoc/index.json b/src/app/showcase/doc/apidoc/index.json index 64913dd3ba0..14c44abe4c3 100644 --- a/src/app/showcase/doc/apidoc/index.json +++ b/src/app/showcase/doc/apidoc/index.json @@ -7058,6 +7058,26 @@ ] } } + }, + "interfaces": { + "components": {}, + "templates": { + "description": "Defines the templates used by the component.", + "values": [ + { + "parent": "chart", + "name": "header", + "parameters": [], + "description": "Custom template of header." + }, + { + "parent": "chart", + "name": "footer", + "parameters": [], + "description": "Custom template of footer." + } + ] + } } }, "checkbox": {