1
I need to take the distance covered by a vehicle in the last 3 days and display on the chart. For this, I make a for
and I calculate the distance traveled and saving in a array (arrayKmDayData)
:
arrayKmDayData.push(Math.abs((response[response.length - 1].distanceTraveled - response[0].distanceTraveled)/1000));
After you fill this array at a distance of 3 days, I display them on the chart:
$scope.labels = arrayKmDayLabel,
$scope.data = [
arrayKmDayData
];
The problem is that before we even finish the for
, my script is already calling the part to display the chart, ie it runs for the first time and already displays the chart, then runs the for
for the second time and re-displays the chart and last, runs the for
for the 3rd time and displays the chart again.
How to view the chart only after finishing?
Follows 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
];
I appreciate any help.
What part of your code displays the chart?
– Samir Braga
@Samirbraga in HTML has:
<canvas id="line" class="chart chart-line" chart-data="data" chart-labels="labels" 
 chart-legend="true" chart-series="series" chart-click="onClick"></canvas>
and in javascript$scope.labels = arrayKmDayLabel,
 $scope.data = [
 arrayKmDayData
 ];
– S_A
@Samirbraga am using Chart.js
– S_A
Could you put the
$scope.labels = arrayKmDayLabel, $scope.data = [ arrayKmDayData ];
to be executed in aif (i == 0)
within thefor
, since this is the end ofloop
. What do you think?– Samir Braga