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