TraceDemo/src/gui/widgets/plot.py

60 lines
2.4 KiB
Python
Raw Normal View History

import pandas as pd
from PyQt5.QtWidgets import QWidget, QVBoxLayout
import pyqtgraph as pg
import numpy as np
from base.base import BasePlotWidget
class PlotWidget(BasePlotWidget):
def _create_stage_region(self,
stage: str,
times: pd.Series,
dataframe: pd.DataFrame) -> pg.LinearRegionItem:
stage_diff = np.diff(dataframe[stage])
start_index = np.where(stage_diff == 1)[0]
finish_index = np.where(stage_diff == -1)[0]
start_timestamp = times[start_index[0]]
finish_timestamp = times[finish_index[0]]
region = pg.LinearRegionItem([start_timestamp, finish_timestamp], movable=False)
region.setBrush(pg.mkBrush(self._stage_colors[stage]))
return region
def _build_widget(self, dataframe: pd.DataFrame) -> QWidget:
widget = QWidget()
layout = QVBoxLayout()
time_axis = dataframe["time"]
dataframe_headers = dataframe.columns.tolist()
for channel, description in self._plt_channels.items():
plot_widget = pg.PlotWidget(title=channel)
plot_widget.showGrid(x=True, y=True)
legend = pg.LegendItem((80, 60), offset=(70, 20))
legend.setParentItem(plot_widget.graphicsItem())
settings = description["Settings"]
if settings["stages"] and all([stage in dataframe_headers for stage in self._stages]):
for stage in self._stages:
region = self._create_stage_region(stage, time_axis, dataframe)
plot_widget.addItem(region)
for signal in description["Signals"]:
if signal["name"] in dataframe_headers:
plot = plot_widget.plot(time_axis, dataframe[signal["name"]], pen=signal["pen"])
legend.addItem(plot, signal["name"])
if settings["zoom"] and max(time_axis) < 5.0:
max_value = max(dataframe[signal["name"]])
plot_widget.setYRange(max_value - 200, max_value)
plot_widget.setInteractive(False)
layout.addWidget(plot_widget)
widget.setLayout(layout)
return widget
def build(self, data: list[pd.DataFrame]) -> None:
widgets = [self._build_widget(data_sample) for data_sample in data]
self._mediator.notify(self, widgets)