Bump version to 0.2.2
[harbour-pedalo.git] / src / statshourcongestion.cpp
1 #include "statshourcongestion.h"
2
3 #define LOWESTHOUR (7)
4 #define HIGHESTHOUR (21)
5
6 StatsHourCongestion::StatsHourCongestion(JourneyModel * journeys) :
7 journeys(journeys)
8 {
9 title = "Congestion by hour (cycles per hour)";
10 units = "";
11
12 labels.clear();
13 for (int hour = LOWESTHOUR; hour <= HIGHESTHOUR; hour++) {
14 labels << QString::number(hour);
15 }
16 }
17
18 void StatsHourCongestion::update() {
19 double passed[24];
20 unsigned int count[24];
21 int pos;
22
23 barvalues.clear();
24
25 for (pos = 0; pos < 24; pos++) {
26 passed[pos] = 0.0;
27 count[pos] = 0;
28 }
29
30 foreach (Journey const &journey, journeys->getData()) {
31 QTime time = journey.getStartTime();
32 int hour = time.hour();
33 int startmin = time.minute();
34 int duration = (journey.getDuration() / 60);
35 int remaining = duration;
36
37 while (remaining > 0) {
38 // toadd is always greater than 0, so the loop is guaranteed to exit
39 unsigned int toadd = (startmin + remaining) < 60 ? remaining : 60 - startmin;
40 passed[hour] += (double)toadd * ((double)(journey.getOvertook() + journey.getOvertakenBy()) / duration);
41 count[hour] += toadd;
42 remaining -= toadd;
43 startmin = 0;
44
45 hour = (hour + 1) % 24;
46 }
47 }
48
49 maxval = 0.0;
50 for (pos = LOWESTHOUR; pos <= HIGHESTHOUR; pos++) {
51 float result = 0.0f;
52 if (count[pos] > 0) {
53 result = (float)(60.0 * passed[pos] / (double)count[pos]);
54 }
55 if (result > maxval) {
56 maxval = result;
57 }
58 barvalues << result;
59 }
60
61 step = (maxval > 5.0) ? qRound(maxval / 5.0) : (maxval / 5.0);
62 }