import { Component, DestroyRef, inject, OnInit, OnDestroy } from '@angular/core'; import { takeUntilDestroyed } from "@angular/core/rxjs-interop"; import { MatButtonModule } from '@angular/material/button'; import { MatCardModule } from '@angular/material/card'; import { MatDividerModule } from '@angular/material/divider'; import { MatExpansionModule } from '@angular/material/expansion'; import { MatIconModule } from '@angular/material/icon'; import { MatMenuModule } from '@angular/material/menu'; import { Observable, Subscription } from 'rxjs'; import { MenuItems } from 'src/app/components/shared'; import { BarHorizontalChartComponent } from 'src/app/components/shared/helpers-chart'; import { HistoryTableComponent } from 'src/app/components/shared/helpers-jump'; import { UtilitiesService, AeronefsService } from 'src/app/core/services'; import { AeronefByImat, AeronefByYear } from 'src/app/core/models'; @Component({ selector: 'app-aeronefs-bar', standalone: true, imports: [ MatButtonModule, MatCardModule, MatDividerModule, MatExpansionModule, MatIconModule, MatMenuModule, BarHorizontalChartComponent, HistoryTableComponent ], templateUrl: './aeronefs-bar.component.html' }) export class AeronefsBarComponent implements OnInit, OnDestroy { private _aeronefByImat: Subscription = new Subscription(); private _aeronefByYear: Subscription = new Subscription(); private _aeronefsByImat!: Array; private _aeronefsByYear!: Array; public title: string = 'Aéronefs'; public subtitle: string = 'Nombre total de sauts par aéronef'; public seriesHeader: string[] = []; public seriesName: string[] = []; public seriesValue: number[] = []; public seriesRow: Array> = []; public seriesColor: { backgroundColor: string[], borderColor: string[] } = { backgroundColor: this._utilitiesService.getSeriesColors(0.8, 'all'), borderColor: this._utilitiesService.getSeriesColors(1, 'all') }; public seriesColorClass: string[] = this._utilitiesService.getChartColors(); public displayCharts = false; public destroyRef = inject(DestroyRef); constructor( private _aeronefsService: AeronefsService, private _utilitiesService: UtilitiesService, public menuItems: MenuItems ) { } ngOnInit() { this._loadAeronefByImat(); } ngOnDestroy() { this._aeronefByImat.unsubscribe(); this._aeronefByYear.unsubscribe(); } private _loadAeronefByImat(): void { const aeronefs$: Observable> = this._aeronefsService.getAllByImat().pipe(takeUntilDestroyed(this.destroyRef)); this._aeronefByImat = aeronefs$.subscribe((aggregate: Array) => { this._aeronefsByImat = aggregate.map((row: AeronefByImat) => { //this.chartConfig.barChartData.labels!.push(`${row.aeronef} ${row.imat}`); this.seriesName.push(`${row.aeronef} ${row.imat}`); this.seriesValue.push(row.count); return row; }); this._loadAeronefByYear(); }); } private _loadAeronefByYear(): void { this.seriesHeader = ['2018', '2019', '2020', '2021', '2022', '2023', '2024']; const values: Array = this.seriesHeader.map(() => 0); this.seriesName.forEach(() => { this.seriesRow.push([...values]); }); const dropzones$: Observable> = this._aeronefsService.getAllByImatByYear().pipe(takeUntilDestroyed(this.destroyRef)); this._aeronefByYear = dropzones$.subscribe((aggregate: Array) => { this._aeronefsByYear = aggregate.map((row: AeronefByYear) => { const aeronef: string = row.aeronef; const imat: string = row.imat; const year: string = row.year.toString(); this.seriesRow[this.seriesName.indexOf(`${aeronef} ${imat}`)][this.seriesHeader.indexOf(year)] = row.count; return row; }); this.displayCharts = true; }); } }