class NewGraph(): def __init__(self): self.curves = [] self.titleFont = QFont("Helvetica", 12, QFont.Bold) self.axisFont = QFont("Helvetica", 11, QFont.Bold) def createGraph(self): self.plot = QwtPlot() self.plot.resize(1000, 1000) self.plot.setAxisScaleDraw(QwtPlot.xBottom, DateTimeTimeScaleDraw(names=False)) self.plot.replot() self.plot.show() def createCurve(self, x, y, colour): curve = QwtPlotCurve() colour = QColor(colour) curve.setPen(colour) curve.setData(x, y) curve.attach(self.plot) #self.plot.replot() self.curves.append(curve) def removeCurve(self, curveIndex): self.curves[curveIndex].attach(0) del self.curves[curveIndex] def setAxisTitles(self, yAxis, xAxis): # Create text for graph and axis titles xTitle = QwtText() xTitle.setText(xAxis) xTitle.setFont(self.axisFont) yTitle = QwtText() yTitle.setText(yAxis) yTitle.setFont(self.axisFont) self.plot.setAxisTitle(self.plot.yLeft, yTitle) self.plot.setAxisTitle(self.plot.xBottom, xTitle) def setTitle(self, title): titleText = QwtText() titleText.setText(title) titleText.setFont(self.titleFont) self.plot.setTitle(titleText)
def main(args): app = QApplication(args) demo = QwtPlot() grid = QwtPlotGrid() grid.attach(demo) grid.setPen(QPen(Qt.black, 0, Qt.DotLine)) grid.enableX(True) grid.enableY(True) complex_divider = 50.0 myXScale = ComplexScaleDraw(start_value=0.0, end_value=complex_divider) #print('myXScale', myXScale) demo.setAxisScaleDraw(QwtPlot.xBottom, myXScale) m = QwtPlotMarker() m.attach(demo) m.setValue(complex_divider, 0.0) m.setLineStyle(QwtPlotMarker.VLine) m.setLabelAlignment(Qt.AlignRight | Qt.AlignBottom) m.setLinePen(QPen(Qt.black, 2, Qt.SolidLine)) vector_array = numpy.zeros((100, ), numpy.float32) for i in range(100): vector_array[i] = i curve = QwtPlotCurve('example data') curve.attach(demo) x_array = numpy.zeros(100, numpy.float32) y_array = numpy.zeros(100, numpy.float32) for i in range(100): x_array[i] = 1.0 * i y_array[i] = 2.0 * i curve.setSamples(x_array, y_array) demo.resize(600, 400) demo.replot() demo.show() # app.setMainWidget(demo) app.exec_()
class FermentGraph(QWidget): _logname = 'FermentGraphGeneric' _log = logging.getLogger(f'{_logname}') def __init__(self, database, parent=None): super().__init__(parent) self.db = database self.updateTimer = QTimer(self) self.updateTimer.start(5000) self.plot = QwtPlot() self.curve = QwtPlotCurve() self.curve.attach(self.plot) self.plot.resize(1000, 1000) self.plot.show() self.plot.setAxisScaleDraw(QwtPlot.xBottom, DateTimeTimeScaleDraw()) axisFont = QFont("Helvetica", 11, QFont.Bold) titleFont = QFont("Helvetica", 12, QFont.Bold) xTitle = QwtText() xTitle.setText("Time") xTitle.setFont(axisFont) self.plot.setAxisTitle(self.plot.xBottom, xTitle) self.yTitle = QwtText() self.yTitle.setFont(axisFont) self.plot.setAxisTitle(self.plot.yLeft, self.yTitle) self.titleText = QwtText() self.titleText.setFont(titleFont) self.plot.setTitle(self.titleText) mainLayout = QHBoxLayout() mainLayout.addWidget(self.plot) self.setLayout(mainLayout) self.plot.show() self.results = [] self.batchID = None def updatePlot(self, variable): if self.batchID is not None: self.db.flushTables() sql = f"SELECT TimeStamp, {variable} FROM Ferment WHERE BatchID = '{self.batchID}'" timestamps = [] self.results = [] for data in self.db.custom(sql)[1:]: timestamps.append(data[0]) self.results.append(data[1]) startTime = timestamps[0] for i in range(len(timestamps)): timestamps[i] = (timestamps[i] - startTime).seconds # self.plot.setAxisScaleDraw(QwtPlot.xBottom, TimeScaleDraw()) self.curve.setData(timestamps, self.results) self.plot.replot() self.plot.show() def changeTank(self, tankID): self.titleText.setText(f"Fermentation Tank: {tankID}")