QDateTimeAxis()-系列不显示

时间:2019-07-17 15:41:50

标签: python pyside2 qtcharts

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

我正在尝试按照其他主题的建议,在将x轴的范围设置为epoch时将日期时间更改为自epoch以来的时间-不幸的是,在x ax上使用此方法时,我看到epoch time不是当前时间。 当我像现在这样设置范围时,我在x轴上看到正确的时间,但是看不到任何序列。

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

我正在使用python 3.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)

1 个答案:

答案 0 :(得分:2)

您必须使用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