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