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)
class ChartWidget(QWidget): 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)
class TestChart(QMainWindow): def __init__(self): QMainWindow.__init__(self) self.set0 = QBarSet("Jane") self.set1 = QBarSet("John") self.set2 = QBarSet("Axel") self.set3 = QBarSet("Mary") self.set4 = QBarSet("Sam") self.set0.append([1, 2, 3, 4, 5, 6]) self.set1.append([5, 0, 0, 4, 0, 7]) self.set2.append([3, 5, 8, 13, 8, 5]) self.set3.append([5, 6, 7, 3, 4, 5]) self.set4.append([9, 7, 5, 3, 1, 2]) self.barSeries = QBarSeries() self.barSeries.append(self.set0) self.barSeries.append(self.set1) self.barSeries.append(self.set2) self.barSeries.append(self.set3) self.barSeries.append(self.set4) self.lineSeries = QLineSeries() self.lineSeries.setName("trend") self.lineSeries.append(QPoint(0, 4)) self.lineSeries.append(QPoint(1, 15)) self.lineSeries.append(QPoint(2, 20)) self.lineSeries.append(QPoint(3, 4)) self.lineSeries.append(QPoint(4, 12)) self.lineSeries.append(QPoint(5, 17)) self.chart = QChart() self.chart.addSeries(self.barSeries) self.chart.addSeries(self.lineSeries) self.chart.setTitle("Line and barchart example") self.categories = ["Jan", "Feb", "Mar", "Apr", "May", "Jun"] self.axisX = QBarCategoryAxis() self.axisX.append(self.categories) self.chart.setAxisX(self.axisX, self.lineSeries) self.chart.setAxisX(self.axisX, self.barSeries) self.axisX.setRange("Jan", "Jun") self.axisY = QValueAxis() self.chart.setAxisY(self.axisY, self.lineSeries) self.chart.setAxisY(self.axisY, self.barSeries) self.axisY.setRange(0, 20) self.chart.legend().setVisible(True) self.chart.legend().setAlignment(Qt.AlignBottom) self.chartView = QChartView(self.chart) self.chartView.setRenderHint(QPainter.Antialiasing) self.setCentralWidget(self.chartView)
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)
class View(QGraphicsView): def __init__(self, parent=None): super(View, self).__init__(parent) self.setScene(QGraphicsScene(self)) self.setDragMode(QGraphicsView.NoDrag) self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff) self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff) # Chart self._chart = QChart() self._chart.setMinimumSize(640, 480) self._chart.setTitle("Hover the line to show callout. Click the line " "to make it stay") self._chart.legend().hide() self.series = QLineSeries() self.series.append(1, 3) self.series.append(4, 5) self.series.append(5, 4.5) self.series.append(7, 1) self.series.append(11, 2) self._chart.addSeries(self.series) self.series2 = QSplineSeries() self.series2.append(1.6, 1.4) self.series2.append(2.4, 3.5) self.series2.append(3.7, 2.5) self.series2.append(7, 4) self.series2.append(10, 2) self._chart.addSeries(self.series2) self._chart.createDefaultAxes() self._chart.setAcceptHoverEvents(True) self.setRenderHint(QPainter.Antialiasing) self.scene().addItem(self._chart) self._coordX = QGraphicsSimpleTextItem(self._chart) self._coordX.setPos(self._chart.size().width() / 2 - 50, self._chart.size().height()) self._coordX.setText("X: ") self._coordY = QGraphicsSimpleTextItem(self._chart) self._coordY.setPos(self._chart.size().width() / 2 + 50, self._chart.size().height()) self._coordY.setText("Y: ") self._callouts = [] self._tooltip = Callout(self._chart) self.series.clicked.connect(self.keepCallout) self.series.hovered.connect(self.tooltip) self.series2.clicked.connect(self.keepCallout) self.series2.hovered.connect(self.tooltip) self.setMouseTracking(True) def resizeEvent(self, event): if self.scene(): self.scene().setSceneRect(QRectF(QPointF(0, 0), event.size())) self._chart.resize(event.size()) self._coordX.setPos(self._chart.size().width() / 2 - 50, self._chart.size().height() - 20) self._coordY.setPos(self._chart.size().width() / 2 + 50, self._chart.size().height() - 20) for callout in self._callouts: callout.updateGeometry() QGraphicsView.resizeEvent(self, event) def mouseMoveEvent(self, event): pos = self._chart.mapToValue(event.position().toPoint()) x = pos.x() y = pos.y() self._coordX.setText(f"X: {x:.2f}") self._coordY.setText(f"Y: {y:.2f}") QGraphicsView.mouseMoveEvent(self, event) def keepCallout(self): self._callouts.append(self._tooltip) self._tooltip = Callout(self._chart) def tooltip(self, point, state): if self._tooltip == 0: self._tooltip = Callout(self._chart) if state: self._tooltip.setText("X: {0:.2f} \nY: {1:.2f} ".format( point.x(), point.y())) self._tooltip.setAnchor(point) self._tooltip.setZValue(11) self._tooltip.updateGeometry() self._tooltip.show() else: self._tooltip.hide()
class TestChart(QMainWindow): def __init__(self): QMainWindow.__init__(self) self.series = QLineSeries() self.series.append(0, 6) self.series.append(2, 4) self.series.append(3, 8) self.series.append(7, 4) self.series.append(10, 5) self.series.append(QPointF(11, 1)) self.series.append(QPointF(13, 3)) self.series.append(QPointF(17, 6)) self.series.append(QPointF(18, 3)) self.series.append(QPointF(20, 2)) self.chart = QChart() self.chart.legend().hide() self.chart.addSeries(self.series) self.chart.createDefaultAxes() self.chart.setTitle("Simple line chart example") self.chartView = QChartView(self.chart) self.chartView.setRenderHint(QPainter.Antialiasing) self.setCentralWidget(self.chartView)