c4842b3742ceab4be4f0872c2f47d9f47185215c
[harbour-pedalo.git] / src / statsweekdaycongestion.cpp
1 #include <QDebug>
2
3 #include "statsweekdaycongestion.h"
4
5 StatsWeekdayCongestion::StatsWeekdayCongestion(JourneyModel * journeys) :
6 journeys(journeys)
7 {
8 title = "Congestion by day (cycles per hour)";
9 units = "";
10 labels = QStringList{"M", "T", "W", "Th", "F", "S", "Su"};
11 }
12
13 void StatsWeekdayCongestion::update() {
14 double passed[7];
15 unsigned int count[7];
16 int pos;
17
18 qDebug() << "Calculating values";
19 values.clear();
20
21 for (pos = 0; pos < 7; pos++) {
22 passed[pos] = 0u;
23 count[pos] = 0u;
24 }
25
26 foreach (Journey const &journey, journeys->getData()) {
27 QDate date = journey.getStartDate();
28 int dayofweek = date.dayOfWeek() - 1;
29 double duration = journey.getDuration() / (60.0 * 60.0);
30 if (dayofweek >= 0) {
31 passed[dayofweek] += (journey.getOvertook() + journey.getOvertakenBy()) / duration;
32 count[dayofweek]++;
33 }
34 }
35
36 maxval = 0.0;
37 for (pos = 0; pos < 7; pos++) {
38 float result = 0.0f;
39 if (count[pos] > 0) {
40 result = ((double)passed[pos] / (double)count[pos]);
41 }
42 if (result > maxval) {
43 maxval = result;
44 }
45 values << result;
46 }
47
48 step = maxval > 5.0 ? qRound(maxval / 5.0) : (maxval / 5.0);
49
50 qDebug() << "Calculated values";
51 }