有一个沿轴相当大的图X
。只有一部分应该显示在窗口中,其余部分应该可以通过 QScrollArea 中的滑块滚动。
from PySide6.QtWidgets import *
from PySide6.QtGui import *
from PySide6.QtCore import *
from PySide6.QtCharts import *
import pandas as pd
import sys
# Функция чтения данных
def read_csv_file( file, n):
df = pd.read_csv(file)
col = df.loc[:, n]
return col.tolist()
# Скролл обёртка для графика
class ScrollChartClass(QWidget):
def __init__(self, chart):
super().__init__()
self.chart = chart
self.initUI()
def initUI(self):
self.scroll = QScrollArea()
self.hbox = QHBoxLayout()
self.scroll.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.scroll.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
self.scroll.setWidgetResizable(True)
self.scroll.sizePolicy()
self.scroll.setWidget(self.chart)
self.hbox.addWidget(self.scroll)
self.setLayout(self.hbox)
return
class Main(QWidget):
def __init__(self, parent: QObject = None):
super().__init__(parent)
self.filename = "example_csv_file.csv"
self.resize(1000, 250)
self.initUI()
def initUI(self):
# достали данные
self.data1_1 = read_csv_file(self.filename, 'plan_mes')
self.data1_2 = read_csv_file(self.filename, 'plan_prj')
seriesData1_1 = QLineSeries()
seriesData1_2 = QLineSeries()
# одинаковое число строк в файле => для всех графиков одинаковая длина OX
for x in range(len(self.data1_1)):
seriesData1_1.append(QPointF(x, self.data1_1[x]))
seriesData1_1.setColor(QColor("green"))
seriesData1_1.setName('plan_mes')
seriesData1_2.append(QPointF(x, self.data1_2[x]))
seriesData1_2.setColor(QColor("red"))
seriesData1_2.setName('plan_prj')
x_axis = QValueAxis()
x_axis.setRange(0.0, len(self.data1_1))
y_axis = QValueAxis()
y_axis.setRange(min(self.data1_1), max(self.data1_1))
chartModel = QChart()
chartModel.layout().setContentsMargins(0, 0, 0, 0) #
chartModel.addSeries(seriesData1_1)
chartModel.addSeries(seriesData1_2)
chartModel.createDefaultAxes()
windowLayout = QGridLayout() # было бы достаточно QHBoxLayout()
view = QChartView(chartModel)
view.setRenderHint(QPainter.Antialiasing)
scroll_chart_class = ScrollChartClass(view)
windowLayout.addWidget(scroll_chart_class, 0, 0)
self.setLayout(windowLayout)
if __name__ == "__main__":
import sys
app = QApplication()
w = Main()
w.show()
sys.exit(app.exec())
该图是根据玩具文件中的数据构建的,现在有 200 行,即 沿轴有 200 个值X
。现在这 200 行全部同时显示。
例如,我需要查看其中的 50 个,其余的应该在卷轴下。
第一个想法是调整设置x_axis.setRange
,但不行,行不通。
该示例是在PySide中制作的,这与PyQt相同,不要让它混淆任何人。
2 个回答