Add graphs showing journeys/durations over last year
[harbour-pedalo.git] / src / statsyearjourneys.cpp
diff --git a/src/statsyearjourneys.cpp b/src/statsyearjourneys.cpp
new file mode 100644 (file)
index 0000000..f477c3b
--- /dev/null
@@ -0,0 +1,61 @@
+#include "statsyearjourneys.h"
+
+#define WINDOW (3)
+
+StatsYearJourneys::StatsYearJourneys(JourneyModel * journeys) :
+    journeys(journeys)
+{
+    title = QString("Year view (journeys/mon + moving ave)");
+    units = "";
+}
+
+void StatsYearJourneys::update() {
+    unsigned int count[12];
+    unsigned int average[11 + WINDOW];
+    int pos;
+    QDate now;
+    QDate start;
+    QDate startave;
+
+    now = QDate::currentDate();
+    start = now.addMonths(-12);
+    startave = start.addMonths(1 - WINDOW);
+
+    barvalues.clear();
+    linevalues.clear();
+
+    for (pos = 0; pos < 12; pos++) {
+        count[pos] = 0u;
+        average[pos] = 0u;
+    }
+
+    foreach (Journey const &journey, journeys->getData()) {
+        QDate date = journey.getStartDate();
+        if (date > start) {
+            count[(date.month() - now.month() + 11) % 12]++;
+        }
+        if (date > startave) {
+            int month = (date.month() - now.month() + 11) % 12;
+            for (pos = 0; pos < WINDOW; pos++) {
+                average[month + pos]++;
+            }
+        }
+    }
+
+    labels = QStringList{"J", "F", "M", "A", "M", "J", "J", "A", "S", "O", "N", "D"};
+    for (pos = 0; pos < now.month(); pos++) {
+        // Rotate values
+        labels.push_back(labels.takeFirst());
+    }
+
+    maxval = 0.0;
+    for (pos = 0; pos < 12; pos++) {
+        if (count[pos] > maxval) {
+            maxval = count[pos];
+        }
+        barvalues << (float)count[pos];
+        linevalues << (float)average[pos] / (float)WINDOW;
+    }
+
+    step = maxval > 5.0 ? qRound(maxval / 5.0) : (maxval / 5.0);
+}