-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathTrackStats.js
70 lines (63 loc) · 2.84 KB
/
TrackStats.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
BR.TrackStats = L.Class.extend({
update(polyline, segments) {
if (segments.length == 0) {
$('#stats-container').hide();
$('#stats-info').show();
return;
}
$('#stats-container').show();
$('#stats-info').hide();
document.getElementById('beeline-warning').hidden = !BR.Routing.hasBeeline(segments);
var stats = this.calcStats(polyline, segments),
length1 = L.Util.formatNum(stats.trackLength / 1000, 1).toLocaleString(),
length3 = L.Util.formatNum(stats.trackLength / 1000, 3).toLocaleString(undefined, {
minimumFractionDigits: 3,
}),
formattedAscend = stats.filteredAscend.toLocaleString(),
formattedPlainAscend = stats.plainAscend.toLocaleString(),
formattedCost = stats.cost.toLocaleString(),
meanCostFactor = stats.trackLength
? L.Util.formatNum(stats.cost / stats.trackLength, 2).toLocaleString()
: '0',
formattedTime =
Math.trunc(stats.totalTime / 3600) + ':' + ('0' + Math.trunc((stats.totalTime % 3600) / 60)).slice(-2),
formattedTimeHMS = formattedTime + ':' + ('0' + Math.trunc(stats.totalTime % 60)).slice(-2),
formattedEnergy = L.Util.formatNum(stats.totalEnergy / 3600000, 2).toLocaleString(),
meanEnergy = stats.trackLength
? L.Util.formatNum(stats.totalEnergy / 36 / stats.trackLength, 2).toLocaleString()
: '0';
$('#distance').html(length1);
// alternative 3-digit format down to meters as tooltip
$('#distance').attr('title', length3 + ' km');
$('#ascend').html(formattedAscend);
$('#plainascend').html(formattedPlainAscend);
$('#cost').html(formattedCost);
$('#meancostfactor').html(meanCostFactor);
$('#totaltime').html(formattedTime);
// alternative time format with seconds display as tooltip
$('#totaltime').attr('title', formattedTimeHMS + ' h');
$('#totalenergy').html(formattedEnergy);
$('#meanenergy').html(meanEnergy);
},
calcStats(polyline, segments) {
var stats = {
trackLength: 0,
filteredAscend: 0,
plainAscend: 0,
totalTime: 0,
totalEnergy: 0,
cost: 0,
};
var i, props;
for (i = 0; segments && i < segments.length; i++) {
props = segments[i].feature.properties;
stats.trackLength += +props['track-length'];
stats.filteredAscend += +props['filtered ascend'];
stats.plainAscend += +props['plain-ascend'];
stats.totalTime += +props['total-time'];
stats.totalEnergy += +props['total-energy'];
stats.cost += +props['cost'];
}
return stats;
},
});