mirror of
https://github.com/bec-project/bec_widgets.git
synced 2026-05-09 08:12:15 +02:00
Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| a523e66536 |
@@ -2,11 +2,12 @@ from typing import List
|
|||||||
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import pyqtgraph as pg
|
import pyqtgraph as pg
|
||||||
|
from PyQt5.QtWidgets import QApplication, QGridLayout, QSizePolicy, QWidget
|
||||||
from pyqtgraph import mkPen
|
from pyqtgraph import mkPen
|
||||||
from pyqtgraph.Qt import QtCore, QtWidgets
|
from pyqtgraph.Qt import QtCore
|
||||||
|
|
||||||
|
|
||||||
class ConfigPlotter(pg.GraphicsWidget):
|
class ConfigPlotter(QWidget):
|
||||||
"""
|
"""
|
||||||
ConfigPlotter is a widget that can be used to plot data from multiple channels
|
ConfigPlotter is a widget that can be used to plot data from multiple channels
|
||||||
in a grid layout. The layout is specified by a list of dicts, where each dict
|
in a grid layout. The layout is specified by a list of dicts, where each dict
|
||||||
@@ -28,7 +29,7 @@ class ConfigPlotter(pg.GraphicsWidget):
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, configs: List[dict], parent=None):
|
def __init__(self, configs: List[dict], parent=None):
|
||||||
super().__init__(parent)
|
super(ConfigPlotter, self).__init__()
|
||||||
self.configs = configs
|
self.configs = configs
|
||||||
self.plots = {}
|
self.plots = {}
|
||||||
self._init_ui()
|
self._init_ui()
|
||||||
@@ -37,19 +38,46 @@ class ConfigPlotter(pg.GraphicsWidget):
|
|||||||
def _init_ui(self):
|
def _init_ui(self):
|
||||||
pg.setConfigOption("background", "w")
|
pg.setConfigOption("background", "w")
|
||||||
pg.setConfigOption("foreground", "k")
|
pg.setConfigOption("foreground", "k")
|
||||||
|
self.layout = QGridLayout()
|
||||||
|
self.setLayout(self.layout)
|
||||||
self.pen = mkPen(color=(56, 76, 107), width=4, style=QtCore.Qt.SolidLine)
|
self.pen = mkPen(color=(56, 76, 107), width=4, style=QtCore.Qt.SolidLine)
|
||||||
|
self.show()
|
||||||
self.view = pg.GraphicsView()
|
|
||||||
self.view.setAntialiasing(True)
|
|
||||||
self.view.show()
|
|
||||||
|
|
||||||
self.layout = pg.GraphicsLayout()
|
|
||||||
self.view.setCentralWidget(self.layout)
|
|
||||||
|
|
||||||
def _init_plots(self):
|
def _init_plots(self):
|
||||||
for config in self.configs:
|
for config in self.configs:
|
||||||
channels = config["config"]["channels"]
|
channels = config["config"]["channels"]
|
||||||
for channel in channels:
|
for channel in channels:
|
||||||
|
# call the corresponding init function, e.g. init_plotitem
|
||||||
|
init_func = getattr(self, f"init_{config['config']['item']}")
|
||||||
|
init_func(channel, config)
|
||||||
|
|
||||||
|
# self.init_ImageItem(channel, config["config"], item)
|
||||||
|
|
||||||
|
def init_PlotItem(self, channel: str, config: dict):
|
||||||
|
"""
|
||||||
|
Initialize a PlotItem
|
||||||
|
|
||||||
|
Args:
|
||||||
|
channel(str): channel to plot
|
||||||
|
config(dict): config dict for the channel
|
||||||
|
"""
|
||||||
|
# pylint: disable=invalid-name
|
||||||
|
plot_widget = pg.PlotWidget()
|
||||||
|
plot_widget.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
|
||||||
|
self.layout.addWidget(plot_widget, config["y"], config["x"], config["rows"], config["cols"])
|
||||||
|
plot_data = plot_widget.plot(np.random.rand(100), pen=self.pen)
|
||||||
|
# item.setLabel("left", channel)
|
||||||
|
# self.plots[channel] = {"item": item, "plot_data": plot_data}
|
||||||
|
|
||||||
|
def init_ImageItem(self, channel: str, config: dict):
|
||||||
|
"""
|
||||||
|
Initialize an ImageItem
|
||||||
|
|
||||||
|
Args:
|
||||||
|
channel(str): channel to plot
|
||||||
|
config(dict): config dict for the channel
|
||||||
|
"""
|
||||||
|
# pylint: disable=invalid-name
|
||||||
item = pg.PlotItem()
|
item = pg.PlotItem()
|
||||||
self.layout.addItem(
|
self.layout.addItem(
|
||||||
item,
|
item,
|
||||||
@@ -58,42 +86,25 @@ class ConfigPlotter(pg.GraphicsWidget):
|
|||||||
rowspan=config["rows"],
|
rowspan=config["rows"],
|
||||||
colspan=config["cols"],
|
colspan=config["cols"],
|
||||||
)
|
)
|
||||||
|
|
||||||
# call the corresponding init function, e.g. init_plotitem
|
|
||||||
init_func = getattr(self, f"init_{config['config']['item']}")
|
|
||||||
init_func(channel, config["config"], item)
|
|
||||||
|
|
||||||
# self.init_ImageItem(channel, config["config"], item)
|
|
||||||
|
|
||||||
def init_PlotItem(self, channel: str, config: dict, item: pg.GraphicsItem):
|
|
||||||
"""
|
|
||||||
Initialize a PlotItem
|
|
||||||
|
|
||||||
Args:
|
|
||||||
channel(str): channel to plot
|
|
||||||
config(dict): config dict for the channel
|
|
||||||
item(pg.GraphicsItem): PlotItem to plot the data
|
|
||||||
"""
|
|
||||||
# pylint: disable=invalid-name
|
|
||||||
plot_data = item.plot(np.random.rand(100), pen=self.pen)
|
|
||||||
item.setLabel("left", channel)
|
|
||||||
self.plots[channel] = {"item": item, "plot_data": plot_data}
|
|
||||||
|
|
||||||
def init_ImageItem(self, channel: str, config: dict, item: pg.GraphicsItem):
|
|
||||||
"""
|
|
||||||
Initialize an ImageItem
|
|
||||||
|
|
||||||
Args:
|
|
||||||
channel(str): channel to plot
|
|
||||||
config(dict): config dict for the channel
|
|
||||||
item(pg.GraphicsItem): ImageItem to plot the data
|
|
||||||
"""
|
|
||||||
# pylint: disable=invalid-name
|
|
||||||
img = pg.ImageItem()
|
img = pg.ImageItem()
|
||||||
item.addItem(img)
|
item.addItem(img)
|
||||||
img.setImage(np.random.rand(100, 100))
|
img.setImage(np.random.rand(100, 100))
|
||||||
self.plots[channel] = {"item": item, "plot_data": img}
|
self.plots[channel] = {"item": item, "plot_data": img}
|
||||||
|
|
||||||
|
def init_ImageView(self, channel: str, config: dict):
|
||||||
|
"""
|
||||||
|
Initialize an ImageView
|
||||||
|
|
||||||
|
Args:
|
||||||
|
channel(str): channel to plot
|
||||||
|
config(dict): config dict for the channel
|
||||||
|
"""
|
||||||
|
# pylint: disable=invalid-name
|
||||||
|
img = pg.ImageView()
|
||||||
|
img.setImage(np.random.rand(100, 100))
|
||||||
|
self.layout.addWidget(img, config["y"], config["x"], config["rows"], config["cols"])
|
||||||
|
self.plots[channel] = {"item": img, "plot_data": img}
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
import sys
|
import sys
|
||||||
@@ -118,10 +129,10 @@ if __name__ == "__main__":
|
|||||||
"rows": 2,
|
"rows": 2,
|
||||||
"y": 0,
|
"y": 0,
|
||||||
"x": 1,
|
"x": 1,
|
||||||
"config": {"channels": ["c"], "label_xy": ["", "c"], "item": "ImageItem"},
|
"config": {"channels": ["c"], "label_xy": ["", "c"], "item": "ImageView"},
|
||||||
},
|
},
|
||||||
]
|
]
|
||||||
|
|
||||||
app = QtWidgets.QApplication(sys.argv)
|
app = QApplication(sys.argv)
|
||||||
win = ConfigPlotter(CONFIG)
|
win = ConfigPlotter(CONFIG)
|
||||||
pg.exec()
|
pg.exec()
|
||||||
|
|||||||
Reference in New Issue
Block a user