Bump version to 0.2.2
[harbour-pedalo.git] / src / statsyearduration.cpp
1 #include "statsyearduration.h"
2
3 #define WINDOW (3)
4
5 StatsYearDuration::StatsYearDuration(JourneyModel * journeys) :
6 journeys(journeys)
7 {
8 title = QString("Year view (hours/mon + moving ave)");
9 units = "";
10 }
11
12 void StatsYearDuration::update() {
13 double duration[12];
14 double 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 duration[pos] = 0.0;
29 average[pos] = 0.0;
30 }
31
32 foreach (Journey const &journey, journeys->getData()) {
33 QDate date = journey.getStartDate();
34 if (date > start) {
35 duration[(date.month() - now.month() + 11) % 12] += journey.getDuration() / (60.0 * 60.0);
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] += journey.getDuration() / (60.0 * 60.0);
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 (duration[pos] > maxval) {
54 maxval = duration[pos];
55 }
56 barvalues << duration[pos];
57 linevalues << average[pos] / (double)WINDOW;
58 }
59
60 step = maxval > 5.0 ? qRound(maxval / 5.0) : (maxval / 5.0);
61 }