From f5499d53e81c60f8ce6abfdbc6febf5cfa7de9ee Mon Sep 17 00:00:00 2001 From: WorldTeacher <41587052+WorldTeacher@users.noreply.github.com> Date: Mon, 3 Jun 2024 12:27:28 +0200 Subject: [PATCH] add graph widget --- src/ui/graph.py | 436 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 436 insertions(+) create mode 100644 src/ui/graph.py diff --git a/src/ui/graph.py b/src/ui/graph.py new file mode 100644 index 0000000..9beb983 --- /dev/null +++ b/src/ui/graph.py @@ -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)