77 */
88
99/** Angular Imports */
10- import { Component , OnChanges , Input , inject } from '@angular/core' ;
10+ import { Component , OnChanges , OnInit , OnDestroy , Input , inject , ViewChild , ElementRef } from '@angular/core' ;
1111
1212/** Custom Services */
1313import { ReportsService } from '../../reports.service' ;
@@ -38,18 +38,56 @@ Chart.register(...registerables);
3838 NgStyle
3939 ]
4040} )
41- export class ChartComponent implements OnChanges {
41+ export class ChartComponent implements OnChanges , OnInit , OnDestroy {
4242 private reportsService = inject ( ReportsService ) ;
4343
4444 /** Run Report Data */
4545 @Input ( ) dataObject : any ;
46+ @ViewChild ( 'chartContainer' , { static : false } ) chartContainer : ElementRef ;
4647
4748 /** chart data object */
4849 chart : any ;
4950 /** substitute for resolver */
5051 hideOutput = true ;
5152 /** Data object for witching charts in view. */
5253 inputData : ChartData ;
54+ /** Tracks the currently selected chart type */
55+ selectedChartType : string = 'Pie' ;
56+ /** Resize listener */
57+ private resizeListener : any ;
58+
59+ /**
60+ * Initialize component and add resize listener.
61+ */
62+ ngOnInit ( ) {
63+ this . resizeListener = window . addEventListener ( 'resize' , ( ) => {
64+ this . resizeChart ( ) ;
65+ } ) ;
66+ }
67+
68+ /**
69+ * Clean up on component destroy.
70+ */
71+ ngOnDestroy ( ) {
72+ if ( this . resizeListener ) {
73+ window . removeEventListener ( 'resize' , this . resizeListener ) ;
74+ }
75+ if ( this . chart ) {
76+ this . chart . destroy ( ) ;
77+ }
78+ }
79+
80+ /**
81+ * Resize and redraw chart when window size changes.
82+ */
83+ resizeChart ( ) {
84+ if ( this . chart ) {
85+ // Resize the chart canvas
86+ setTimeout ( ( ) => {
87+ this . chart . resize ( ) ;
88+ } , 100 ) ;
89+ }
90+ }
5391
5492 /**
5593 * Fetches run report data post changes in run report form.
@@ -63,11 +101,31 @@ export class ChartComponent implements OnChanges {
63101 . getChartRunReportData ( this . dataObject . report . name , this . dataObject . formData )
64102 . subscribe ( ( response : ChartData ) => {
65103 this . inputData = response ;
104+ this . selectedChartType = 'Pie' ;
66105 this . setPieChart ( this . inputData ) ;
67106 this . hideOutput = false ;
68107 } ) ;
69108 }
70109
110+ /**
111+ * Handles chart type selection and renders the selected chart.
112+ * @param {string } chartType The type of chart to display
113+ */
114+ selectChart ( chartType : string ) {
115+ this . selectedChartType = chartType ;
116+ switch ( chartType ) {
117+ case 'Bar' :
118+ this . setBarChart ( this . inputData ) ;
119+ break ;
120+ case 'Pie' :
121+ this . setPieChart ( this . inputData ) ;
122+ break ;
123+ case 'Polar' :
124+ this . setPolarAreaChart ( this . inputData ) ;
125+ break ;
126+ }
127+ }
128+
71129 /**
72130 * Creates instance of chart.js pie chart.
73131 * Refer: https://www.chartjs.org/docs/latest/charts/doughnut.html for configuration details.
@@ -89,6 +147,8 @@ export class ChartComponent implements OnChanges {
89147 ]
90148 } ,
91149 options : {
150+ responsive : true ,
151+ maintainAspectRatio : false ,
92152 plugins : {
93153 title : {
94154 display : true ,
@@ -120,6 +180,8 @@ export class ChartComponent implements OnChanges {
120180 ]
121181 } ,
122182 options : {
183+ responsive : true ,
184+ maintainAspectRatio : false ,
123185 plugins : {
124186 legend : { display : false }
125187 } ,
@@ -138,6 +200,46 @@ export class ChartComponent implements OnChanges {
138200 } ) ;
139201 }
140202
203+ /**
204+ * Creates instance of chart.js polar area chart.
205+ * Refer: https://www.chartjs.org/docs/latest/charts/polar.html for configuration details.
206+ */
207+ setPolarAreaChart ( inputData : ChartData ) {
208+ if ( this . chart ) {
209+ this . chart . destroy ( ) ;
210+ }
211+ this . chart = new Chart ( 'output' , {
212+ type : 'polarArea' ,
213+ data : {
214+ labels : inputData . keys ,
215+ datasets : [
216+ {
217+ label : inputData . valuesLabel ,
218+ data : inputData . values ,
219+ backgroundColor : this . randomColorArray ( inputData . values . length ) ,
220+ borderColor : this . randomColorArray ( inputData . values . length )
221+ }
222+ ]
223+ } ,
224+ options : {
225+ responsive : true ,
226+ maintainAspectRatio : false ,
227+ plugins : {
228+ title : {
229+ display : true ,
230+ text : inputData . keysLabel
231+ } ,
232+ legend : { display : true }
233+ } ,
234+ scales : {
235+ r : {
236+ min : 0
237+ }
238+ }
239+ }
240+ } ) ;
241+ }
242+
141243 /**
142244 * Generates bar/pie-slice colors array for dynamic charts.
143245 * @param {number } length Length of dataset array.
0 commit comments