-
Notifications
You must be signed in to change notification settings - Fork 254
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4224 from cisagov/feat/CSET-2905
Analytics Chart for CPG - Feat/cset 2905
- Loading branch information
Showing
7 changed files
with
210 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
CSETWebNg/src/app/assessment/results/analytics-results/analytics-results.component.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<div class="white-panel oy-auto ox-auto d-flex flex-column flex-11a"> | ||
|
||
<div class="container"> | ||
<div class="container-fluid"> | ||
<div class="row"> | ||
<div class="col-sm"> | ||
<mat-card> | ||
<mat-card-header class="d-flex justify-content-center"><h2>Analytics</h2></mat-card-header> | ||
<div class="p-4"> | ||
<mat-button-toggle-group [(ngModel)]="dataType" (change)="toggleData($event)"> | ||
<mat-button-toggle value="mySector">My Sector</mat-button-toggle> | ||
<mat-button-toggle value="allSectors">All Sectors</mat-button-toggle> | ||
</mat-button-toggle-group> | ||
</div> | ||
<div style="display: block;"> | ||
<canvas #barCanvas></canvas> | ||
</div> | ||
</mat-card> | ||
</div> | ||
</div> | ||
</div> | ||
|
||
<app-nav-back-next [page]="'analytics-results-page'" | ||
[hide]="navSvc.isLastVisiblePage('analytics') ? 'next' : ''"></app-nav-back-next> | ||
|
||
</div> |
16 changes: 16 additions & 0 deletions
16
CSETWebNg/src/app/assessment/results/analytics-results/analytics-results.component.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
.mat-card-margin-10 { | ||
margin: 10px; | ||
float: left; | ||
width: 99%; | ||
} | ||
.mat-row:hover { | ||
background-color: #f2f2f2; | ||
cursor: pointer; | ||
} | ||
|
||
.table-container { | ||
position: relative; | ||
height: 350px; | ||
max-height: 350px; | ||
overflow: auto; | ||
} |
152 changes: 152 additions & 0 deletions
152
CSETWebNg/src/app/assessment/results/analytics-results/analytics-results.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
import { Component, ElementRef, OnInit, ViewChild } from '@angular/core'; | ||
import { AnalyticsService } from '../../../services/analytics.service'; | ||
import { NavigationService } from '../../../services/navigation/navigation.service'; | ||
import Chart, { ChartConfiguration, ChartType, registerables } from 'chart.js/auto'; | ||
import { AssessmentService } from '../../../services/assessment.service'; | ||
import { AggregationService } from '../../../services/aggregation.service'; | ||
import { AssessmentDetail } from '../../../models/assessment-info.model'; | ||
|
||
Chart.register(...registerables); | ||
|
||
@Component({ | ||
selector: 'app-analytics-results', | ||
templateUrl: './analytics-results.component.html', | ||
styleUrls: ['./analytics-results.component.scss'] | ||
}) | ||
export class AnalyticsResultsComponent implements OnInit { | ||
|
||
sectorId: any; | ||
assessmentId: any; | ||
modelId: any; | ||
minData: number[] = []; | ||
medianData: number[] = []; | ||
maxData: number[] = []; | ||
currentUserData: number[] = []; | ||
labels: string[] = []; | ||
|
||
@ViewChild('barCanvas') private barCanvas!: ElementRef<HTMLCanvasElement>; | ||
private barChart!: Chart; | ||
|
||
// Toggle state | ||
dataType: "mySector" | "allSectors" = "mySector"; | ||
|
||
constructor( | ||
public navSvc: NavigationService, | ||
public analyticsSvc: AnalyticsService, | ||
public assessSvc: AssessmentService, | ||
public aggregSvc: AggregationService | ||
) { } | ||
|
||
ngOnInit(): void { | ||
this.assessSvc.getAssessmentDetail().subscribe((resp: AssessmentDetail) => { | ||
this.assessmentId = resp.id; | ||
this.sectorId = resp.sectorId; | ||
this.modelId = resp.maturityModel.modelId; | ||
// Fetch initial data after getting assessment details | ||
this.getAnalyticsResults(); | ||
}); | ||
} | ||
|
||
ngAfterViewInit(): void { | ||
// Initialize the chart after the view is initialized | ||
this.initializeChart(); | ||
} | ||
|
||
// Get analytics results for specified sector | ||
private async getAnalyticsResults(allSectors?: boolean): Promise<void> { | ||
try { | ||
let result = null; | ||
if (allSectors){ | ||
result = await this.analyticsSvc.getAnalyticResults(this.assessmentId, this.modelId).toPromise(); | ||
} else { | ||
result = await this.analyticsSvc.getAnalyticResults(this.assessmentId, this.modelId, this.sectorId).toPromise(); | ||
} | ||
this.setData(result); | ||
} catch (error) { | ||
console.error('Error fetching analytics results', error); | ||
} | ||
} | ||
|
||
private setData(result: any): void { | ||
this.minData = result.min || []; | ||
this.medianData = result.median || []; | ||
this.maxData = result.max || []; | ||
this.currentUserData = result.barData?.values || []; | ||
this.labels = result.barData?.labels || []; | ||
if (this.barChart) { | ||
this.updateChart(); | ||
} | ||
} | ||
|
||
private initializeChart(): void { | ||
const data: ChartConfiguration<'bar'>['data'] = { | ||
labels: this.labels, | ||
datasets: [ | ||
{ | ||
label: 'Min', | ||
data: this.minData, | ||
backgroundColor: 'rgba(178, 29, 45, 1)', | ||
borderWidth: 1 | ||
}, | ||
{ | ||
label: 'Median', | ||
data: this.medianData, | ||
backgroundColor: 'rgba(216, 173, 30, 1)', | ||
borderWidth: 1 | ||
}, | ||
{ | ||
label: 'Max', | ||
data: this.maxData, | ||
backgroundColor: 'rgba(16, 145, 71, 1)', | ||
borderWidth: 1 | ||
}, | ||
{ | ||
label: 'My Assessment', | ||
data: this.currentUserData, | ||
backgroundColor: 'rgba(29, 136, 230, 1)', | ||
borderWidth: 1 | ||
} | ||
] | ||
}; | ||
|
||
const options: ChartConfiguration<'bar'>['options'] = { | ||
indexAxis: 'y', | ||
responsive: true, | ||
scales: { | ||
x: { | ||
beginAtZero: true, | ||
min: 0, | ||
max: 100 | ||
}, | ||
y: { | ||
beginAtZero: true, | ||
} | ||
} | ||
}; | ||
|
||
this.barChart = new Chart(this.barCanvas.nativeElement, { | ||
type: 'bar' as ChartType, | ||
data: data, | ||
options: options | ||
}); | ||
} | ||
|
||
private updateChart(): void { | ||
this.barChart.data.labels = this.labels; | ||
this.barChart.data.datasets[0].data = this.minData; | ||
this.barChart.data.datasets[1].data = this.medianData; | ||
this.barChart.data.datasets[2].data = this.maxData; | ||
this.barChart.data.datasets[3].data = this.currentUserData; | ||
this.barChart.update(); | ||
} | ||
|
||
toggleData(event: any): void { | ||
this.dataType = event.value; | ||
if (this.dataType === "allSectors") { | ||
this.getAnalyticsResults(true); | ||
} else { | ||
this.getAnalyticsResults(); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters