不显示QDateTimeAxis()系列

2024-06-16 13:18:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在构建包括QCharts的应用程序。在我将Value轴更改为DateTime轴之前,一切都正常。现在我在图表上看不到任何序列。我在尝试其他主题中提供的关于堆栈溢出的方法,但没有成功。在

我正试图像其他主题中建议的那样,将datetime更改为毫秒,因为我正在设置x轴的范围-不幸的是,在x轴上使用这个方法,我看到的是epoch时间而不是当前时间。 当我像现在这样设置范围时,我在x轴上看到了正确的时间,但我没有看到任何序列。在

我检查了序列-在x,y轴的范围内有正确的点。在

我使用的是python3.7和pyside2。在

self.plot = QtCharts.QChart()
self.add_series("Magnitude (Column 1)", [0, 1])
self.chart_view = QtCharts.QChartView(self.plot)

self.series = QtCharts.QLineSeries()
self.series.setName(name)
self.plot.addSeries(self.series)

# Setting X-axis
self.axis_x = QtCharts.QDateTimeAxis()
self.axis_x.setTickCount(10)
self.axis_x.setLabelsAngle(70)
self.axis_x.setFormat("dd.MM.yy h:mm:ss")
self.axis_x.setTitleText("Date")
self.axis_x.setMax(QDateTime.currentDateTime().addSecs(60))
self.axis_x.setMin(QDateTime.currentDateTime())

# Setting Y-axis
self.axis_y = QtCharts.QValueAxis()
self.axis_y.setTickCount(7)
self.axis_y.setLabelFormat("%i")
self.axis_y.setTitleText("Temperature [celcious]")
self.axis_y.setMax(30)
self.axis_y.setMin(20)

self.series.attachAxis(self.axis_x)
self.series.attachAxis(self.axis_y)
self.plot.addAxis(self.axis_x, Qt.AlignBottom)
self.plot.addAxis(self.axis_y, Qt.AlignLeft)
...

# Add points to the chart
def addPoint(self):
    x = QDateTime.currentDateTime().toSecsSinceEpoch()
    y = float(20+self.i)

    self.series.append(x, y)
    print(self.series.points())
    self.i += 1
    print(QDateTime.currentDateTime().toMSecsSinceEpoch(),y)

Tags: 方法self主题plotchart时间序列setting
1条回答
网友
1楼 · 发布于 2024-06-16 13:18:42

必须使用toMSecsSinceEpoch()方法,而不是toSecsSinceEpoch()。另一方面,我发现每次添加数据时都需要确定范围(可能是QtCharts错误)。在

考虑到上述情况,解决方案是:

import random
from PySide2 import QtCore, QtGui, QtWidgets
from PySide2.QtCharts import QtCharts


class MainWindow(QtWidgets.QMainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()

        self.plot = QtCharts.QChart()
        # self.add_series("Magnitude (Column 1)", [0, 1])
        self.chart_view = QtCharts.QChartView(self.plot)
        self.setCentralWidget(self.chart_view)

        self.series = QtCharts.QLineSeries()
        self.series.setName("Magnitude")
        self.plot.addSeries(self.series)

        # Setting X-axis
        self.axis_x = QtCharts.QDateTimeAxis()
        self.axis_x.setTickCount(10)
        self.axis_x.setLabelsAngle(70)
        self.axis_x.setFormat("dd.MM.yy h:mm:ss")
        self.axis_x.setTitleText("Date")
        self.axis_x.setMax(QtCore.QDateTime.currentDateTime().addSecs(60))
        self.axis_x.setMin(QtCore.QDateTime.currentDateTime())

        # Setting Y-axis
        self.axis_y = QtCharts.QValueAxis()
        self.axis_y.setTickCount(7)
        self.axis_y.setLabelFormat("%i")
        self.axis_y.setTitleText("Temperature [celcious]")
        self.axis_y.setMax(30)
        self.axis_y.setMin(20)

        self.plot.setAxisX(self.axis_x, self.series)
        self.plot.setAxisY(self.axis_y, self.series)
        # ...
        timer = QtCore.QTimer(self)
        timer.timeout.connect(self.addPoint)
        timer.start(500)

    # Add points to the chart
    def addPoint(self):
        dt = QtCore.QDateTime.currentDateTime()
        v = random.uniform(20, 30)
        self.series.append(dt.toMSecsSinceEpoch(), v)
        t_m, t_M = min(dt, self.axis_x.min()), max(dt, self.axis_x.max())
        m, M = min(v, self.axis_y.min()), max(v, self.axis_y.max())
        self.axis_x.setRange(t_m, t_M)
        self.axis_y.setRange(m, M)


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    window = MainWindow()
    window.resize(640, 480)
    window.show()
    sys.exit(app.exec_())

enter image description here

相关问题 更多 >