Add stacked bars for x types of flight

This commit is contained in:
Sébastien ANDRE
2023-08-22 15:05:14 +02:00
parent 76c8497c13
commit 5199f746c3

View File

@@ -142,13 +142,10 @@ export class ListOfTunnelFlightsComponent implements OnInit {
this.serviceTunnelFlight.getTunnelFlightsByMonth(beginDate, endDate) this.serviceTunnelFlight.getTunnelFlightsByMonth(beginDate, endDate)
.subscribe((data) => { .subscribe((data) => {
const allMonths = this.getMontsBetweenDates(beginDate, endDate) const allMonths = this.getMontsBetweenDates(beginDate, endDate)
const cumulatedTime = this.getCumulatedTimeByMonth(data, allMonths); const cumulatedTime = this.getCumulatedTimeByTypeByMonth(data, allMonths);
this.barChartData = { this.barChartData = {
labels: allMonths, labels: allMonths,
datasets: [ datasets: cumulatedTime
{ data: cumulatedTime, label: 'Time in tunnel' },
{ data: cumulatedTime, label: 'Time in tunnel 2' }
]
}; };
this.isLoading = false; this.isLoading = false;
@@ -168,50 +165,30 @@ export class ListOfTunnelFlightsComponent implements OnInit {
return results; return results;
} }
private getCumulatedTimeByMonth(stats: TunnelFlightByMonth[], allMonths: string[]): Array<number> { private getCumulatedTimeByTypeByMonth(stats: TunnelFlightByMonth[], allMonths: string[]): Array<any> {
// type GraphModel = { let tmpResults = new Map<string, number[]>();
// data: number[];
// label: string;
// }
// let results: Array<number> = []; const disctintType = Array.from(new Set(stats.map((item) => item.type)));
disctintType.forEach(type => {
// for (let i = 0; i < allMonths.length; i++) { tmpResults.set(type, Array.from({length: allMonths.length}, (v, k) => 0));
// 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<string, number[]>();
for (let i = 0; i < allMonths.length; i++) { for (let i = 0; i < allMonths.length; i++) {
const month = allMonths[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){ if (filteredStats.length > 0){
tmp.forEach(a => { filteredStats.forEach(fs => {
if (!results.has(a.type)) { tmpResults.get(fs.type)[i] += fs.nb;
results.set(a.type, [a.nb])
} else {
results[a.type].push(a.nb);
}
}); });
} }
} }
return new Array<number>(); const results = Array.from(tmpResults, function (item) {
return { label: item[0], data: item[1] }
});
return results;
} }
private footer = (tooltipItems) => { private footer = (tooltipItems) => {