class MainWindow(QMainWindow): def __init__(self, device): super(MainWindow, self).__init__() self.series = QLineSeries() self.chart = QChart() self.chart.addSeries(self.series) self.axisX = QValueAxis() self.axisX.setRange(0, sampleCount) self.axisX.setLabelFormat("%g") self.axisX.setTitleText("Samples") self.axisY = QValueAxis() self.axisY.setRange(-1, 1) self.axisY.setTitleText("Audio level") self.chart.setAxisX(self.axisX, self.series) self.chart.setAxisY(self.axisY, self.series) self.chart.legend().hide() self.chart.setTitle("Data from the microphone ({})".format( device.deviceName())) formatAudio = QAudioFormat() formatAudio.setSampleRate(8000) formatAudio.setChannelCount(1) formatAudio.setSampleSize(8) formatAudio.setCodec("audio/pcm") formatAudio.setByteOrder(QAudioFormat.LittleEndian) formatAudio.setSampleType(QAudioFormat.UnSignedInt) self.audioInput = QAudioInput(device, formatAudio, self) self.ioDevice = self.audioInput.start() self.ioDevice.readyRead.connect(self._readyRead) self.chartView = QChartView(self.chart) self.setCentralWidget(self.chartView) self.buffer = [QPointF(x, 0) for x in range(sampleCount)] self.series.append(self.buffer) def closeEvent(self, event): if self.audioInput is not None: self.audioInput.stop() event.accept() def _readyRead(self): data = self.ioDevice.readAll() availableSamples = data.size() // resolution start = 0 if (availableSamples < sampleCount): start = sampleCount - availableSamples for s in range(start): self.buffer[s].setY(self.buffer[s + availableSamples].y()) dataIndex = 0 for s in range(start, sampleCount): value = (ord(data[dataIndex]) - 128) / 128 self.buffer[s].setY(value) dataIndex = dataIndex + resolution self.series.replace(self.buffer)
def __init__(self, parent, quotes, trades, data_range, currency_name): QWidget.__init__(self, parent) self.setMinimumWidth(600) self.setMinimumHeight(400) self.quotes_series = QLineSeries() for point in quotes: # Conversion to 'float' in order not to get 'int' overflow on some platforms self.quotes_series.append(float(point['timestamp']), point['quote']) self.trade_series = QScatterSeries() for point in trades: # Conversion to 'float' in order not to get 'int' overflow on some platforms self.trade_series.append(float(point['timestamp']), point['price']) self.trade_series.setMarkerSize(5) self.trade_series.setBorderColor(CustomColor.LightRed) self.trade_series.setBrush(CustomColor.DarkRed) axisX = QDateTimeAxis() axisX.setTickCount(11) axisX.setRange(QDateTime().fromSecsSinceEpoch(data_range[0]), QDateTime().fromSecsSinceEpoch(data_range[1])) axisX.setFormat("yyyy/MM/dd") axisX.setLabelsAngle(-90) axisX.setTitleText("Date") axisY = QValueAxis() axisY.setTickCount(11) axisY.setRange(data_range[2], data_range[3]) axisY.setTitleText("Price, " + currency_name) self.chartView = QChartView() self.chartView.chart().addSeries(self.quotes_series) self.chartView.chart().addSeries(self.trade_series) self.chartView.chart().addAxis(axisX, Qt.AlignBottom) self.chartView.chart().setAxisX(axisX, self.quotes_series) self.chartView.chart().setAxisX(axisX, self.trade_series) self.chartView.chart().addAxis(axisY, Qt.AlignLeft) self.chartView.chart().setAxisY(axisY, self.quotes_series) self.chartView.chart().setAxisY(axisY, self.trade_series) self.chartView.chart().legend().hide() self.chartView.setViewportMargins(0, 0, 0, 0) self.chartView.chart().layout().setContentsMargins( 0, 0, 0, 0) # To remove extra spacing around chart self.chartView.chart().setBackgroundRoundness( 0) # To remove corner rounding self.chartView.chart().setMargins(QMargins( 0, 0, 0, 0)) # Allow chart to fill all space self.layout = QHBoxLayout(self) self.layout.setContentsMargins(0, 0, 0, 0) # Remove extra space around layout self.layout.addWidget(self.chartView) self.setLayout(self.layout)
def __init__(self): QMainWindow.__init__(self) low = QBarSet("Min") high = QBarSet("Max") low.append([-52, -50, -45.3, -37.0, -25.6, -8.0, -6.0, -11.8, -19.7, -32.8, -43.0, -48.0]) high.append([11.9, 12.8, 18.5, 26.5, 32.0, 34.8, 38.2, 34.8, 29.8, 20.4, 15.1, 11.8]) series = QStackedBarSeries() series.append(low) series.append(high) chart = QChart() chart.addSeries(series) chart.setTitle("Temperature records in celcius") chart.setAnimationOptions(QChart.SeriesAnimations) categories = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"] axisX = QBarCategoryAxis() axisX.append(categories) axisX.setTitleText("Month") chart.addAxis(axisX, Qt.AlignBottom) axisY = QValueAxis() axisY.setRange(-52, 52) axisY.setTitleText("Temperature [°C]") chart.addAxis(axisY, Qt.AlignLeft) series.attachAxis(axisX) series.attachAxis(axisY) chart.legend().setVisible(True) chart.legend().setAlignment(Qt.AlignBottom) chart_view = QChartView(chart) chart_view.setRenderHint(QPainter.Antialiasing) self.setCentralWidget(chart_view)
class TestChart(QMainWindow): def __init__(self): QMainWindow.__init__(self) self.series = QLineSeries() self.series.append([ QPointF(1.0, 1.0), QPointF(2.0, 73.0), QPointF(3.0, 268.0), QPointF(4.0, 17.0), QPointF(5.0, 4325.0), QPointF(6.0, 723.0) ]) self.chart = QChart() self.chart.addSeries(self.series) self.chart.legend().hide() self.chart.setTitle("Logarithmic axis example") self.axisX = QValueAxis() self.axisX.setTitleText("Data point") self.axisX.setLabelFormat("%i") self.axisX.setTickCount(self.series.count()) self.chart.addAxis(self.axisX, Qt.AlignBottom) self.series.attachAxis(self.axisX) self.axisY = QLogValueAxis() self.axisY.setTitleText("Values") self.axisY.setLabelFormat("%g") self.axisY.setBase(8.0) self.axisY.setMinorTickCount(-1) self.chart.addAxis(self.axisY, Qt.AlignLeft) self.series.attachAxis(self.axisY) self.chartView = QChartView(self.chart) self.chartView.setRenderHint(QPainter.Antialiasing) self.setCentralWidget(self.chartView)