From 7415b7cb2b378dd428851106dbd2c570bb79245d Mon Sep 17 00:00:00 2001
From: Matteo Pignataro <matteo.pignataro@skywarder.eu>
Date: Fri, 11 Aug 2023 13:02:24 +0000
Subject: [PATCH] [Graphs] Updated legend to show the current value

---
 src/shared/Modules/Graph/Graph.cpp | 12 +++++++++++-
 src/shared/Modules/Graph/Graph.h   |  2 ++
 2 files changed, 13 insertions(+), 1 deletion(-)

diff --git a/src/shared/Modules/Graph/Graph.cpp b/src/shared/Modules/Graph/Graph.cpp
index 2fbb9ce7..3b10efda 100644
--- a/src/shared/Modules/Graph/Graph.cpp
+++ b/src/shared/Modules/Graph/Graph.cpp
@@ -137,7 +137,7 @@ void Graph::onFilterAdded(const Filter& filter)
         // Create the graph
         QCPGraph* graph = plot->addGraph();
         graph->setPen(
-            QPen(QColor(rand() % 255, rand() % 255, rand() % 255), 2));
+            QPen(QColor(rand() % 200, rand() % 200, rand() % 200), 2));
         graph->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssNone, 2));
         graph->setSelectable(QCP::stNone);
         graph->setName(field);
@@ -171,6 +171,9 @@ void Graph::onFilterAdded(const Filter& filter)
                     message.getField("timestamp").getUnsignedInteger() / 1e6;
                 double y = message.getField(field).getDouble();
 
+                lines[filter].first[field].graph->setName(field + ": " +
+                                                          QString::number(y));
+
                 // Check if the timestamp resets
                 if (lastTimestamp - 1 > x)
                     onClearClicked();
@@ -257,6 +260,13 @@ void Graph::setupUi()
 
     setLayout(outerLayout);
 
+    QFont pfont = QApplication::font();
+    plot->legend->setIconSize(32, 2 * legendFontSize - 2);
+    pfont.setPointSize(legendFontSize);
+    plot->legend->setFont(pfont);
+
+    plot->axisRect()->insetLayout()->setInsetAlignment(
+        0, Qt::AlignTop | Qt::AlignLeft);
     plot->legend->setVisible(true);
 
     // Allow user to drag axis ranges with mouse, zoom with mouse wheel and
diff --git a/src/shared/Modules/Graph/Graph.h b/src/shared/Modules/Graph/Graph.h
index cdc7ceb7..2b1c64e0 100644
--- a/src/shared/Modules/Graph/Graph.h
+++ b/src/shared/Modules/Graph/Graph.h
@@ -73,4 +73,6 @@ private:
 
     bool stopped   = false;
     bool following = true;
+
+    int legendFontSize = 15;
 };
-- 
GitLab