add graph widget
This commit is contained in:
436
src/ui/graph.py
Normal file
436
src/ui/graph.py
Normal file
@@ -0,0 +1,436 @@
|
|||||||
|
import pyqtgraph as pg
|
||||||
|
from PyQt6 import QtWidgets
|
||||||
|
|
||||||
|
|
||||||
|
class Graph:
|
||||||
|
def __init__(self, data: dict | list):
|
||||||
|
self.xlist = []
|
||||||
|
if isinstance(data, dict):
|
||||||
|
self.xlist += data[list(data.keys())[0]]["x"]
|
||||||
|
else:
|
||||||
|
for entry in data:
|
||||||
|
self.xlist += entry[list(entry.keys())[0]]["x"]
|
||||||
|
self.xlist = [date.split("2024-")[1] for date in self.xlist]
|
||||||
|
xlist = list(set(self.xlist))
|
||||||
|
xlist.sort()
|
||||||
|
xdict = dict(enumerate(xlist))
|
||||||
|
print("xlist:", xlist)
|
||||||
|
print(xdict)
|
||||||
|
self.xdict = xdict
|
||||||
|
stringaxis_x = pg.AxisItem(orientation="bottom")
|
||||||
|
stringaxis_x.setTicks([xdict.items()])
|
||||||
|
self.graph = pg.PlotWidget(axisItems={"bottom": stringaxis_x})
|
||||||
|
# self.graph.("Zeitschriftennutzung")
|
||||||
|
self.graph.setLabel("left", "Anzahl der potentiellen Nutzungen")
|
||||||
|
self.graph.setLabel("bottom", "Datum")
|
||||||
|
|
||||||
|
self.graph.addLegend()
|
||||||
|
self.color = ["r", "g", "b", "y", "m", "c"]
|
||||||
|
if isinstance(data, list):
|
||||||
|
self.plot_lines(data)
|
||||||
|
else:
|
||||||
|
self.plot_line(data)
|
||||||
|
# remove duplicate x values
|
||||||
|
|
||||||
|
self.graph.showGrid(x=True, y=True)
|
||||||
|
# set max and min values for x and y axis
|
||||||
|
|
||||||
|
# self.graph.resize(800, 600)
|
||||||
|
|
||||||
|
def plot_line(self, data, color="r"):
|
||||||
|
line_label = list(data.keys())[0]
|
||||||
|
x_data = data[line_label]["x"]
|
||||||
|
self.xlist += x_data
|
||||||
|
x_data = [date.split("2024-")[1] for date in x_data]
|
||||||
|
xdict = dict(enumerate(x_data))
|
||||||
|
# stringaxis_x = pg.AxisItem(orientation="bottom")
|
||||||
|
# stringaxis_x.setTicks([xdict.items()])
|
||||||
|
# self.graph.getAxis("bottom").setTicks([xdict.items()])
|
||||||
|
global_xdict = self.xdict
|
||||||
|
|
||||||
|
# remove all x values that are not in the xdict
|
||||||
|
global_xdict = {
|
||||||
|
key: global_xdict[key] for key in xdict.keys() if key in global_xdict.keys()
|
||||||
|
}
|
||||||
|
print(line_label, global_xdict)
|
||||||
|
|
||||||
|
y_data = data[line_label]["y"]
|
||||||
|
self.graph.plot(
|
||||||
|
list(xdict.keys()),
|
||||||
|
y_data,
|
||||||
|
name=line_label,
|
||||||
|
pen=pg.mkPen(color=color, width=2),
|
||||||
|
)
|
||||||
|
|
||||||
|
def plot_lines(self, data):
|
||||||
|
i = 0
|
||||||
|
for entry in data:
|
||||||
|
self.plot_line(entry, self.color[i])
|
||||||
|
i += 1
|
||||||
|
|
||||||
|
|
||||||
|
def launch(data):
|
||||||
|
app = QtWidgets.QApplication([])
|
||||||
|
graph = Graph(data)
|
||||||
|
graph.graph.show()
|
||||||
|
app.exec()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
data = [
|
||||||
|
{
|
||||||
|
"t^1": {
|
||||||
|
"x": [
|
||||||
|
"2024-04-04",
|
||||||
|
"2024-04-05",
|
||||||
|
"2024-04-06",
|
||||||
|
"2024-04-07",
|
||||||
|
"2024-04-08",
|
||||||
|
"2024-04-09",
|
||||||
|
"2024-04-10",
|
||||||
|
"2024-04-11",
|
||||||
|
"2024-04-12",
|
||||||
|
"2024-04-13",
|
||||||
|
"2024-04-14",
|
||||||
|
"2024-04-15",
|
||||||
|
"2024-04-16",
|
||||||
|
"2024-04-17",
|
||||||
|
"2024-04-18",
|
||||||
|
"2024-04-19",
|
||||||
|
"2024-04-20",
|
||||||
|
"2024-04-21",
|
||||||
|
"2024-04-22",
|
||||||
|
"2024-04-23",
|
||||||
|
"2024-04-24",
|
||||||
|
"2024-04-25",
|
||||||
|
"2024-04-26",
|
||||||
|
"2024-04-27",
|
||||||
|
"2024-04-28",
|
||||||
|
"2024-04-29",
|
||||||
|
"2024-04-30",
|
||||||
|
"2024-05-01",
|
||||||
|
"2024-05-02",
|
||||||
|
"2024-05-03",
|
||||||
|
"2024-05-04",
|
||||||
|
"2024-05-05",
|
||||||
|
"2024-05-06",
|
||||||
|
"2024-05-07",
|
||||||
|
"2024-05-08",
|
||||||
|
"2024-05-09",
|
||||||
|
"2024-05-10",
|
||||||
|
"2024-05-11",
|
||||||
|
"2024-05-12",
|
||||||
|
"2024-05-13",
|
||||||
|
"2024-05-14",
|
||||||
|
"2024-05-15",
|
||||||
|
"2024-05-16",
|
||||||
|
"2024-05-17",
|
||||||
|
"2024-05-18",
|
||||||
|
"2024-05-19",
|
||||||
|
"2024-05-20",
|
||||||
|
"2024-05-21",
|
||||||
|
"2024-05-22",
|
||||||
|
"2024-05-23",
|
||||||
|
"2024-05-24",
|
||||||
|
"2024-05-25",
|
||||||
|
"2024-05-26",
|
||||||
|
"2024-05-27",
|
||||||
|
"2024-05-28",
|
||||||
|
],
|
||||||
|
"y": [
|
||||||
|
0,
|
||||||
|
9.0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
2.0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
2.0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
2.0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
3.0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
],
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"t^2": {
|
||||||
|
"x": [
|
||||||
|
"2024-04-04",
|
||||||
|
"2024-04-05",
|
||||||
|
"2024-04-06",
|
||||||
|
"2024-04-07",
|
||||||
|
"2024-04-08",
|
||||||
|
"2024-04-09",
|
||||||
|
"2024-04-10",
|
||||||
|
"2024-04-11",
|
||||||
|
"2024-04-12",
|
||||||
|
"2024-04-13",
|
||||||
|
"2024-04-14",
|
||||||
|
"2024-04-15",
|
||||||
|
"2024-04-16",
|
||||||
|
"2024-04-17",
|
||||||
|
"2024-04-18",
|
||||||
|
"2024-04-19",
|
||||||
|
"2024-04-20",
|
||||||
|
"2024-04-21",
|
||||||
|
"2024-04-22",
|
||||||
|
"2024-04-23",
|
||||||
|
"2024-04-24",
|
||||||
|
"2024-04-25",
|
||||||
|
"2024-04-26",
|
||||||
|
"2024-04-27",
|
||||||
|
"2024-04-28",
|
||||||
|
"2024-04-29",
|
||||||
|
"2024-04-30",
|
||||||
|
"2024-05-01",
|
||||||
|
"2024-05-02",
|
||||||
|
"2024-05-03",
|
||||||
|
"2024-05-04",
|
||||||
|
"2024-05-05",
|
||||||
|
"2024-05-06",
|
||||||
|
"2024-05-07",
|
||||||
|
"2024-05-08",
|
||||||
|
"2024-05-09",
|
||||||
|
"2024-05-10",
|
||||||
|
"2024-05-11",
|
||||||
|
"2024-05-12",
|
||||||
|
"2024-05-13",
|
||||||
|
"2024-05-14",
|
||||||
|
"2024-05-15",
|
||||||
|
"2024-05-16",
|
||||||
|
"2024-05-17",
|
||||||
|
"2024-05-18",
|
||||||
|
"2024-05-19",
|
||||||
|
"2024-05-20",
|
||||||
|
"2024-05-21",
|
||||||
|
"2024-05-22",
|
||||||
|
"2024-05-23",
|
||||||
|
"2024-05-24",
|
||||||
|
"2024-05-25",
|
||||||
|
"2024-05-26",
|
||||||
|
"2024-05-27",
|
||||||
|
"2024-05-28",
|
||||||
|
],
|
||||||
|
"y": [
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
4.0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
4.0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
6.0,
|
||||||
|
0,
|
||||||
|
2.0,
|
||||||
|
2.0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
2.0,
|
||||||
|
2.0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
],
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"t^3": {
|
||||||
|
"x": [
|
||||||
|
"2024-04-04",
|
||||||
|
"2024-04-05",
|
||||||
|
"2024-04-06",
|
||||||
|
"2024-04-07",
|
||||||
|
"2024-04-08",
|
||||||
|
"2024-04-09",
|
||||||
|
"2024-04-10",
|
||||||
|
"2024-04-11",
|
||||||
|
"2024-04-12",
|
||||||
|
"2024-04-13",
|
||||||
|
"2024-04-14",
|
||||||
|
"2024-04-15",
|
||||||
|
"2024-04-16",
|
||||||
|
"2024-04-17",
|
||||||
|
"2024-04-18",
|
||||||
|
"2024-04-19",
|
||||||
|
"2024-04-20",
|
||||||
|
"2024-04-21",
|
||||||
|
"2024-04-22",
|
||||||
|
"2024-04-23",
|
||||||
|
"2024-04-24",
|
||||||
|
"2024-04-25",
|
||||||
|
"2024-04-26",
|
||||||
|
"2024-04-27",
|
||||||
|
"2024-04-28",
|
||||||
|
"2024-04-29",
|
||||||
|
"2024-04-30",
|
||||||
|
"2024-05-01",
|
||||||
|
"2024-05-02",
|
||||||
|
"2024-05-03",
|
||||||
|
"2024-05-04",
|
||||||
|
"2024-05-05",
|
||||||
|
"2024-05-06",
|
||||||
|
"2024-05-07",
|
||||||
|
"2024-05-08",
|
||||||
|
"2024-05-09",
|
||||||
|
"2024-05-10",
|
||||||
|
"2024-05-11",
|
||||||
|
"2024-05-12",
|
||||||
|
"2024-05-13",
|
||||||
|
"2024-05-14",
|
||||||
|
"2024-05-15",
|
||||||
|
"2024-05-16",
|
||||||
|
"2024-05-17",
|
||||||
|
"2024-05-18",
|
||||||
|
"2024-05-19",
|
||||||
|
"2024-05-20",
|
||||||
|
"2024-05-21",
|
||||||
|
"2024-05-22",
|
||||||
|
"2024-05-23",
|
||||||
|
"2024-05-24",
|
||||||
|
"2024-05-25",
|
||||||
|
"2024-05-26",
|
||||||
|
"2024-05-27",
|
||||||
|
"2024-05-28",
|
||||||
|
],
|
||||||
|
"y": [
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
],
|
||||||
|
}
|
||||||
|
},
|
||||||
|
]
|
||||||
|
launch(data)
|
||||||
Reference in New Issue
Block a user