I need to get the distance traveled by a vehicle in the last 3 days and display it on the chart. For this, I make a for
and I'm calculating the distance covered and saving in an array (arrayKmDayData)
:
arrayKmDayData.push(Math.abs((response[response.length - 1].distanceTraveled - response[0].distanceTraveled)/1000));
After filling this array with the distance of 3 days, display them on the chart:
$scope.labels = arrayKmDayLabel,
$scope.data = [
arrayKmDayData
];
The problem is that before I even finish the for
, my script is already calling the display part of the graph, ie it runs the for the first time and already displays the graph, then executes the for
for the second time and redisplays the graph and finally executes for
for the 3rd time and displays the graph again.
How do I display the chart only after the forex has ended?
Follow the code:
for(var i = -2; i <= 0; i ++){
day = getKmDate(i);
date = new Date(day[0]);
arrayKmDayLabel.push(date.toLocaleDateString("pt-BR"));
var query = new $kinvey.Query();
query.equalTo('idColetor', arraySelectedItem[0]);
query.greaterThanOrEqualTo('tsmilliseconds', day[0]);
query.lessThan('tsmilliseconds', day[1]);
query.descending('tsmilliseconds');
var promise = $kinvey.DataStore.find('myCollection', query);
promise.then(function(response) {
arrayKmDayData.push(Math.abs((response[response.length - 1].distanceTraveled - response[0].distanceTraveled)/1000));
});
}
$scope.labels = arrayKmDayLabel,
$scope.data = [
arrayKmDayData
];
Thanks for any help.