commit 70b83aa27c019932863ec7a7eb77d447c2d91869
Author: Jan Rach <jrach@students.zcu.cz>
Date:   Wed May 19 13:55:59 2021 +0200

    Re #8923 - Graphs displayed separately in a grid layout.

diff --git a/aswi2021vochomurka/view/main_view.py b/aswi2021vochomurka/view/main_view.py
index a769968..f5796d8 100644
--- a/aswi2021vochomurka/view/main_view.py
+++ b/aswi2021vochomurka/view/main_view.py
@@ -1,11 +1,8 @@
 import logging
 import math
-import random
 
 from PyQt5.QtCore import QSize, QThread, QObject, pyqtSignal
-from PyQt5.QtWidgets import QMainWindow, QPlainTextEdit, QDialog, QHBoxLayout
-from numpy import pi, sin, cos, tan, exp
-from matplotlib.pyplot import subplot
+from PyQt5.QtWidgets import QMainWindow, QPlainTextEdit, QDialog, QHBoxLayout, QGridLayout
 
 from aswi2021vochomurka.model.Message import Message
 from aswi2021vochomurka.service.mqtt.mqtt_subscriber import MQTTSubscriber
@@ -13,16 +10,10 @@ from aswi2021vochomurka.service.subscriber import Subscriber
 from aswi2021vochomurka.service.subscriber_callback import SubscriberCallback
 from aswi2021vochomurka.service.subscriber_params import SubscriberParams, ConnectionParams
 
-from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
-from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar
-import matplotlib.pyplot as plt
-
-import sys
 from PyQt5.QtWidgets import QDialog, QApplication, QPushButton, QVBoxLayout
 from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
 from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar
 import matplotlib.pyplot as plt
-import random
 
 from aswi2021vochomurka.view.logger_view import LoggerView
 
@@ -31,11 +22,11 @@ class Worker(QObject, SubscriberCallback):
     connected = pyqtSignal()
     disconnected = pyqtSignal()
     error = pyqtSignal(Exception)
-    newMessage = pyqtSignal(str)
+    newMessage = pyqtSignal(Message)
     subscriber: Subscriber = None
 
     params = SubscriberParams(
-        ["/home/1", "/home/2"],
+        ["/home/1", "/home/2", "/home/3", "/home/4", "/home/5", "/home/6", "/home/7", "/home/8"],
         10,
         ConnectionParams("localhost", 1883, 60),
         True
@@ -55,8 +46,8 @@ class Worker(QObject, SubscriberCallback):
         self.error.emit()
 
     def onMessage(self, message: Message):
-        self.newMessage.emit(message.topic)
-        self.window.plot(message)
+        self.newMessage.emit(message)
+        #self.window.plot(message)
 
     def onCloseTopic(self, topic: str):
         pass
@@ -75,12 +66,13 @@ class MainView(QDialog):
         self.dataIndex = 0
         self.dataDict = {}
 
-        self.figure = plt.figure(figsize=([500,500]))
+        self.canvasDict = {}
+        self.figureDict = {}
 
-        self.canvas = FigureCanvas(self.figure)
-        self.toolbar = NavigationToolbar(self.canvas, self)
+        #self.toolbar = NavigationToolbar(self.canvas, self)
 
-        self.setMinimumSize(QSize(440, 240))
+        #self.setMinimumSize(QSize(440, 240))
+        self.setMinimumSize(QSize(1200, 800))
         self.setWindowTitle("MQTT demo")
 
         # Add logger text field
@@ -92,39 +84,50 @@ class MainView(QDialog):
 
         layout = QVBoxLayout()
         layout.addWidget(logger.widget)
-        layout.addWidget(self.toolbar)
-        layout.addWidget(self.canvas)
+        #layout.addWidget(self.toolbar)
 
         self.setLayout(layout)
 
-        self.initSubscriber()
+        self.grid = QGridLayout()
 
-    def plot(self, message: Message):
-        self.figure.clear()
+        layout.addLayout(self.grid)
+
+        self.init_subscriber()
 
+    def plot(self, message: Message):
         if message.topic in self.dataDict:
             self.dataDict[message.topic].append(message.value)
+
+            figure = self.figureDict[message.topic]
+            figure.clear()
+
+            plt.figure(figure.number)
+            plt.plot(self.dataDict[message.topic])
+
+            self.canvasDict[message.topic].draw()
         else:
             self.dataDict[message.topic] = [message.value]
-            self.chartsNum += 1
 
-        rows = math.ceil(self.chartsNum / 2)
+            figure = plt.figure(figsize=[500, 500])
+            canvas = FigureCanvas(figure)
 
-        b = 0
-        for a in self.dataDict.values():
-            self.figure.add_subplot(rows, 2, b + 1)
-            b += 1
-            plt.plot(a)
+            plt.plot(self.dataDict[message.topic])
 
-        self.canvas.draw()
+            self.canvasDict[message.topic] = canvas
+            self.figureDict[message.topic] = figure
+
+            self.grid.addWidget(canvas, int(self.chartsNum / 2), self.chartsNum % 2)
+
+            self.chartsNum += 1
 
-    def initSubscriber(self):
+    def init_subscriber(self):
         self.workerThread = QThread()
         self.worker = Worker()
         self.worker.moveToThread(self.workerThread)
         self.workerThread.started.connect(self.worker.start)
-        # self.worker.newMessage.connect(
-        #    lambda message: self.b.insertPlainText(message + "\n")
-        # )
+        self.worker.newMessage.connect(
+            lambda message: self.plot(message)
+        )
         self.worker.window = self
         self.workerThread.start()
+        #lambda message: self.b.insertPlainText(message + "\n")
\ No newline at end of file
