RealTimeMonitorDemo Example

import sys, os, math
sys.path.append(os.path.dirname(os.path.realpath(__file__)) + "/../shared")

from DevMachines import __pyside2__, __pyside6__
from DevMachines.QtitanBase import Qtitan
from DevMachines.QtitanChart import (Chart, ChartBarSeries2D)

if __pyside2__:
    from PySide2 import QtCore
    from PySide2.QtCore import Qt
    from PySide2.QtWidgets import (QApplication, QPushButton, QLabel, QSlider)

if __pyside6__:
    from PySide6 import QtCore
    from PySide6.QtCore import Qt
    from PySide6.QtWidgets import (QApplication, QPushButton, QLabel, QSlider)

from DemoChartWindow import DemoChartWindow

class MainWindow(DemoChartWindow):
    MAX_POINTS_IN_RANGE = 750
    MIN_VAL             = -15.0
    MAX_VAL             = 15.0
    MIN_FREQUENCY       = 16
    def __init__(self):
        DemoChartWindow.__init__(self, "Real Time Monitor")
        self.values = list()
        self.setChart(None)
        self.frequency = 100
        self.timer = QtCore.QTimer()
        self.createSeriesParametrs()
        self.createChart()
        self.connect(self.timer, QtCore.SIGNAL("timeout()"), self, QtCore.SLOT("animation()"))

    def createSeriesParametrs(self):
        # Option Series
        seriesTypeGroup = self.createGroupParameters(self.tr("Real Time"))
        localLayout = seriesTypeGroup.layout()

        self.startButton = QPushButton(self.tr("Start"), seriesTypeGroup)
        self.connect(self.startButton, QtCore.SIGNAL("pressed()"), self, QtCore.SLOT("start()"))

        self.stopButton = QPushButton(self.tr("Stop"), seriesTypeGroup)
        self.connect(self.stopButton, QtCore.SIGNAL("pressed()"), self, QtCore.SLOT("stop()"))
        self.stopButton.setEnabled(False)

        updateFrequencyText = QLabel(self.tr("Update Frequency:"), seriesTypeGroup)

        frequencySlider = QSlider(Qt.Horizontal, seriesTypeGroup)
        frequencySlider.setRange(50, 300)
        frequencySlider.setSliderPosition(self.frequency)
        frequencySlider.setSingleStep(5)
        self.connect(frequencySlider, QtCore.SIGNAL("valueChanged(int)"), self, QtCore.SLOT("updateFrequency(int)"))
        # Layouts Series
        localLayout.addRow(self.startButton)
        localLayout.addRow(self.stopButton)
        localLayout.addRow(updateFrequencyText)
        localLayout.addRow(frequencySlider)

    def createChart(self):
        totalPoints = int(16000 / MainWindow.MAX_POINTS_IN_RANGE + 1)

        series = ChartBarSeries2D()
        series.label().setVisible(False)
        series.setDistanceFixed(25)
        self.chart.appendSeries(series)

        view = series.view()
        axisX = view.axisX()

        axisX.setGridSpacing(2.1)
        axisX.setGridSpacingAuto(False)
        axisX.setAllowZoom(True)

        axisY = view.axisY()
        axisY.setAutoRange(False)
        axisY.setFixedRange(MainWindow.MIN_VAL - 1, MainWindow.MAX_VAL + 1)

        title = axisY.title()
        title.setText(self.tr("dB"))
        title.setVisible(True)

        strCategory = ""
        DemoChartWindow.resize_list(self.values, totalPoints)
        dblArg = 0.0
        for i in range(0, totalPoints):
            self.values[i] = math.cos(dblArg) * MainWindow.MAX_VAL
            dblArg += 0.2
            frequency = i * MainWindow.MAX_POINTS_IN_RANGE + MainWindow.MIN_FREQUENCY
            if frequency < 1000:
                strCategory = "{:f}".format(frequency)
                if i == 0:
                    strCategory = strCategory + self.tr("Hz")
            else:
                strCategory = "{:f}".format(float(frequency) / 1000.)
                if i == totalPoints - 1:
                    strCategory = strCategory + self.tr("kHz")

            series.addAxisPointY(self.values[i], strCategory)

    def start(self):
        self.timer.start(self.frequency)
        self.startButton.setEnabled(False)
        self.stopButton.setEnabled(True)

    def stop(self):
        self.timer.stop()
        self.startButton.setEnabled(True)
        self.stopButton.setEnabled(False)

    def updateFrequency(self, frequency):
        self.frequency = frequency
        self.timer.setInterval(self.frequency)

    @QtCore.Slot()
    def animation(self):

        series2D = self.chart.series()[0]
        for i in range(0, len(self.values)):
            val = self.values[i]
            delta = self.getRandomValue(-2.0, 2.0)
            val = val + delta
            val = max(MainWindow.MIN_VAL, min(MainWindow.MAX_VAL, val))
            self.values[i] = val
            point = series2D.at(i)
            point.setValueY(val)

        self.chart.updateView()

if __name__ == "__main__":
    app = QApplication(sys.argv)
    w = MainWindow()
    w.show()
    sys.exit(app.exec_())