ฉันกำลังสร้างแอปพลิเคชันเพื่อดูข้อมูลแบบเรียลไทม์ตามตัวอย่างการเลื่อนแปลง แกน x ของฉันควรแสดงเวลาเป็นสตริงที่จัดรูปแบบแล้ว ค่า x ที่เพิ่มลงในพล็อตคือการประทับเวลาลอยในหน่วยวินาที นี่คือโค้ดหน้าต่างการลงจุดเวอร์ชันที่เรียบง่ายของฉัน
ทุกอย่างทำงานแบบเรียลไทม์ และฉันไม่มีปัญหาในการแสดงค่าที่ฉันต้องการพล็อต แต่ป้ายกำกับสำหรับแกน x ของฉันเป็นเพียงการประทับเวลา ไม่ใช่สตริงที่จัดรูปแบบ ฉันรู้ว่าฟังก์ชัน formatTimestampToString(val) ในการโอเวอร์โหลดของ AxisItem ส่งกลับค่าสตริงที่ดี
import pyqtgraph as pg
class NewLegend(pg.LegendItem):
def __init__(self, size=None, offset=None):
pg.LegendItem.__init__(self, size, offset)
def paint(self, p, *args):
p.setPen(pg.mkPen(0,0,0)) # outline
p.setBrush(pg.mkBrush(255,255,255)) # background
p.drawRect(self.boundingRect())
class DateAxis(pg.AxisItem):
def tickStrings(self, values, scale, spacing):
strings = []
for val in values:
strings.append(formatTimestampToString(val))
return strings
class PlotWindow(QtWidgets.QWidget):
def __init__(self, iof, num):
QtWidgets.QWidget.__init__(self)
self.setWindowTitle('Plot Window ')
self.resize(1000, 800)
pg.setConfigOption('background', 'w')
pg.setConfigOption('foreground', 'k')
"""
... other stuff ...
"""
# Externally updated dict with data
self.data = {}
self.curves = {}
self.plotWidget = pg.GraphicsWindow("Graph Window")
axis = DateAxis(orientation='bottom')
self.plot = self.plotWidget.addPlot(axisItem={'bottom': axis})
self.plot.setAutoPan(x=True)
self.legend = NewLegend(size=(100, 60), offset=(70, 30))
self.legend.setParentItem(self.plot.graphicsItem())
def updatePlots(self):
#update data for the different curves
for data_name, curve in self.curves.items():
if curve != 0:
curve.setData(y=self.data[data_name].values[:], x=self.data[data_name].timestamps[:])
def addCurve(self, data_name):
if data_name not in self.curves:
self.curves[data_name] = self.plot.plot(y=self.data[data_name].values[:], x=self.data[data_name].timestamps[:], pen=pg.mkPen(color=self.randomColor(),width=3))
self.legend.addItem(self.curves[data_name], name=data_name)
def removeCurve(self, data_name):
if data_name in self.curves:
self.plot.removeItem(self.curves[data_name])
self.legend.removeItem(data_name)
del self.curves[data_name]
ฉันทำอะไรผิดหรือเปล่า? มีวิธีที่ดีกว่าในการโอเวอร์โหลด AxisItem หรือไม่ ฉันยังพยายามโอเวอร์โหลด AxisItem ด้วยสูตรตัวเลขง่ายๆ เพื่อดูว่ามีผลกระทบต่อแกนของฉันหรือไม่
ปัญหาอีกประการหนึ่งที่ฉันมีคือกับ LegendItem: ฉันสามารถเพิ่มและลบป้ายกำกับได้โดยไม่มีปัญหา แต่จะอัปเดตขนาดของกล่องคำอธิบายแผนภูมิเท่านั้นเมื่อเพิ่มป้ายกำกับ ซึ่งหมายความว่าเมื่อฉันเพิ่มและลบเส้นโค้ง/ข้อมูลในพล็อต คำอธิบายจะขยายใหญ่ขึ้น แต่จะไม่มีวันย่อขนาดลงอีกเลย ฉันได้ลองเรียกใช้ฟังก์ชัน LegendItem.updateSize() หลังจากลบป้ายกำกับออก แต่ไม่มีอะไรเกิดขึ้น
ฉันหวังว่าคุณจะช่วยได้! ขอบคุณ