Добавление скролла при изменении масштаба графика в PyQt
Есть график с возможностью увеличения масштаба при нажатии на горячие клавиши.
Вопрос 1. Как добавить автоматическое добавление скролла (полосы прокрутки) по вертикали и горизонтали с возможностью перемещения по стрелкам.
Вопрос 2: При изменении масштаба нужен перерасчёт значений координатных осей.
import sys
import math
from PySide6.QtWidgets import QWidget, QApplication, QHBoxLayout, QScrollArea, QVBoxLayout
from PySide6.QtCharts import QChart, QChartView, QLineSeries, QValueAxis
from PySide6.QtCore import QPointF
from PySide6.QtGui import QPainter, Qt
class Charts(QWidget):
def __init__(self, parent=None):
super(Charts, self).__init__(parent)
self.series1= QLineSeries()
self.zoomFactor = 1.1
self.zoom_value = 0
self.chart1 = QChart()
for i in range(0,2500,1):
self.series1.append([QPointF(i, math.sin(i * 0.01))])
self.chart1.addSeries(self.series1)
y_axis = QValueAxis()
y_axis.setRange(-100, 100)
y_axis.setTickType(QValueAxis.TickType.TicksDynamic)
y_axis.setTickInterval(20)
x_axis = QValueAxis()
x_axis.setRange(1, 2500)
x_axis.setTickInterval(100)
x_axis.setTickType(QValueAxis.TickType.TicksDynamic)
self.chart1.addAxis(x_axis, Qt.AlignmentFlag.AlignBottom)
self.chart1.addAxis(y_axis, Qt.AlignmentFlag.AlignLeft)
self.chart1.legend().hide()
self.chartview1 = QChartView(self.chart1)
self.chartview1.setRenderHint(QPainter.Antialiasing)
vbox = QVBoxLayout()
vbox.addWidget(self.chartview1)
self.setLayout(vbox)
def keyPressEvent(self, keyEvent):
key = keyEvent.key()
if key == Qt.Key_W and self.zoom_value < 10:
self.chartview1.chart().zoom(self.zoomFactor)
self.zoom_value = self.zoom_value + 1
elif key == Qt.Key_S and self.zoom_value > 0:
self.chartview1.chart().zoom(1 / self.zoomFactor)
self.zoom_value = self.zoom_value - 1
if __name__ == '__main__':
app = QApplication(sys.argv)
charts = Charts()
charts.show()
sys.exit(app.exec())
Ответы (1 шт):
Автор решения: S. Nick
→ Ссылка
Попробуйте так:
'''
from PySide6.QtWidgets import QWidget, QApplication, QHBoxLayout, QScrollArea, QVBoxLayout
from PySide6.QtCharts import QChart, QChartView, QLineSeries, QValueAxis
from PySide6.QtCore import QPointF
from PySide6.QtGui import QPainter, Qt
'''
import math
import sys
from PyQt5 import QtCore, QtGui, QtWidgets, QtChart
from PyQt5.QtChart import *
from PyQt5.Qt import *
x = 2500
x_ = x - 1
class MainWindow(QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.step = 1000
self.scrollbar = QtWidgets.QScrollBar(
QtCore.Qt.Horizontal,
sliderMoved=self.onAxisSliderMoved,
pageStep=self.step,
)
self.scrollbar.setRange(0, x_)
self.centralWidget = QtWidgets.QWidget()
self.setCentralWidget(self.centralWidget)
self.splitter = QtWidgets.QSplitter(QtCore.Qt.Vertical)
self.layout = QtWidgets.QVBoxLayout(self.centralWidget)
self.layout.addWidget(self.splitter, stretch=1)
self.layout.addWidget(self.scrollbar)
self.zoomFactor = 1.1
self.zoom_value = 0
self.series1 = QLineSeries()
for i in range(0, 2500, 1):
self.series1.append([QPointF(i, math.sin(i * 0.01))])
_pen = QPen(Qt.red)
self.series1.setPen(_pen)
y_axis = QValueAxis()
y_axis.setRange(-100, 100)
y_axis.setTickType(QValueAxis.TickType.TicksDynamic)
y_axis.setTickInterval(20)
x_axis = QValueAxis()
x_axis.setRange(1, 2500)
x_axis.setTickInterval(100)
x_axis.setTickType(QValueAxis.TickType.TicksDynamic)
self.chart1 = QChart()
self.chart1.addSeries(self.series1)
self.chart1.addAxis(x_axis, Qt.AlignmentFlag.AlignBottom)
self.chart1.addAxis(y_axis, Qt.AlignmentFlag.AlignLeft)
self.chart1.legend().hide()
self.chart1.createDefaultAxes()
self.chartview1 = QChartView(self.chart1)
self.chartview1.setRenderHint(QPainter.Antialiasing)
self.splitter.addWidget(self.chartview1)
self.onAxisSliderMoved(0)
def keyPressEvent(self, keyEvent):
key = keyEvent.key()
if key == Qt.Key_W and self.zoom_value < 10:
self.chartview1.chart().zoom(self.zoomFactor)
self.zoom_value = self.zoom_value + 1
elif key == Qt.Key_S and self.zoom_value > 0:
self.chartview1.chart().zoom(1 / self.zoomFactor)
self.zoom_value = self.zoom_value - 1
def adjust_axes(self, value_min, value_max):
for i in range(self.splitter.count()):
chart_view = self.splitter.widget(i)
if isinstance(chart_view, QtChart.QChartView):
chart = chart_view.chart()
chart = self.chart1
for serie in chart.series():
chart.axisX(serie).setRange(value_min, value_max)
@QtCore.pyqtSlot(int)
def onAxisSliderMoved(self, value):
value2 = value + self.step
value1 = value
if value2 >= x_:
value2 = x_
value1 = value2 - self.step
self.adjust_axes(math.floor(value1), math.ceil(value2))
if __name__ == '__main__':
app = QApplication(sys.argv)
w = MainWindow()
w.resize(500, 400)
w.show()
sys.exit(app.exec())