Added new graphs
[harbour-pedalo.git] / src / statshourcongestion.cpp
diff --git a/src/statshourcongestion.cpp b/src/statshourcongestion.cpp
new file mode 100644 (file)
index 0000000..a98230e
--- /dev/null
@@ -0,0 +1,67 @@
+#include <QDebug>
+
+#include "statshourcongestion.h"
+
+#define LOWESTHOUR (7)
+#define HIGHESTHOUR (21)
+
+StatsHourCongestion::StatsHourCongestion(JourneyModel * journeys) :
+    journeys(journeys)
+{
+    title = "Congestion by hour (cycles per hour)";
+    units = "";
+
+    labels.clear();
+    for (int hour = LOWESTHOUR; hour <= HIGHESTHOUR; hour++) {
+        labels << QString::number(hour);
+    }
+}
+
+void StatsHourCongestion::update() {
+    double passed[24];
+    unsigned int count[24];
+    int pos;
+
+    qDebug() << "Calculating values";
+    values.clear();
+
+    for (pos = 0; pos < 24; pos++) {
+        passed[pos] = 0.0;
+        count[pos] = 0;
+    }
+
+    foreach (Journey const &journey, journeys->getData()) {
+        QTime time = journey.getStartTime();
+        int hour = time.hour();
+        int startmin = time.minute();
+        int duration = (journey.getDuration() / 60);
+        int remaining = duration;
+
+        while (remaining > 0) {
+            // toadd is always greater than 0, so the loop is guaranteed to exit
+            unsigned int toadd = (startmin + remaining) < 60 ? remaining : 60 - startmin;
+            passed[hour] += (double)toadd * ((double)(journey.getOvertook() + journey.getOvertakenBy()) / duration);
+            count[hour] += toadd;
+            remaining -= toadd;
+            startmin = 0;
+
+            hour = (hour + 1) % 24;
+        }
+    }
+
+    maxval = 0.0;
+    for (pos = LOWESTHOUR; pos <= HIGHESTHOUR; pos++) {
+        float result = 0.0f;
+        if (count[pos] > 0) {
+            result = (float)(60.0 * passed[pos] / (double)count[pos]);
+        }
+        if (result > maxval) {
+            maxval = result;
+        }
+        values << result;
+    }
+
+    step = (maxval > 5.0) ? qRound(maxval / 5.0) : (maxval / 5.0);
+
+    qDebug() << "Calculated values";
+}