replace datagraph with dataqtgraph implementation to fix a scrolling bug

This commit is contained in:
2025-06-17 16:13:20 +02:00
parent fdab4e5caa
commit c3d9daa1b0

View File

@@ -2,9 +2,12 @@ import random
import sys import sys
from typing import Any, Union from typing import Any, Union
import loguru import loguru
import pyqtgraph as pg from PySide6 import QtWidgets, QtCore, QtGui
from PyQt6 import QtWidgets from PySide6.QtCore import Qt
from PySide6.QtGui import QPainter, QPen, QColor
from PySide6.QtCharts import QChart, QChartView, QLineSeries, QValueAxis, QCategoryAxis
from src import LOG_DIR from src import LOG_DIR
@@ -30,21 +33,24 @@ def mergedicts(d1: dict[str, Any], d2: dict[str, Any]):
res.update(d2_dict) # type: ignore res.update(d2_dict) # type: ignore
return res return res
class DataQtGraph(QtWidgets.QWidget):
class DataGraph(QtWidgets.QWidget):
def __init__( def __init__(
self, self,
title: str, title: str,
data=Union[dict[list, list], dict[list[dict[str, list[Any]]]]], data: dict,
generateMissing: bool = False, generateMissing: bool,
label: str = None, y_label: str,
x_rotation: int = 90,
): ):
super().__init__() super().__init__()
log.debug( self.series = QLineSeries()
"Initialized with options: {}, {}, {}, {}".format( self.chart = QChart()
title, data, generateMissing, label # scale the chart to fit the data
) self.chart.setTitle(title)
) self.chart.legend().setVisible(True)
layout = QtWidgets.QVBoxLayout()
lst = [] lst = []
if generateMissing: if generateMissing:
x_data = data["x"] x_data = data["x"]
@@ -71,54 +77,44 @@ class DataGraph(QtWidgets.QWidget):
lst.append(data) lst.append(data)
x_data = lst[0]["x"] # x_data = lst[0]["x"] #
xdict = dict(enumerate(x_data)) xdict = dict(enumerate(x_data))
stringaxis_x = pg.AxisItem(orientation="bottom")
stringaxis_x.setTicks([xdict.items()])
graph = pg.PlotWidget(axisItems={"bottom": stringaxis_x})
graph.addLegend()
colors = ["b", "r", "c", "m", "y", "k", "w"] print("xdict:", xdict)
symbols = [
"o",
"s",
"t",
"d",
"+",
"t1",
"t2",
"t3",
"p",
"h",
"star",
"x",
"arrow_up",
"arrow_down",
"arrow_left",
"arrow_right",
]
color_index = 0
index = 0
for data in lst: self.chart.createDefaultAxes()
symbol = symbols[random.randint(0, len(symbols) - 1)] for entry in lst:
if color_index >= len(colors): print("entry:", entry)
color_index = 0 entryseries = QLineSeries()
# iterate over the list, use y-data and y-label to plot the graph for x_val, y_val in zip(entry["x"], entry["y"]):
y_data = data["y"] #
label = data["y-label"] if "y-label" in data else label entryseries.append(entry["x"].index(x_val), y_val)
entryseries.setName(entry["y-label"] if "y-label" in entry else y_label)
pen = pg.mkPen(color=colors[color_index], width=2) self.chart.addSeries(entryseries)
if isinstance(y_data, list):
graph.plot( x_axis = QCategoryAxis()
list(xdict.keys()), y_data, pen=pen, symbol=symbol, name=label for index, semester in enumerate(lst[0]["x"]):
) x_axis.append(semester, index)
color_index += 1 x_axis.setLabelsPosition(
index += 1 QCategoryAxis.AxisLabelsPosition.AxisLabelsPositionOnValue
else: )
pass # rotare the label by 45 degrees
graph.setBackground("#d3d3d3") x_axis.setLabelsAngle(x_rotation)
graph.setTitle(title) x_axis.setLabelsFont(QtGui.QFont("Arial", 8, QtGui.QFont.Weight.Normal, False))
layout = QtWidgets.QVBoxLayout() self.chart.setAxisX(x_axis, entryseries)
layout.addWidget(graph)
# entryseries.append(
# str()
# )
self.chart.legend().setVisible(True)
self.chart.legend().setAlignment(QtCore.Qt.AlignmentFlag.AlignBottom)
# set legend labels
self.chart.setAxisY(QValueAxis(self.chart), entryseries)
# the chart's x axis labels are not being displayed, as the chart only has limited space. scale down the x axis font
chartview = QChartView(self.chart)
chartview.setRenderHint(QPainter.RenderHint.Antialiasing)
layout.addWidget(chartview)
self.setLayout(layout) self.setLayout(layout)
def generateMissingSemesters(self, data: dict[list]): def generateMissingSemesters(self, data: dict[list]):