From c384f44c0cce599a965a765f88d38baed7a2a05c Mon Sep 17 00:00:00 2001 From: Gaurav Kumar Date: Wed, 29 Oct 2025 17:01:50 +0530 Subject: [PATCH] doughnut --- .../doughnut-chart.component.html | 5 +- .../doughnut-chart.component.ts | 113 ++++++++++++++---- 2 files changed, 90 insertions(+), 28 deletions(-) diff --git a/frontend/angular-clarity-master/src/app/modules/main/builder/dashboardnew/gadgets/doughnut-chart/doughnut-chart.component.html b/frontend/angular-clarity-master/src/app/modules/main/builder/dashboardnew/gadgets/doughnut-chart/doughnut-chart.component.html index 600d4b2..1acc617 100644 --- a/frontend/angular-clarity-master/src/app/modules/main/builder/dashboardnew/gadgets/doughnut-chart/doughnut-chart.component.html +++ b/frontend/angular-clarity-master/src/app/modules/main/builder/dashboardnew/gadgets/doughnut-chart/doughnut-chart.component.html @@ -284,13 +284,12 @@
-
+

No chart data available

-
+
{{ label }} diff --git a/frontend/angular-clarity-master/src/app/modules/main/builder/dashboardnew/gadgets/doughnut-chart/doughnut-chart.component.ts b/frontend/angular-clarity-master/src/app/modules/main/builder/dashboardnew/gadgets/doughnut-chart/doughnut-chart.component.ts index 6faa761..09fc78e 100644 --- a/frontend/angular-clarity-master/src/app/modules/main/builder/dashboardnew/gadgets/doughnut-chart/doughnut-chart.component.ts +++ b/frontend/angular-clarity-master/src/app/modules/main/builder/dashboardnew/gadgets/doughnut-chart/doughnut-chart.component.ts @@ -148,7 +148,10 @@ export class DoughnutChartComponent implements OnInit, OnChanges, AfterViewCheck // Validate initial data this.validateChartData(); - this.fetchChartData(); + // Only fetch data if we have the required inputs, otherwise show default data + if (this.table && this.xAxis && this.yAxis) { + this.fetchChartData(); + } } /** @@ -237,6 +240,12 @@ export class DoughnutChartComponent implements OnInit, OnChanges, AfterViewCheck console.log('Chart configuration changed, fetching new data'); this.fetchChartData(); } + + // If we have the required inputs and haven't fetched data yet, fetch it + if ((xAxisChanged || yAxisChanged || tableChanged) && this.table && this.xAxis && this.yAxis && !this.isFetchingData) { + console.log('Required inputs available, fetching data'); + this.fetchChartData(); + } } ngAfterViewChecked() { @@ -674,11 +683,31 @@ export class DoughnutChartComponent implements OnInit, OnChanges, AfterViewCheck // Backend has already filtered the data, just display it this.noDataAvailable = data.chartLabels.length === 0; this.doughnutChartLabels = data.chartLabels; + + // Handle different data structures + let chartDataValues; + if (Array.isArray(data.chartData)) { + // If chartData is already an array of values + if (data.chartData.length > 0 && typeof data.chartData[0] !== 'object') { + chartDataValues = data.chartData; + } + // If chartData is an array with one object containing the data + else if (data.chartData.length > 0 && data.chartData[0].data) { + chartDataValues = data.chartData[0].data; + } + // Default case + else { + chartDataValues = data.chartData; + } + } else { + chartDataValues = [data.chartData]; + } + this.doughnutChartData = [ { - data: data.chartData, - backgroundColor: this.chartColors.slice(0, data.chartData.length), - hoverBackgroundColor: this.chartColors.slice(0, data.chartData.length) + data: chartDataValues, + backgroundColor: this.chartColors.slice(0, chartDataValues.length), + hoverBackgroundColor: this.chartColors.slice(0, chartDataValues.length) } ]; console.log('Updated doughnut chart with data:', { labels: this.doughnutChartLabels, data: this.doughnutChartData }); @@ -691,12 +720,21 @@ export class DoughnutChartComponent implements OnInit, OnChanges, AfterViewCheck } else { console.warn('Received data does not have expected structure', data); this.noDataAvailable = true; - this.doughnutChartLabels = []; + // Keep default data instead of empty arrays + this.doughnutChartLabels = ["Category A", "Category B", "Category C"]; this.doughnutChartData = [ { - data: [], - backgroundColor: [], - hoverBackgroundColor: [] + data: [30, 50, 20], + backgroundColor: [ + '#FF6384', + '#36A2EB', + '#FFCE56' + ], + hoverBackgroundColor: [ + '#FF6384', + '#36A2EB', + '#FFCE56' + ] } ]; } @@ -707,12 +745,26 @@ export class DoughnutChartComponent implements OnInit, OnChanges, AfterViewCheck (error) => { console.error('Error fetching doughnut chart data:', error); this.noDataAvailable = true; - this.doughnutChartLabels = []; - this.doughnutChartData = []; + // Keep default data in case of error + this.doughnutChartLabels = ["Category A", "Category B", "Category C"]; + this.doughnutChartData = [ + { + data: [30, 50, 20], + backgroundColor: [ + '#FF6384', + '#36A2EB', + '#FFCE56' + ], + hoverBackgroundColor: [ + '#FF6384', + '#36A2EB', + '#FFCE56' + ] + } + ]; // Reset flags after fetching this.isFetchingData = false; this.isLoading = false; - // Keep default data in case of error } ); } else { @@ -869,11 +921,31 @@ export class DoughnutChartComponent implements OnInit, OnChanges, AfterViewCheck // Backend has already filtered the data, just display it this.noDataAvailable = data.chartLabels.length === 0; this.doughnutChartLabels = data.chartLabels; + + // Handle different data structures + let chartDataValues; + if (Array.isArray(data.chartData)) { + // If chartData is already an array of values + if (data.chartData.length > 0 && typeof data.chartData[0] !== 'object') { + chartDataValues = data.chartData; + } + // If chartData is an array with one object containing the data + else if (data.chartData.length > 0 && data.chartData[0].data) { + chartDataValues = data.chartData[0].data; + } + // Default case + else { + chartDataValues = data.chartData; + } + } else { + chartDataValues = [data.chartData]; + } + this.doughnutChartData = [ { - data: data.chartData, - backgroundColor: this.chartColors.slice(0, data.chartData.length), - hoverBackgroundColor: this.chartColors.slice(0, data.chartData.length) + data: chartDataValues, + backgroundColor: this.chartColors.slice(0, chartDataValues.length), + hoverBackgroundColor: this.chartColors.slice(0, chartDataValues.length) } ]; console.log('Updated doughnut chart with drilldown data:', { labels: this.doughnutChartLabels, data: this.doughnutChartData }); @@ -890,14 +962,7 @@ export class DoughnutChartComponent implements OnInit, OnChanges, AfterViewCheck } else { console.warn('Drilldown received data does not have expected structure', data); this.noDataAvailable = true; - this.doughnutChartLabels = []; - this.doughnutChartData = [ - { - data: [], - backgroundColor: [], - hoverBackgroundColor: [] - } - ]; + // Keep current data instead of empty arrays // Set loading state to false this.isLoading = false; } @@ -905,11 +970,9 @@ export class DoughnutChartComponent implements OnInit, OnChanges, AfterViewCheck (error) => { console.error('Error fetching drilldown data:', error); this.noDataAvailable = true; - this.doughnutChartLabels = []; - this.doughnutChartData = []; + // Keep current data in case of error // Set loading state to false this.isLoading = false; - // Keep current data in case of error } );