From 5199f746c3df47388acabae1e8a196d0648c5721 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20ANDRE?= Date: Tue, 22 Aug 2023 15:05:14 +0200 Subject: [PATCH] Add stacked bars for x types of flight --- .../list-of-tunnel-flights.component.ts | 57 ++++++------------- 1 file changed, 17 insertions(+), 40 deletions(-) diff --git a/Front/skydivelogs-app/src/app/list-of-tunnel-flights/list-of-tunnel-flights.component.ts b/Front/skydivelogs-app/src/app/list-of-tunnel-flights/list-of-tunnel-flights.component.ts index c8f65be..bc4c970 100644 --- a/Front/skydivelogs-app/src/app/list-of-tunnel-flights/list-of-tunnel-flights.component.ts +++ b/Front/skydivelogs-app/src/app/list-of-tunnel-flights/list-of-tunnel-flights.component.ts @@ -142,13 +142,10 @@ export class ListOfTunnelFlightsComponent implements OnInit { this.serviceTunnelFlight.getTunnelFlightsByMonth(beginDate, endDate) .subscribe((data) => { const allMonths = this.getMontsBetweenDates(beginDate, endDate) - const cumulatedTime = this.getCumulatedTimeByMonth(data, allMonths); + const cumulatedTime = this.getCumulatedTimeByTypeByMonth(data, allMonths); this.barChartData = { labels: allMonths, - datasets: [ - { data: cumulatedTime, label: 'Time in tunnel' }, - { data: cumulatedTime, label: 'Time in tunnel 2' } - ] + datasets: cumulatedTime }; this.isLoading = false; @@ -168,50 +165,30 @@ export class ListOfTunnelFlightsComponent implements OnInit { return results; } - private getCumulatedTimeByMonth(stats: TunnelFlightByMonth[], allMonths: string[]): Array { - // type GraphModel = { - // data: number[]; - // label: string; - // } + private getCumulatedTimeByTypeByMonth(stats: TunnelFlightByMonth[], allMonths: string[]): Array { + let tmpResults = new Map(); - // let results: Array = []; - - // for (let i = 0; i < allMonths.length; i++) { - // const month = allMonths[i]; - // let tmp = stats.filter((d) => d.month == month); - - // let sum: number = 0; - // if (tmp.length > 0){ - // tmp.forEach(a => sum += a.nb); - // } - - // results.push(sum); - // } - - // return results; - - let arr3 = Array.from({length: allMonths.length + 1}, (v, k) => 0); - let disctintType = stats.map(item => item.type).filter((value, index, self) => self.indexOf(value) === index); - let disctintType2 = Array.from(new Set(stats.map((item) => item.type))) - - let results = new Map(); + const disctintType = Array.from(new Set(stats.map((item) => item.type))); + disctintType.forEach(type => { + tmpResults.set(type, Array.from({length: allMonths.length}, (v, k) => 0)); + }); for (let i = 0; i < allMonths.length; i++) { const month = allMonths[i]; - let tmp = stats.filter((d) => d.month == month); + let filteredStats = stats.filter((d) => d.month == month); - if (tmp.length > 0){ - tmp.forEach(a => { - if (!results.has(a.type)) { - results.set(a.type, [a.nb]) - } else { - results[a.type].push(a.nb); - } + if (filteredStats.length > 0){ + filteredStats.forEach(fs => { + tmpResults.get(fs.type)[i] += fs.nb; }); } } - return new Array(); + const results = Array.from(tmpResults, function (item) { + return { label: item[0], data: item[1] } + }); + + return results; } private footer = (tooltipItems) => {