def __init__(self, parent=None): super().__init__(parent) scene = QGraphicsScene(self) self.setScene(scene) self.m_tooltip = None self.m_callouts = [] self.setDragMode(QGraphicsView.NoDrag) self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff) self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff) # chart self.m_chart = QChart() self.m_chart.setMinimumSize(640, 480) self.m_chart.setTitle( "Hover the line to show callout. Click the line to make it stay" ) self.m_chart.legend().hide() series = QLineSeries() series.append(1, 3) series.append(4, 5) series.append(5, 4.5) series.append(7, 1) series.append(11, 2) self.m_chart.addSeries(series) series2 = QSplineSeries() series2.append(1.6, 1.4) series2.append(2.4, 3.5) series2.append(3.7, 2.5) series2.append(7, 4) series2.append(10, 2) self.m_chart.addSeries(series2) self.m_chart.createDefaultAxes() self.m_chart.setAcceptHoverEvents(True) self.setRenderHint(QPainter.Antialiasing) self.scene().addItem(self.m_chart) self.m_coordX = QGraphicsSimpleTextItem(self.m_chart) self.m_coordX.setPos( self.m_chart.size().width() / 2 - 50, self.m_chart.size().height() ) self.m_coordX.setText("X: ") self.m_coordY = QGraphicsSimpleTextItem(self.m_chart) self.m_coordY.setPos( self.m_chart.size().width() / 2 + 50, self.m_chart.size().height() ) self.m_coordY.setText("Y: ") series.clicked.connect(self.keepCallout) series.hovered.connect(self.tooltip) series2.clicked.connect(self.keepCallout) series2.hovered.connect(self.tooltip) self.setMouseTracking(True)
class Widget(QWidget): def __init__(self, device_info: QAudioDeviceInfo, parent: QWidget = None) -> None: super().__init__(parent) self.m_chart = QChart() self.m_series = QLineSeries() chart_view = QChartView(self.m_chart) chart_view.setMinimumSize(800, 600) self.m_chart.addSeries(self.m_series) axisX = QValueAxis() axisX.setRange(0, XYSeriesIODevice.sampleCount) axisX.setLabelFormat("%g") axisX.setTitleText("Samples") axisY = QValueAxis() axisY.setRange(-1, 1) axisY.setTitleText("Audio level") self.m_chart.addAxis(axisX, Qt.AlignBottom) self.m_series.attachAxis(axisX) self.m_chart.addAxis(axisY, Qt.AlignLeft) self.m_series.attachAxis(axisY) self.m_chart.legend().hide() self.m_chart.setTitle( f"Data from the microphone ({device_info.deviceName()})") mainLayout = QVBoxLayout(self) mainLayout.addWidget(chart_view) formatAudio = QAudioFormat() formatAudio.setSampleRate(8000) formatAudio.setChannelCount(1) formatAudio.setSampleSize(8) formatAudio.setCodec("audio/pcm") formatAudio.setByteOrder(QAudioFormat.LittleEndian) formatAudio.setSampleType(QAudioFormat.UnSignedInt) self.m_audioInput = QAudioInput(device_info, formatAudio, self) self.m_device = XYSeriesIODevice(self.m_series, self) self.m_device.open(QIODevice.WriteOnly) self.m_audioInput.start(self.m_device)
def main(): import sys app = QApplication(sys.argv) series0 = QLineSeries() series1 = QLineSeries() series0 << QPointF(1, 5) << QPointF(3, 7) << QPointF(7, 6) << QPointF( 9, 7) << QPointF(12, 6) << QPointF(16, 7) << QPointF(18, 5) series1 << QPointF(1, 3) << QPointF(3, 4) << QPointF(7, 3) << QPointF( 8, 2) << QPointF(12, 3) << QPointF(16, 4) << QPointF(18, 3) series = QAreaSeries(series0, series1) series.setName("Batman") pen = QPen(0x059605) pen.setWidth(3) series.setPen(pen) gradient = QLinearGradient(QPointF(0, 0), QPointF(0, 1)) gradient.setColorAt(0.0, QColor(0x3CC63C)) gradient.setColorAt(1.0, QColor(0x26F626)) gradient.setCoordinateMode(QGradient.ObjectBoundingMode) series.setBrush(gradient) chart = QChart() chart.addSeries(series) chart.setTitle("Simple areachart example") chart.createDefaultAxes() chart.axes(Qt.Horizontal)[0].setRange(0, 20) chart.axes(Qt.Vertical)[0].setRange(0, 10) chartView = QChartView(chart) chartView.setRenderHint(QPainter.Antialiasing) window = QMainWindow() window.setCentralWidget(chartView) window.resize(400, 300) window.show() sys.exit(app.exec_())