Added new graphs
authorDavid Llewellyn-Jones <david@flypig.co.uk>
Sun, 22 Jul 2018 23:14:35 +0000 (00:14 +0100)
committerDavid Llewellyn-Jones <david@flypig.co.uk>
Sun, 22 Jul 2018 23:14:35 +0000 (00:14 +0100)
Journey proportions per hour.
Congestion by hour.

harbour-pedalo.pro
src/harbour-pedalo.cpp
src/statshourcongestion.cpp [new file with mode: 0644]
src/statshourcongestion.h [new file with mode: 0644]
src/statshourjourneys.cpp [new file with mode: 0644]
src/statshourjourneys.h [new file with mode: 0644]
src/statsweekdayave.cpp
src/statsweekdayave.h
src/statsweekdaycongestion.cpp
src/statsweekdaycongestion.h

index 7c01192..bc118dd 100644 (file)
@@ -37,7 +37,9 @@ SOURCES += src/harbour-pedalo.cpp \
     src/statsmodel.cpp \
     src/stats.cpp \
     src/statsweekdayave.cpp \
-    src/statsweekdaycongestion.cpp
+    src/statsweekdaycongestion.cpp \
+    src/statshourcongestion.cpp \
+    src/statshourjourneys.cpp
 
 DISTFILES += qml/harbour-pedalo.qml \
     qml/cover/CoverPage.qml \
@@ -83,4 +85,6 @@ HEADERS += \
     src/statsmodel.h \
     src/stats.h \
     src/statsweekdayave.h \
-    src/statsweekdaycongestion.h
+    src/statsweekdaycongestion.h \
+    src/statshourcongestion.h \
+    src/statshourjourneys.h
index 35fc77f..f19a8fc 100644 (file)
@@ -13,7 +13,9 @@
 #include "imageprovider.h"
 #include "graph.h"
 #include "statsweekdayave.h"
+#include "statshourjourneys.h"
 #include "statsweekdaycongestion.h"
+#include "statshourcongestion.h"
 
 #include "harbour-pedalo.h"
 
@@ -51,9 +53,15 @@ int main(int argc, char *argv[])
     StatsWeekdayAve statsweekdayave(&journeys);
     statsmodel.addStats(statsweekdayave);
 
+    StatsHourJourneys statshourjourneys(&journeys);
+    statsmodel.addStats(statshourjourneys);
+
     StatsWeekdayCongestion statsweekdaycongestion(&journeys);
     statsmodel.addStats(statsweekdaycongestion);
 
+    StatsHourCongestion statshourcongestion(&journeys);
+    statsmodel.addStats(statshourcongestion);
+
     QFile file;
     file.setFileName(Settings::getConfigDir() + "/journeys.csv");
     journeys.importFromFile(file);
