gpt4 book ai didi

python - 当绘图到达 QChartView 边框时如何 ScrollView

转载 作者:行者123 更新时间:2023-12-01 07:20:00 27 4
gpt4 key购买 nike

我想创建一个动态图表,当绘图到达其可见边界时,它将移动图 TableView 。不幸的是,我不知道如何检测何时应该移动 View 。

顺便说一句, scroll 方法应该适用于移动 View (据我所知),但即使我尝试 scroll(1000, 0)< 也没有可见的结果

这是我的小部件类

from PySide2 import QtGui, QtWidgets, QtCore
from PySide2.QtCharts import QtCharts
from psutil import cpu_percent
import sys


class cpu_chart(QtCharts.QChart):
def __init__(self, input_data=None, parent=None):
super().__init__(parent)

self.axisX = QtCharts.QValueAxis()
self.axisY = QtCharts.QValueAxis()

self.axisX.setVisible(False)

self.step = 0
self.x = 0
self.y = 0

self.timer = QtCore.QTimer()
self.timer.setInterval(100)
self.timer.timeout.connect(self.onTimeout)

self.m_series = QtCharts.QSplineSeries()

pen = QtGui.QPen(QtCore.Qt.green)
pen.setWidth(1)

self.m_series.setPen(pen)
self.m_series.append(self.x, self.y)

self.addSeries(self.m_series)
self.addAxis(self.axisX, QtCore.Qt.AlignBottom)
self.addAxis(self.axisY, QtCore.Qt.AlignLeft)

self.m_series.attachAxis(self.axisX)
self.m_series.attachAxis(self.axisY)


self.axisX.setRange(0, 100)
self.axisY.setTickCount(5)
self.axisY.setRange(0, 100)

self.timer.start()

def onTimeout(self):
self.x += 1
self.y = cpu_percent()

self.m_series.append(self.x, self.y)
self.scroll(10, 0)

if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = QtWidgets.QMainWindow()

chart = cpu_chart()
chart.legend().hide()
chart.setAnimationOptions(QtCharts.QChart.AllAnimations)

chart_view = QtCharts.QChartView(chart)
chart_view.setRenderHint(QtGui.QPainter.Antialiasing)

window.setCentralWidget(chart_view)
window.resize(400, 300)
window.show()

sys.exit(app.exec_())

最佳答案

您必须更改 X 轴的范围。

  • 如果您想显示所有数据:
def onTimeout(self):
self.x += 1
self.y = cpu_percent()
self.m_series.append(self.x, self.y)
self.axisX.setRange(0, self.x)
  • 如果要显示最新的N条数据:
def onTimeout(self):
self.x += 1
self.y = cpu_percent()
self.m_series.append(self.x, self.y)
N = 100
self.axisX.setRange(max(0, self.x - N), self.x)

关于python - 当绘图到达 QChartView 边框时如何 ScrollView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57748558/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com