+#include "statsyearduration.h"
+
+#define WINDOW (3)
+
+StatsYearDuration::StatsYearDuration(JourneyModel * journeys) :
+ journeys(journeys)
+{
+ title = QString("Year view (hours/mon + moving ave)");
+ units = "";
+}
+
+void StatsYearDuration::update() {
+ double duration[12];
+ double 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++) {
+ duration[pos] = 0.0;
+ average[pos] = 0.0;
+ }
+
+ foreach (Journey const &journey, journeys->getData()) {
+ QDate date = journey.getStartDate();
+ if (date > start) {
+ duration[(date.month() - now.month() + 11) % 12] += journey.getDuration() / (60.0 * 60.0);
+ }
+ if (date > startave) {
+ int month = (date.month() - now.month() + 11) % 12;
+ for (pos = 0; pos < WINDOW; pos++) {
+ average[month + pos] += journey.getDuration() / (60.0 * 60.0);
+ }
+ }
+ }
+
+ 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 (duration[pos] > maxval) {
+ maxval = duration[pos];
+ }
+ barvalues << duration[pos];
+ linevalues << average[pos] / (double)WINDOW;
+ }
+
+ step = maxval > 5.0 ? qRound(maxval / 5.0) : (maxval / 5.0);
+}