Consider this observable method which calls an api get bar chart data
Class:
export interface AlarmChart {
chartLabel: string[];
data: number[][];
}
Api Call
getBarChartDataV2(period: string): Observable<AlarmChart> {
for(let x = 0 ;x<severities.length;x++){
//each severity all values of a week
var countoFserverity=[];
for(let i = 0 ;i<daysRangeForWeek.length;i++){
let url = `${this.config.apiCumulocityUrl}/alarm/alarms?dateFrom=${daysRangeForWeek[i].dayStart}&dateTo=${daysRangeForWeek[i].dayEnd}&severity=${severities[x]}'`;
url = url.replace(/'/g,'');
this.callApi(url).subscribe(res=>{
let response:any = res;
countoFserverity.push(response.alarms.length);
//add to final when all results came
});
}
//PREVENT GOING TO THIS LINE BEFORE RESPONSE FROM API INSIDE SUBSCRIBE
data.push(countoFserverity);
}
debugger;
result = {
chartLabel: labels,
data: data,
};
return result;
}
Api Caller
callApi(url:string):Observable<any>{
return this.httpClient.get(url).pipe(
map(res => {
debugger;
let response:any = res;
return response;
})
);
}