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