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)
class View(QGraphicsView): 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) def resizeEvent(self, event): if self.scene() is not None: self.scene().setSceneRect(QRectF(QRect(QPoint(0, 0), event.size()))) self.m_chart.resize(QSizeF(event.size())) self.m_coordX.setPos( self.m_chart.size().width() / 2 - 50, self.m_chart.size().height() - 20 ) self.m_coordY.setPos( self.m_chart.size().width() / 2 + 50, self.m_chart.size().height() - 20 ) for callout in self.m_callouts: callout.updateGeometry() super().resizeEvent(event) def mouseMoveEvent(self, event): self.m_coordX.setText("X: %f" % self.m_chart.mapToValue(event.pos()).x()) self.m_coordY.setText("Y: %f" % self.m_chart.mapToValue(event.pos()).y()) super().mouseMoveEvent(event) def keepCallout(self): self.m_callouts.append(self.m_tooltip) self.m_tooltip = Callout(self.m_chart) def tooltip(self, point, state): if self.m_tooltip is None: self.m_tooltip = Callout(self.m_chart) if state: self.m_tooltip.setText("X: %f \nY: %f " % (point.x(), point.y())) self.m_tooltip.setAnchor(point) self.m_tooltip.setZValue(11) self.m_tooltip.updateGeometry() self.m_tooltip.show() else: self.m_tooltip.hide()
def main(): import sys app = QApplication(sys.argv) acmeSeries = QBoxPlotSeries() acmeSeries.setName("Acme Ltd") boxWhiskSeries = QBoxPlotSeries() boxWhiskSeries.setName("BoxWhisk Inc") acmeData = QFile(":acme") if not acmeData.open(QIODevice.ReadOnly | QIODevice.Text): sys.exit(1) dataReader = BoxDataReader(acmeData) while not dataReader.atEnd(): _set = dataReader.readBox() if _set is not None: acmeSeries.append(_set) boxwhiskData = QFile(":boxwhisk") if not boxwhiskData.open(QIODevice.ReadOnly | QIODevice.Text): sys.exit(1) dataReader.readFile(boxwhiskData) while not dataReader.atEnd(): _set = dataReader.readBox() if _set is not None: boxWhiskSeries.append(_set) chart = QChart() chart.addSeries(acmeSeries) chart.addSeries(boxWhiskSeries) chart.setTitle("Acme Ltd and BoxWhisk Inc share deviation in 2012") chart.setAnimationOptions(QChart.SeriesAnimations) chart.createDefaultAxes() chart.axes(Qt.Vertical)[0].setMin(15.0) chart.axes(Qt.Vertical)[0].setMax(34.0) chart.legend().setVisible(True) chart.legend().setAlignment(Qt.AlignBottom) chartView = QChartView(chart) chartView.setRenderHint(QPainter.Antialiasing) window = QMainWindow() window.setCentralWidget(chartView) window.resize(800, 600) window.show() sys.exit(app.exec_())
def main(): import sys a = QApplication(sys.argv) acmeSeries = QCandlestickSeries() acmeSeries.setName("Acme Ltd") acmeSeries.setIncreasingColor(QColor(Qt.green)) acmeSeries.setDecreasingColor(QColor(Qt.red)) acmeData = QFile(":acme") if not acmeData.open(QIODevice.ReadOnly | QIODevice.Text): sys.exit(1) categories = [] dataReader = CandlestickDataReader(acmeData) while not dataReader.atEnd(): _set = dataReader.readCandlestickSet() if _set is not None: acmeSeries.append(_set) categories.append( QDateTime.fromMSecsSinceEpoch(int( _set.timestamp())).toString("dd")) chart = QChart() chart.addSeries(acmeSeries) chart.setTitle("Acme Ltd Historical Data (July 2015)") chart.setAnimationOptions(QChart.SeriesAnimations) chart.createDefaultAxes() axisX = chart.axes(Qt.Horizontal)[0] axisX.setCategories(categories) axisY = chart.axes(Qt.Vertical)[0] axisY.setMax(axisY.max() * 1.01) axisY.setMin(axisY.min() * 0.99) chart.legend().setVisible(True) chart.legend().setAlignment(Qt.AlignBottom) chartView = QChartView(chart) chartView.setRenderHint(QPainter.Antialiasing) window = QMainWindow() window.setCentralWidget(chartView) window.resize(800, 600) window.show() sys.exit(a.exec_())
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_())
def main(): import sys app = QApplication(sys.argv) set0 = QBarSet("Jane") set1 = QBarSet("John") set2 = QBarSet("Axel") set3 = QBarSet("Mary") set4 = QBarSet("Samantha") set0 << 1 << 2 << 3 << 4 << 5 << 6 set1 << 5 << 0 << 0 << 4 << 0 << 7 set2 << 3 << 5 << 8 << 13 << 8 << 5 set3 << 5 << 6 << 7 << 3 << 4 << 5 set4 << 9 << 7 << 5 << 3 << 1 << 2 series = QBarSeries() series.append(set0) series.append(set1) series.append(set2) series.append(set3) series.append(set4) chart = QChart() chart.addSeries(series) chart.setTitle("Simple barchart example") chart.setAnimationOptions(QChart.SeriesAnimations) categories = ("Jan", "Feb", "Mar", "Apr", "May", "Jun") axisX = QBarCategoryAxis() axisX.append(categories) chart.addAxis(axisX, Qt.AlignBottom) series.attachAxis(axisX) axisY = QValueAxis() axisY.setRange(0, 15) chart.addAxis(axisY, Qt.AlignLeft) series.attachAxis(axisY) chart.legend().setVisible(True) chart.legend().setAlignment(Qt.AlignBottom) chartView = QChartView(chart) chartView.setRenderHint(QPainter.Antialiasing) window = QMainWindow() window.setCentralWidget(chartView) window.resize(420, 300) window.show() sys.exit(app.exec_())