QDateTimeAxis() - series are not displayed

倖福魔咒の 提交于 2020-12-04 05:27:35

问题


I am building application which includes QCharts. Everything was working until I changed Value Axis to the DateTime axis. Now I don't see any series on the chart. I was trying methods which was provided in other topics on stack overflow but without success.

I was trying as it was suggested in other topics to change datetime to msec since epoch when I am setting range of x axe - unfortunately with this method on x axe I see epoch time not current time. When I am setting range like now I see correct time on x axe but I don't see any series.

I've checked series - there are correct points in the range of x, y axis.

I am using python 3.7 and 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:


You must use the toMSecsSinceEpoch() method instead of toSecsSinceEpoch(). On the other side of my experience I have seen that it is necessary to establish the range each time data is added (maybe it is a QtCharts bug).

Considering the above the solution is:

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_())



来源:https://stackoverflow.com/questions/57079698/qdatetimeaxis-series-are-not-displayed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!