X-Git-Url: https://www.flypig.org.uk/git/?p=harbour-pedalo.git;a=blobdiff_plain;f=src%2Fstatsyearduration.cpp;fp=src%2Fstatsyearduration.cpp;h=19ad38089617ea3852ca6c2337127de37636277e;hp=0000000000000000000000000000000000000000;hb=336d2255eb501f5c228a80a0fa469844bbc1ee2f;hpb=54ab3ebfadf8cb258561a4641249da8c6469dc0f diff --git a/src/statsyearduration.cpp b/src/statsyearduration.cpp new file mode 100644 index 0000000..19ad380 --- /dev/null +++ b/src/statsyearduration.cpp @@ -0,0 +1,61 @@ +#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); +}