Example #1
0
class QtChartCanvas(QWidget):
    def __init__(self, parent=None):
        super(QtChartCanvas, self).__init__(parent)
        self.plotChart = QChart()
        self.plotChart.legend().hide()
        self.verticalLayout = QtWidgets.QVBoxLayout(self)

        self.plotView = QChartView(self.plotChart)
        self.verticalLayout.addWidget(self.plotView)
        # self.setCentralWidget(self.plotView)

        self.plotCurve = QSplineSeries()
        self.plotCurve.setUseOpenGL(True)
        self.plotCurve.pen().setColor(Qt.red)
        self.plotChart.addSeries(self.plotCurve)

        self.plotChart.createDefaultAxes()
        # self.plotChart.axisX().setLabelFormat('%d')
        self.plotChart.axisX().hide()

        self.RecvData = []  # 存储接收到的传感器数据
        self.RecvIndx = 0

        self.setLockY = True

        self.isTop = False
        self.minY = 0
        self.maxY = 100

    def update_figure(self, payload):
        self.RecvData.append(payload["data"])
        self.RecvData = self.RecvData[-20:]
        plotData = []
        #print("call")
        if self.isTop:
            for i, val in enumerate(self.RecvData):
                plotData.append(QPoint(i, val))

            self.plotCurve.replace(plotData)
            self.plotChart.axisX().setMax(len(plotData))
            if not self.setLockY:
                self.plotChart.axisY().setRange(min(self.RecvData),
                                                max(self.RecvData))
            else:
                self.plotChart.axisY().setRange(self.minY, self.maxY)

    def setYLimit(self, miny=0, maxy=100, lockY=True):
        self.setLockY = lockY
        if self.setLockY:
            self.maxY = maxy
            self.minY = miny
            # self.plotChart.axisY().setRange(miny,maxy)

    def changeTop(self, isTop):
        self.isTop = isTop
Example #2
0
class CpuLineChart(QChart):

    def __init__(self, *args, **kwargs):
        super(CpuLineChart, self).__init__(*args, **kwargs)
        self.m_count = 10
        # 隐藏图例
        self.legend().hide()
        self.m_series = QSplineSeries(self)
        # 设置画笔
        self.m_series.setPen(QPen(QColor('#3B8CFF'), 2, Qt.SolidLine, Qt.RoundCap, Qt.RoundJoin))
        self.addSeries(self.m_series)
        # x轴
        self.m_axisX = QDateTimeAxis(self)
        self.m_axisX.setTickCount(self.m_count + 1)  # 设置刻度数量
        self.m_axisX.setFormat('hh:mm:ss')  # 设置时间显示格式
        now = QDateTime.currentDateTime()  # 前10秒到现在
        self.m_axisX.setRange(now.addSecs(-self.m_count), now)
        self.addAxis(self.m_axisX, Qt.AlignBottom)
        self.m_series.attachAxis(self.m_axisX)
        # y轴
        self.m_axisY = QValueAxis(self)
        self.m_axisY.setLabelFormat('%d')  # 设置文本格式
        self.m_axisY.setMinorTickCount(4)  # 设置小刻度线的数目
        self.m_axisY.setTickCount(self.m_count + 1)
        self.m_axisY.setRange(0, 100)
        self.addAxis(self.m_axisY, Qt.AlignLeft)
        self.m_series.attachAxis(self.m_axisY)

        # 填充11个初始点,注意x轴 需要转为秒的时间戳
        self.m_series.append(
            [QPointF(now.addSecs(-i).toMSecsSinceEpoch(), 0) for i in range(self.m_count, -1, -1)])

        # 定时器获取数据
        self.m_timer = QTimer()
        self.m_timer.timeout.connect(self.update_data)
        self.m_timer.start(1000)

    def update_data(self):
        value = cpu_percent()
        now = QDateTime.currentDateTime()
        self.m_axisX.setRange(now.addSecs(-self.m_count), now)  # 重新调整x轴的时间范围
        # 获取原来的所有点,去掉第一个并追加新的一个
        points = self.m_series.pointsVector()
        points.pop(0)
        points.append(QPointF(now.toMSecsSinceEpoch(), value))
        # 替换法速度更快
        self.m_series.replace(points)
Example #3
0
class QtChartCanvas(QWidget):
    def __init__(self, parent=None):
        super(QtChartCanvas, self).__init__(parent)
        self.setStyleSheet("border:0;background-color:#263848")
        self.plotChart = QChart()
        self.plotChart.legend().hide()
        self.verticalLayout = QtWidgets.QVBoxLayout(self)

        self.plotView = QChartView(self.plotChart)
        self.plotView.setStyleSheet("border:0;background-color:#263848;")
        self.plotView.setBackgroundBrush(QBrush(QColor("#263848")))
        self.plotChart.setBackgroundBrush(QBrush(QColor("#263848")))

        #self.plotChart.setStyle()
        self.verticalLayout.addWidget(self.plotView)

        self.plotCurve = QSplineSeries()
        self.plotCurve.setColor(QColor("#AABFFF"))
        self.plotCurve.setUseOpenGL(True)
        self.plotCurve.pen().setColor(QColor("#FAF0FF"))
        self.plotChart.addSeries(self.plotCurve)

        # self.scatter=QScatterSeries()
        # self.scatter.setMarkerSize(8)
        # self.plotChart.addSeries(self.scatter)

        self.plotChart.createDefaultAxes()
        self.plotChart.axisY().setGridLineColor(QColor("#5D5C72"))
        self.plotChart.axisY().setLinePenColor(QColor("#9D9CA2"))
        self.plotChart.axisY().setLabelsColor(QColor("#F8F6F6"))
        self.plotChart.axisY().setRange(0, 100)
        self.plotChart.axisX().hide()

        self.RecvData = []  # 存储接收到的传感器数据
        self.RecvIndx = 0

        self.setLockY = True

        self.isTop = False
        self.minY = 0
        self.maxY = 100

    def update_figure(self, payload):
        data = payload["data"]
        self.RecvData.append(data)
        self.RecvData = self.RecvData[-20:]
        plotData = []
        if self.isTop:
            for i, val in enumerate(self.RecvData):
                plotData.append(QPoint(i, val))
            self.plotCurve.replace(plotData)
            #self.scatter.replace(plotData)
            self.plotChart.axisX().setMax(len(plotData))
            if not self.setLockY:
                self.plotChart.axisY().setRange(
                    min(self.RecvData) * 0.5,
                    max(self.RecvData) * 1.3)
            else:
                self.plotChart.axisY().setRange(self.minY, self.maxY)

    def setYLimit(self, miny=0, maxy=100, lockY=True):
        self.setLockY = lockY
        if self.setLockY:
            self.maxY = maxy
            self.minY = miny

    def changeTop(self, isTop):
        self.isTop = isTop