diff --git a/src/statshourcongestion.cpp b/src/statshourcongestion.cpp
new file mode 100644 (file)
index 0000000..a98230e
--- /dev/null
@@ -0,0 +1,67 @@
+#include <QDebug>
+
+#include "statshourcongestion.h"
+
+#define LOWESTHOUR (7)
+#define HIGHESTHOUR (21)
+
+StatsHourCongestion::StatsHourCongestion(JourneyModel * journeys) :
+    journeys(journeys)
+{
+    title = "Congestion by hour (cycles per hour)";
+    units = "";
+
+    labels.clear();
+    for (int hour = LOWESTHOUR; hour <= HIGHESTHOUR; hour++) {
+        labels << QString::number(hour);
+    }
+}
+
+void StatsHourCongestion::update() {
+    double passed[24];
+    unsigned int count[24];
+    int pos;
+
+    qDebug() << "Calculating values";
+    values.clear();
+
+    for (pos = 0; pos < 24; pos++) {
+        passed[pos] = 0.0;
+        count[pos] = 0;
+    }
+
+    foreach (Journey const &journey, journeys->getData()) {
+        QTime time = journey.getStartTime();
+        int hour = time.hour();
+        int startmin = time.minute();
+        int duration = (journey.getDuration() / 60);
+        int remaining = duration;
+
+        while (remaining > 0) {
+            // toadd is always greater than 0, so the loop is guaranteed to exit
+            unsigned int toadd = (startmin + remaining) < 60 ? remaining : 60 - startmin;
+            passed[hour] += (double)toadd * ((double)(journey.getOvertook() + journey.getOvertakenBy()) / duration);
+            count[hour] += toadd;
+            remaining -= toadd;
+            startmin = 0;
+
+            hour = (hour + 1) % 24;
+        }
+    }
+
+    maxval = 0.0;
+    for (pos = LOWESTHOUR; pos <= HIGHESTHOUR; pos++) {
+        float result = 0.0f;
+        if (count[pos] > 0) {
+            result = (float)(60.0 * passed[pos] / (double)count[pos]);
+        }
+        if (result > maxval) {
+            maxval = result;
+        }
+        values << result;
+    }
+
+    step = (maxval > 5.0) ? qRound(maxval / 5.0) : (maxval / 5.0);
+
+    qDebug() << "Calculated values";
+}
diff --git a/src/statshourcongestion.h b/src/statshourcongestion.h
new file mode 100644 (file)
index 0000000..ce64257
--- /dev/null
@@ -0,0 +1,18 @@
+#ifndef STATSHOURCONGESTION_H
+#define STATSHOURCONGESTION_H
+
+#include "journeymodel.h"
+#include "stats.h"
+
+class StatsHourCongestion : public Stats
+{
+public:
+    StatsHourCongestion(JourneyModel * journeys);
+
+    void update();
+
+private:
+    JourneyModel * journeys;
+};
+
+#endif // STATSHOURCONGESTION_H
diff --git a/src/statshourjourneys.cpp b/src/statshourjourneys.cpp
new file mode 100644 (file)
index 0000000..f8616cb
--- /dev/null
@@ -0,0 +1,60 @@
+#include <QDebug>
+
+#include "statshourjourneys.h"
+
+#define LOWESTHOUR (7)
+#define HIGHESTHOUR (21)
+
+StatsHourJourneys::StatsHourJourneys(JourneyModel * journeys) :
+    journeys(journeys)
+{
+    title = "Journey proportions per hour (%)";
+    units = "%";
+
+    labels.clear();
+    for (int hour = LOWESTHOUR; hour <= HIGHESTHOUR; hour++) {
+        labels << QString::number(hour);
+    }
+}
+
+void StatsHourJourneys::update() {
+    unsigned int minsperhour[24];
+    quint64 totalmins;
+    int pos;
+
+    qDebug() << "Calculating values";
+    values.clear();
+
+    for (pos = 0; pos < 24; pos++) {
+        minsperhour[pos] = 0u;
+    }
+
+    totalmins = 0u;
+    foreach (Journey const &journey, journeys->getData()) {
+        QTime time = journey.getStartTime();
+        int hour = time.hour();
+        int startmin = time.minute();
+        int remaining = (journey.getDuration() / 60);
+        totalmins += remaining;
+
+        while (remaining > 0) {
+            // toadd is always greater than 0, so the loop is guaranteed to exit
+            unsigned int toadd = (startmin + remaining) < 60 ? remaining : 60 - startmin;
+            minsperhour[hour] += toadd;
+            remaining -= toadd;
+            startmin = 0;
+
+            hour = (hour + 1) % 24;
+        }
+    }
+
+    for (pos = LOWESTHOUR; pos <= HIGHESTHOUR; pos++) {
+        float result = 0.0f;
+        if (totalmins > 0) {
+            result = ((double)minsperhour[pos] / (double)totalmins);
+        }
+        values << result;
+    }
+
+    qDebug() << "Calculated values";
+}
diff --git a/src/statshourjourneys.h b/src/statshourjourneys.h
new file mode 100644 (file)
index 0000000..ad42832
--- /dev/null
@@ -0,0 +1,18 @@
+#ifndef STATSHOURJOURNEYS_H
+#define STATSHOURJOURNEYS_H
+
+#include "journeymodel.h"
+#include "stats.h"
+
+class StatsHourJourneys : public Stats
+{
+public:
+    StatsHourJourneys(JourneyModel * journeys);
+
+    void update();
+
+private:
+    JourneyModel * journeys;
+};
+
+#endif // STATSHOURJOURNEYS_H
index 63e595e..527cb1d 100644 (file)
@@ -44,7 +44,7 @@ void StatsWeekdayAve::update() {
         values << result;
     }
 
-    step = qRound(maxval / 5.0);
+    step = maxval > 5.0 ? qRound(maxval / 5.0) : (maxval / 5.0);
 
     qDebug() << "Calculated values";
 }
index 1a36d32..5e5a63a 100644 (file)
@@ -2,7 +2,6 @@
 #define STATSWEEKDAYAVE_H
 
 #include "journeymodel.h"
-
 #include "stats.h"
 
 class StatsWeekdayAve : public Stats
index 685e175..c4842b3 100644 (file)
@@ -5,13 +5,13 @@
 StatsWeekdayCongestion::StatsWeekdayCongestion(JourneyModel * journeys) :
     journeys(journeys)
 {
-    title = "Congested days (cycles passed)";
+    title = "Congestion by day (cycles per hour)";
     units = "";
     labels = QStringList{"M", "T", "W", "Th", "F", "S", "Su"};
 }
 
 void StatsWeekdayCongestion::update() {
-    quint32 passed[7];
+    double passed[7];
     unsigned int count[7];
     int pos;
 
@@ -26,8 +26,9 @@ void StatsWeekdayCongestion::update() {
     foreach (Journey const &journey, journeys->getData()) {
         QDate date = journey.getStartDate();
         int dayofweek = date.dayOfWeek() - 1;
+        double duration = journey.getDuration() / (60.0 * 60.0);
         if (dayofweek >= 0) {
-            passed[dayofweek] += journey.getOvertook() + journey.getOvertakenBy();
+            passed[dayofweek] += (journey.getOvertook() + journey.getOvertakenBy()) / duration;
             count[dayofweek]++;
         }
     }
@@ -44,7 +45,7 @@ void StatsWeekdayCongestion::update() {
         values << result;
     }
 
-    step = qRound(maxval / 5.0);
+    step = maxval > 5.0 ? qRound(maxval / 5.0) : (maxval / 5.0);
 
     qDebug() << "Calculated values";
 }
index 2b0dda2..1e53cff 100644 (file)
@@ -2,7 +2,6 @@
 #define STATSWEEKDAYCONGESTION_H
 
 #include "journeymodel.h"
-
 #include "stats.h"
 
 class StatsWeekdayCongestion : public Stats