34 lines
1.1 KiB
Python
34 lines
1.1 KiB
Python
|
|
# CustomPlotItem.py
|
|||
|
|
from PyQt6 import *
|
|||
|
|
from PyQt6.QtCore import *
|
|||
|
|
from PyQt6.QtQuick import QQuickPaintedItem
|
|||
|
|
from PyQt6.QtGui import QPainter
|
|||
|
|
from QCustomPlot_PyQt6 import QCustomPlot, QCPGraph
|
|||
|
|
class CustomPlotItem(QQuickPaintedItem):
|
|||
|
|
def __init__(self, parent=None):
|
|||
|
|
super().__init__(parent)
|
|||
|
|
self.customPlot = QCustomPlot()
|
|||
|
|
|
|||
|
|
# 示例:添加一个简单的图表
|
|||
|
|
self.graph0 = self.customPlot.addGraph()
|
|||
|
|
|
|||
|
|
self.customPlot.xAxis.setLabel("X")
|
|||
|
|
self.customPlot.yAxis.setLabel("Y")
|
|||
|
|
|
|||
|
|
self.customPlot.xAxis.setRange(0, 5000)
|
|||
|
|
self.customPlot.yAxis.setRange(0, 30)
|
|||
|
|
|
|||
|
|
|
|||
|
|
def paint(self, painter: QPainter):
|
|||
|
|
self.customPlot.setGeometry(0, 0, int(self.width()), int(self.height()))
|
|||
|
|
# 将 QCustomPlot 的内容绘制到 QQuickPaintedItem 上
|
|||
|
|
painter.drawPixmap( 0, 0, int(self.width()),int(self.height()), self.customPlot.toPixmap())
|
|||
|
|
|
|||
|
|
@pyqtSlot(list, list)
|
|||
|
|
def setGraphData(self, keys, values):
|
|||
|
|
# 设置图表数据
|
|||
|
|
|
|||
|
|
self.graph0.setData(keys, values)
|
|||
|
|
self.customPlot.replot()
|
|||
|
|
self.update(QRect(0, 0, int(self.width()), int(self.height())))
|