From 24c20995d54a3eef3cf99b1471cf1594ba5a50ba Mon Sep 17 00:00:00 2001
From: Alberto Nidasio <alberto.nidasio@skywarder.eu>
Date: Mon, 5 Sep 2022 09:38:37 +0200
Subject: [PATCH] [Graph] Fixed legend and timestamp reset

---
 Modules/Graph/Graph.cpp | 26 ++++++++++++++++++++++----
 Modules/Graph/Graph.h   |  2 +-
 2 files changed, 23 insertions(+), 5 deletions(-)

diff --git a/Modules/Graph/Graph.cpp b/Modules/Graph/Graph.cpp
index 37da61de..7e78bba7 100644
--- a/Modules/Graph/Graph.cpp
+++ b/Modules/Graph/Graph.cpp
@@ -93,8 +93,7 @@ void Graph::onClearClicked()
 {
     for (auto graph : graphs)
         graph->data()->clear();
-
-    // plot->replot();
+    plot->replot();
 }
 
 void Graph::onStopClicked(bool checked)
@@ -125,14 +124,21 @@ void Graph::onSubscriptionAdded(const TopicAndFieldFilter& filter)
         graphs.append(graph);
         buffersX.append(QVector<double>());
         buffersY.append(QVector<double>());
+
+        qDebug() << "Graph index:" << graphs.indexOf(graph);
     }
 }
 
 void Graph::onSubscriptionRemoved(const TopicAndFieldFilter& filter)
 {
+    qDebug() << "Removing filter" << filter.toString();
     if (filters.contains(filter))
     {
         int index = filters.indexOf(filter);
+        qDebug() << "Filter found, index:" << index;
+
+        plot->removeGraph(graphs[index]);
+        plot->replot();
 
         filters.removeAt(index);
         graphs.removeAt(index);
@@ -202,8 +208,20 @@ void Graph::onMsgReceived(const ModuleMessage& msg)
             double x = msg.getField("timestamp").getUInteger(0) / 1e6;
             double y = value.getDouble(0);
 
-            bufferX.append(x);
-            bufferY.append(y);
+            // Check if the timestamp resets
+            if (bufferX.last() < x)
+            {
+                bufferX.append(x);
+                bufferY.append(y);
+            }
+            else
+            {
+                bufferX.clear();
+                bufferY.clear();
+
+                auto graph = graphs[index];
+                graph->data()->clear();
+            }
         }
     }
 }
diff --git a/Modules/Graph/Graph.h b/Modules/Graph/Graph.h
index e595a701..041cede4 100644
--- a/Modules/Graph/Graph.h
+++ b/Modules/Graph/Graph.h
@@ -47,7 +47,7 @@ private:
     QList<QVector<double>> buffersY;
 
     QTimer updaterTimer;
-    int updatePeriod = 1000 / 10;
+    int updatePeriod = 1000 / 5;  // 5fps
 
     bool stopped   = false;
     bool following = true;
-- 
GitLab