Bump version to 0.2.2
[harbour-pedalo.git] / src / statsyearjourneys.cpp
1 #include "statsyearjourneys.h"
2
3 #define WINDOW (3)
4
5 StatsYearJourneys::StatsYearJourneys(JourneyModel * journeys) :
6 journeys(journeys)
7 {
8 title = QString("Year view (journeys/mon + moving ave)");
9 units = "";
10 }
11
12 void StatsYearJourneys::update() {
13 unsigned int count[12];
14 unsigned int average[11 + WINDOW];
15 int pos;
16 QDate now;
17 QDate start;
18 QDate startave;
19
20 now = QDate::currentDate();
21 start = now.addMonths(-12);
22 startave = start.addMonths(1 - WINDOW);
23
24 barvalues.clear();
25 linevalues.clear();
26
27 for (pos = 0; pos < 12; pos++) {
28 count[pos] = 0u;
29 average[pos] = 0u;
30 }
31
32 foreach (Journey const &journey, journeys->getData()) {
33 QDate date = journey.getStartDate();
34 if (date > start) {
35 count[(date.month() - now.month() + 11) % 12]++;
36 }
37 if (date > startave) {
38 int month = (date.month() - now.month() + 11) % 12;
39 for (pos = 0; pos < WINDOW; pos++) {
40 average[month + pos]++;
41 }
42 }
43 }
44
45 labels = QStringList{"J", "F", "M", "A", "M", "J", "J", "A", "S", "O", "N", "D"};
46 for (pos = 0; pos < now.month(); pos++) {
47 // Rotate values
48 labels.push_back(labels.takeFirst());
49 }
50
51 maxval = 0.0;
52 for (pos = 0; pos < 12; pos++) {
53 if (count[pos] > maxval) {
54 maxval = count[pos];
55 }
56 barvalues << (float)count[pos];
57 linevalues << (float)average[pos] / (float)WINDOW;
58 }
59
60 step = maxval > 5.0 ? qRound(maxval / 5.0) : (maxval / 5.0);
61 }