我正在建设的应用程序,其中包括QCharts。在我将Value axis更改为DateTime轴之前,一切都正常工作。现在我在图表上看不到任何序列。我尝试了在其他关于堆栈溢出的主题中提供的方法,但没有成功。
当我在设置x轴的范围时,我尝试着像在其他主题中建议的那样,将日期时间更改为毫秒,因为当我设置x轴的范围时-不幸的是,在x轴上使用这种方法,我看到的是纪元时间而不是当前时间。当我像现在这样设置范围时,我在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)发布于 2019-07-18 00:22:36
必须使用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_())

https://stackoverflow.com/questions/57079698
复制相似问题