Add graphs generated from journey data
[harbour-pedalo.git] / src / statsweekdaycongestion.cpp
diff --git a/src/statsweekdaycongestion.cpp b/src/statsweekdaycongestion.cpp
new file mode 100644 (file)
index 0000000..685e175
--- /dev/null
@@ -0,0 +1,50 @@
+#include <QDebug>
+
+#include "statsweekdaycongestion.h"
+
+StatsWeekdayCongestion::StatsWeekdayCongestion(JourneyModel * journeys) :
+    journeys(journeys)
+{
+    title = "Congested days (cycles passed)";
+    units = "";
+    labels = QStringList{"M", "T", "W", "Th", "F", "S", "Su"};
+}
+
+void StatsWeekdayCongestion::update() {
+    quint32 passed[7];
+    unsigned int count[7];
+    int pos;
+
+    qDebug() << "Calculating values";
+    values.clear();
+
+    for (pos = 0; pos < 7; pos++) {
+        passed[pos] = 0u;
+        count[pos] = 0u;
+    }
+
+    foreach (Journey const &journey, journeys->getData()) {
+        QDate date = journey.getStartDate();
+        int dayofweek = date.dayOfWeek() - 1;
+        if (dayofweek >= 0) {
+            passed[dayofweek] += journey.getOvertook() + journey.getOvertakenBy();
+            count[dayofweek]++;
+        }
+    }
+
+    maxval = 0.0;
+    for (pos = 0; pos < 7; pos++) {
+        float result = 0.0f;
+        if (count[pos] > 0) {
+            result = ((double)passed[pos] / (double)count[pos]);
+        }
+        if (result > maxval) {
+            maxval = result;
+        }
+        values << result;
+    }
+
+    step = qRound(maxval / 5.0);
+
+    qDebug() << "Calculated values";
+}