gpt4 book ai didi

python - 更新PYQT5中的图表数据

转载 作者:行者123 更新时间:2023-12-01 06:34:17 28 4
gpt4 key购买 nike

我的上一个问题 Insert QChartView to ui

def draw_chart(self, obj, chart_view, replace=0):
obj.setContentsMargins(0, 0, 0, 0)
lay = QtWidgets.QHBoxLayout(obj)
lay.setContentsMargins(0, 0, 0, 0)
if replace:
# lay.replaceWidget(chart_view)
pass
else:
lay.addWidget(chart_view)

我这样调用这个函数:

    cv1 = self.candle_chart(self.p)
self.draw_chart(self.chart1, cv1, 0)

candle_chart 返回一个 QtChart.QChartView 对象,self.chart1Qwidget 的对象名称。

它第一次工作正常,但当我想更新图表时,它给出错误:

QLayout: Attempting to add QLayout "" to QWidget "chart1", which already has a layout

我正在尝试通过单击按钮更改图表数据

完整代码:

class Test(base_3, form_3):
def __init__(self):
super(base_3, self).__init__()
self.setupUi(self)

self.p = [(1, 7380, 7520, 7380, 7510, 7324),
(2, 7520, 7580, 7410, 7440, 7372),
(3, 7440, 7650, 7310, 7520, 7434),
(4, 7450, 7640, 7450, 7550, 7480),
(5, 7510, 7590, 7460, 7490, 7502),
(6, 7500, 7590, 7480, 7560, 7512),
(7, 7560, 7830, 7540, 7800, 7584)]

self.next.clicked.connect(self.more)

self.lmt = 3
self.st = 0

cv1 = self.candle_chart(self.p[self.st:self.lmt])
self.draw_chart(self.chart1, cv1, 0)

def more(self):
cv1 = self.candle_chart(self.p[self.st:self.lmt])
self.draw_chart(self.chart1, cv1, 1)

self.st += 1
self.lmt += 1

def draw_chart(self, label, chart_view, replace=0):
label.setContentsMargins(0, 0, 0, 0)
lay = QtWidgets.QHBoxLayout(label)
lay.setContentsMargins(0, 0, 0, 0)
if replace:
# lay.replaceWidget(chart_view)
pass
else:
lay.addWidget(chart_view)


def candle_chart(self, data):
series = QtChart.QCandlestickSeries()
series.setDecreasingColor(QtCore.Qt.red)
series.setIncreasingColor(QtCore.Qt.green)

tm = []
i = 0
for d, o, h, l, c, v in data:
# x = x[1]
# series.append(QtChart.QCandlestickSet(x["open"], x["high"], x["low"], x["close"]))
series.append(QtChart.QCandlestickSet(o, h, l, c))
tm.append(str(i))
i += 1

chart = QtChart.QChart()
chart.addSeries(series)
chart.createDefaultAxes()
chart.legend().hide()
chart.axisX(series).setCategories(tm)
chart_view = QtChart.QChartView(chart)
return chart_view

用户界面代码:

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>1381</width>
<height>680</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QPushButton" name="next">
<property name="geometry">
<rect>
<x>490</x>
<y>540</y>
<width>88</width>
<height>33</height>
</rect>
</property>
<property name="text">
<string>Next</string>
</property>
</widget>
<widget class="QWidget" name="chart1" native="true">
<property name="enabled">
<bool>true</bool>
</property>
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>460</width>
<height>250</height>
</rect>
</property>
</widget>
<widget class="QWidget" name="chart2" native="true">
<property name="geometry">
<rect>
<x>460</x>
<y>0</y>
<width>460</width>
<height>250</height>
</rect>
</property>
</widget>
<widget class="QWidget" name="chart3" native="true">
<property name="geometry">
<rect>
<x>920</x>
<y>0</y>
<width>460</width>
<height>250</height>
</rect>
</property>
</widget>
<widget class="QWidget" name="chart5" native="true">
<property name="geometry">
<rect>
<x>450</x>
<y>250</y>
<width>450</width>
<height>250</height>
</rect>
</property>
</widget>
<widget class="QWidget" name="chart6" native="true">
<property name="geometry">
<rect>
<x>900</x>
<y>250</y>
<width>450</width>
<height>250</height>
</rect>
</property>
</widget>
<widget class="QWidget" name="chart4" native="true">
<property name="enabled">
<bool>true</bool>
</property>
<property name="geometry">
<rect>
<x>0</x>
<y>250</y>
<width>450</width>
<height>250</height>
</rect>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>1381</width>
<height>25</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>

最佳答案

您不必更换小部件,最好重复使用,如下所示:

import sys

from PyQt5 import QtCore, QtWidgets, uic, QtChart

# load both ui file
uifile_1 = "UI/main.ui"
form_1, base_1 = uic.loadUiType(uifile_1)


class Example(base_1, form_1):
def __init__(self):
super(base_1, self).__init__()
self.setupUi(self)

self.series = QtChart.QCandlestickSeries()
self.series.setDecreasingColor(QtCore.Qt.red)
self.series.setIncreasingColor(QtCore.Qt.green)

self.ma5 = QtChart.QLineSeries() # 5-days average data line

self.chart = QtChart.QChart()
self.chart.addSeries(self.series) # candle
self.chart.addSeries(self.ma5) # ma5 line
self.chart.createDefaultAxes()
self.chart.legend().hide()
self.chart.axisX(self.ma5).setVisible(False)

self.chart.setAnimationOptions(QtChart.QChart.SeriesAnimations)

self.chartview = QtChart.QChartView(self.chart)

self.chart_container.setContentsMargins(0, 0, 0, 0)
lay = QtWidgets.QHBoxLayout(self.chart_container)
lay.setContentsMargins(0, 0, 0, 0)
lay.addWidget(self.chartview)

self.update_data()

timer = QtCore.QTimer(self, interval=1000, timeout=self.update_data)
timer.start()

def update_data(self):

import random

data = ((i, *random.sample(range(7000, 8000), 5)) for i in range(7))

self.series.clear()
self.ma5.clear()

tm = [] # stores str type data
x_min, x_max, y_min, y_max = (
float("inf"),
-float("inf"),
float("inf"),
-float("inf"),
)
# in a loop, series and ma5 append corresponding data
for num, o, h, l, c, m in data:
self.series.append(QtChart.QCandlestickSet(o, h, l, c))
self.ma5.append(QtCore.QPointF(num, m))
tm.append(str(num))
x_min = min(x_min, num)
x_max = max(x_max, num)
y_min = min(y_min, m, o, h, l, c)
y_max = max(y_max, m, o, h, l, c)

self.chart.axisY(self.series).setMin(y_min)
self.chart.axisY(self.series).setMax(y_max)
self.chart.axisX(self.series).setCategories(tm)

self.chart.axisX(self.ma5).setMin(x_min)
self.chart.axisX(self.ma5).setMax(x_max)


if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
ex = Example()
ex.show()
sys.exit(app.exec_())

关于python - 更新PYQT5中的图表数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59751779/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com