QDateTimeAxis() - 系列不显示 [英] QDateTimeAxis() - series are not displayed

查看:88
本文介绍了QDateTimeAxis() - 系列不显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

当我设置 x ax 的范围时,我正在尝试按照其他主题中的建议将日期时间更改为毫秒,因为我设置了 x ax 的范围 - 不幸的是,在 x ax 上使用这种方法时,我看到的纪元时间不是当前时间.当我像现在一样设置范围时,我在 x ax 上看到了正确的时间,但没有看到任何系列.

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.

我已经检查过系列 - 在 x、y 轴范围内有正确的点.

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

我正在使用 python 3.7 和 pyside2.

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)

推荐答案

您必须使用 toMSecsSinceEpoch() 方法而不是 toSecsSinceEpoch() 方法.在我经验的另一面,我看到每次添加数据时都需要建立范围(可能是 QtCharts 的错误).

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

这篇关于QDateTimeAxis() - 系列不显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