diff options
Diffstat (limited to 'examples/charts')
33 files changed, 1623 insertions, 11 deletions
diff --git a/examples/charts/audio.py b/examples/charts/audio.py index 6d7b4895a..8a4b148af 100644 --- a/examples/charts/audio.py +++ b/examples/charts/audio.py @@ -1,4 +1,3 @@ - ############################################################################# ## ## Copyright (C) 2018 The Qt Company Ltd. diff --git a/examples/charts/audio/audio.py b/examples/charts/audio/audio.py new file mode 100644 index 000000000..969f2b9ab --- /dev/null +++ b/examples/charts/audio/audio.py @@ -0,0 +1,126 @@ + +############################################################################# +## +## Copyright (C) 2021 The Qt Company Ltd. +## Contact: http://www.qt.io/licensing/ +## +## This file is part of the Qt for Python examples of the Qt Toolkit. +## +## $QT_BEGIN_LICENSE:BSD$ +## You may use this file under the terms of the BSD license as follows: +## +## "Redistribution and use in source and binary forms, with or without +## modification, are permitted provided that the following conditions are +## met: +## * Redistributions of source code must retain the above copyright +## notice, this list of conditions and the following disclaimer. +## * Redistributions in binary form must reproduce the above copyright +## notice, this list of conditions and the following disclaimer in +## the documentation and/or other materials provided with the +## distribution. +## * Neither the name of The Qt Company Ltd nor the names of its +## contributors may be used to endorse or promote products derived +## from this software without specific prior written permission. +## +## +## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +## "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +## LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +## A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +## OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +## THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +## +## $QT_END_LICENSE$ +## +############################################################################# + +"""PySide6 port of the charts/audio example from Qt v5.x""" + +import sys +from PySide6.QtCharts import QChart, QChartView, QLineSeries, QValueAxis +from PySide6.QtCore import QPointF +from PySide6.QtMultimedia import (QAudioDeviceInfo, QAudioFormat, + QAudioInput) +from PySide6.QtWidgets import QApplication, QMainWindow, QMessageBox + +sampleCount = 2000 +resolution = 4 + +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) + +if __name__ == '__main__': + app = QApplication(sys.argv) + + inputDevice = QAudioDeviceInfo.defaultInputDevice() + if (inputDevice.isNull()): + QMessageBox.warning(None, "audio", "There is no audio input device available.") + sys.exit(-1) + + mainWin = MainWindow(inputDevice) + mainWin.setWindowTitle("audio") + availableGeometry = app.desktop().availableGeometry(mainWin) + size = availableGeometry.height() * 3 / 4 + mainWin.resize(size, size) + mainWin.show() + sys.exit(app.exec_()) diff --git a/examples/charts/audio/audio.pyproject b/examples/charts/audio/audio.pyproject new file mode 100644 index 000000000..00d15e1ab --- /dev/null +++ b/examples/charts/audio/audio.pyproject @@ -0,0 +1,3 @@ +{ + "files": ["audio.py"] +} diff --git a/examples/charts/callout.py b/examples/charts/callout/callout.py index 666b2aae5..666b2aae5 100644 --- a/examples/charts/callout.py +++ b/examples/charts/callout/callout.py diff --git a/examples/charts/callout/callout.pyproject b/examples/charts/callout/callout.pyproject new file mode 100644 index 000000000..1360c97b4 --- /dev/null +++ b/examples/charts/callout/callout.pyproject @@ -0,0 +1,3 @@ +{ + "files": ["callout.py"] +} diff --git a/examples/charts/charts.pyproject b/examples/charts/charts.pyproject deleted file mode 100644 index 15a48a3a1..000000000 --- a/examples/charts/charts.pyproject +++ /dev/null @@ -1,5 +0,0 @@ -{ - "files": ["percentbarchart.py", "donutbreakdown.py", "legend.py", "nesteddonuts.py", - "modeldata.py", "lineandbar.py", "memoryusage.py", "callout.py", "audio.py", - "linechart.py", "logvalueaxis.py", "piechart.py", "temperaturerecords.py"] -} diff --git a/examples/charts/donutbreakdown/donutbreakdown.py b/examples/charts/donutbreakdown/donutbreakdown.py new file mode 100644 index 000000000..4c319c84f --- /dev/null +++ b/examples/charts/donutbreakdown/donutbreakdown.py @@ -0,0 +1,182 @@ +############################################################################# +## +## Copyright (C) 2021 The Qt Company Ltd. +## Contact: http://www.qt.io/licensing/ +## +## This file is part of the Qt for Python examples of the Qt Toolkit. +## +## $QT_BEGIN_LICENSE:BSD$ +## You may use this file under the terms of the BSD license as follows: +## +## "Redistribution and use in source and binary forms, with or without +## modification, are permitted provided that the following conditions are +## met: +## * Redistributions of source code must retain the above copyright +## notice, this list of conditions and the following disclaimer. +## * Redistributions in binary form must reproduce the above copyright +## notice, this list of conditions and the following disclaimer in +## the documentation and/or other materials provided with the +## distribution. +## * Neither the name of The Qt Company Ltd nor the names of its +## contributors may be used to endorse or promote products derived +## from this software without specific prior written permission. +## +## +## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +## "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +## LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +## A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +## OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +## THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +## +## $QT_END_LICENSE$ +## +############################################################################# + +"""PySide6 port of the Donut Chart Breakdown example from Qt v5.x""" + + +import sys +from PySide6.QtCore import Qt +from PySide6.QtGui import QColor, QFont, QPainter, QScreen +from PySide6.QtWidgets import QApplication, QMainWindow +from PySide6.QtCharts import QChart, QChartView, QPieSeries, QPieSlice + +class MainSlice(QPieSlice): + def __init__(self, breakdown_series, parent=None): + super(MainSlice, self).__init__(parent) + + self.breakdown_series = breakdown_series + self.name = None + + self.percentageChanged.connect(self.update_label) + + def get_breakdown_series(self): + return self.breakdown_series + + def setName(self, name): + self.name = name + + def name(self): + return self.name + + def update_label(self): + self.setLabel("{} {:.2f}%".format(self.name, + self.percentage() * 100)) + + +class DonutBreakdownChart(QChart): + def __init__(self, parent=None): + super(DonutBreakdownChart, self).__init__(QChart.ChartTypeCartesian, + parent, Qt.WindowFlags()) + self.main_series = QPieSeries() + self.main_series.setPieSize(0.7) + self.addSeries(self.main_series) + + def add_breakdown_series(self, breakdown_series, color): + font = QFont("Arial", 8) + + # add breakdown series as a slice to center pie + main_slice = MainSlice(breakdown_series) + main_slice.setName(breakdown_series.name()) + main_slice.setValue(breakdown_series.sum()) + self.main_series.append(main_slice) + + # customize the slice + main_slice.setBrush(color) + main_slice.setLabelVisible() + main_slice.setLabelColor(Qt.white) + main_slice.setLabelPosition(QPieSlice.LabelInsideHorizontal) + main_slice.setLabelFont(font) + + # position and customize the breakdown series + breakdown_series.setPieSize(0.8) + breakdown_series.setHoleSize(0.7) + breakdown_series.setLabelsVisible() + + for pie_slice in breakdown_series.slices(): + color = QColor(color).lighter(115) + pie_slice.setBrush(color) + pie_slice.setLabelFont(font) + + # add the series to the chart + self.addSeries(breakdown_series) + + # recalculate breakdown donut segments + self.recalculate_angles() + + # update customize legend markers + self.update_legend_markers() + + def recalculate_angles(self): + angle = 0 + slices = self.main_series.slices() + for pie_slice in slices: + breakdown_series = pie_slice.get_breakdown_series() + breakdown_series.setPieStartAngle(angle) + angle += pie_slice.percentage() * 360.0 # full pie is 360.0 + breakdown_series.setPieEndAngle(angle) + + def update_legend_markers(self): + # go through all markers + for series in self.series(): + markers = self.legend().markers(series) + for marker in markers: + if series == self.main_series: + # hide markers from main series + marker.setVisible(False) + else: + # modify markers from breakdown series + marker.setLabel("{} {:.2f}%".format( + marker.slice().label(), + marker.slice().percentage() * 100, 0)) + marker.setFont(QFont("Arial", 8)) + +if __name__ == "__main__": + app = QApplication(sys.argv) + # Graph is based on data of: + # 'Total consumption of energy increased by 10 per cent in 2010' + # Statistics Finland, 13 December 2011 + # http://www.stat.fi/til/ekul/2010/ekul_2010_2011-12-13_tie_001_en.html + series1 = QPieSeries() + series1.setName("Fossil fuels") + series1.append("Oil", 353295) + series1.append("Coal", 188500) + series1.append("Natural gas", 148680) + series1.append("Peat", 94545) + + series2 = QPieSeries() + series2.setName("Renewables") + series2.append("Wood fuels", 319663) + series2.append("Hydro power", 45875) + series2.append("Wind power", 1060) + + series3 = QPieSeries() + series3.setName("Others") + series3.append("Nuclear energy", 238789) + series3.append("Import energy", 37802) + series3.append("Other", 32441) + + donut_breakdown = DonutBreakdownChart() + donut_breakdown.setAnimationOptions(QChart.AllAnimations) + donut_breakdown.setTitle("Total consumption of energy in Finland 2010") + donut_breakdown.legend().setAlignment(Qt.AlignRight) + donut_breakdown.add_breakdown_series(series1, Qt.red) + donut_breakdown.add_breakdown_series(series2, Qt.darkGreen) + donut_breakdown.add_breakdown_series(series3, Qt.darkBlue) + + window = QMainWindow() + chart_view = QChartView(donut_breakdown) + chart_view.setRenderHint(QPainter.Antialiasing) + window.setCentralWidget(chart_view) + available_geometry = window.screen().availableGeometry() + size = available_geometry.height() * 0.75 + window.resize(size, size * 0.8) + window.show() + + sys.exit(app.exec_()) diff --git a/examples/charts/donutbreakdown/donutbreakdown.pyproject b/examples/charts/donutbreakdown/donutbreakdown.pyproject new file mode 100644 index 000000000..6225792da --- /dev/null +++ b/examples/charts/donutbreakdown/donutbreakdown.pyproject @@ -0,0 +1,3 @@ +{ + "files": ["donutbreakdown.py"] +} diff --git a/examples/charts/legend/legend.py b/examples/charts/legend/legend.py new file mode 100644 index 000000000..71fd94885 --- /dev/null +++ b/examples/charts/legend/legend.py @@ -0,0 +1,252 @@ +############################################################################# +## +## Copyright (C) 2021 The Qt Company Ltd. +## Contact: http://www.qt.io/licensing/ +## +## This file is part of the Qt for Python examples of the Qt Toolkit. +## +## $QT_BEGIN_LICENSE:BSD$ +## You may use this file under the terms of the BSD license as follows: +## +## "Redistribution and use in source and binary forms, with or without +## modification, are permitted provided that the following conditions are +## met: +## * Redistributions of source code must retain the above copyright +## notice, this list of conditions and the following disclaimer. +## * Redistributions in binary form must reproduce the above copyright +## notice, this list of conditions and the following disclaimer in +## the documentation and/or other materials provided with the +## distribution. +## * Neither the name of The Qt Company Ltd nor the names of its +## contributors may be used to endorse or promote products derived +## from this software without specific prior written permission. +## +## +## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +## "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +## LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +## A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +## OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +## THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +## +## $QT_END_LICENSE$ +## +############################################################################# + +"""PySide6 port of the Legend example from Qt v5.x""" + +import sys +from PySide6.QtCore import Qt, QRectF +from PySide6.QtGui import QBrush, QColor, QPainter, QPen +from PySide6.QtWidgets import (QApplication, QDoubleSpinBox, + QFormLayout, QGridLayout, QGroupBox, QPushButton, QWidget) +from PySide6.QtCharts import QBarSeries, QBarSet, QChart, QChartView + +class MainWidget(QWidget): + def __init__(self, parent=None): + super(MainWidget, self).__init__(parent) + self.chart = QChart() + self.series = QBarSeries() + + self.main_layout = QGridLayout() + self.button_layout = QGridLayout() + self.font_layout = QFormLayout() + + self.font_size = QDoubleSpinBox() + + self.legend_posx = QDoubleSpinBox() + self.legend_posy = QDoubleSpinBox() + self.legend_width = QDoubleSpinBox() + self.legend_height = QDoubleSpinBox() + + self.detach_legend_button = QPushButton("Toggle attached") + self.detach_legend_button.clicked.connect(self.toggle_attached) + self.button_layout.addWidget(self.detach_legend_button, 0, 0) + + self.add_set_button = QPushButton("add barset") + self.add_set_button.clicked.connect(self.add_barset) + self.button_layout.addWidget(self.add_set_button, 2, 0) + + self.remove_barset_button = QPushButton("remove barset") + self.remove_barset_button.clicked.connect(self.remove_barset) + self.button_layout.addWidget(self.remove_barset_button, 3, 0) + + self.align_button = QPushButton("Align (Bottom)") + self.align_button.clicked.connect(self.set_legend_alignment) + self.button_layout.addWidget(self.align_button, 4, 0) + + self.bold_button = QPushButton("Toggle bold") + self.bold_button.clicked.connect(self.toggle_bold) + self.button_layout.addWidget(self.bold_button, 8, 0) + + self.italic_button = QPushButton("Toggle italic") + self.italic_button.clicked.connect(self.toggle_italic) + self.button_layout.addWidget(self.italic_button, 9, 0) + + self.legend_posx.valueChanged.connect(self.update_legend_layout) + self.legend_posy.valueChanged.connect(self.update_legend_layout) + self.legend_width.valueChanged.connect(self.update_legend_layout) + self.legend_height.valueChanged.connect(self.update_legend_layout) + + legend_layout = QFormLayout() + legend_layout.addRow("HPos", self.legend_posx) + legend_layout.addRow("VPos", self.legend_posy) + legend_layout.addRow("Width", self.legend_width) + legend_layout.addRow("Height", self.legend_height) + + self.legend_settings = QGroupBox("Detached legend") + self.legend_settings.setLayout(legend_layout) + self.button_layout.addWidget(self.legend_settings) + self.legend_settings.setVisible(False) + + # Create chart view with the chart + self.chart_view = QChartView(self.chart, self) + + # Create spinbox to modify font size + self.font_size.setValue(self.chart.legend().font().pointSizeF()) + self.font_size.valueChanged.connect(self.font_size_changed) + + self.font_layout.addRow("Legend font size", self.font_size) + + # Create layout for grid and detached legend + self.main_layout.addLayout(self.button_layout, 0, 0) + self.main_layout.addLayout(self.font_layout, 1, 0) + self.main_layout.addWidget(self.chart_view, 0, 1, 3, 1) + self.setLayout(self.main_layout) + + self.create_series() + + def create_series(self): + self.add_barset() + self.add_barset() + self.add_barset() + self.add_barset() + + self.chart.addSeries(self.series) + self.chart.setTitle("Legend detach example") + self.chart.createDefaultAxes() + + self.chart.legend().setVisible(True) + self.chart.legend().setAlignment(Qt.AlignBottom) + + self.chart_view.setRenderHint(QPainter.Antialiasing) + + def show_legend_spinbox(self): + self.legend_settings.setVisible(True) + chart_viewrect = self.chart_view.rect() + + self.legend_posx.setMinimum(0) + self.legend_posx.setMaximum(chart_viewrect.width()) + self.legend_posx.setValue(150) + + self.legend_posy.setMinimum(0) + self.legend_posy.setMaximum(chart_viewrect.height()) + self.legend_posy.setValue(150) + + self.legend_width.setMinimum(0) + self.legend_width.setMaximum(chart_viewrect.width()) + self.legend_width.setValue(150) + + self.legend_height.setMinimum(0) + self.legend_height.setMaximum(chart_viewrect.height()) + self.legend_height.setValue(75) + + def hideLegendSpinbox(self): + self.legend_settings.setVisible(False) + + def toggle_attached(self): + legend = self.chart.legend() + if legend.isAttachedToChart(): + legend.detachFromChart() + legend.setBackgroundVisible(True) + legend.setBrush(QBrush(QColor(128, 128, 128, 128))) + legend.setPen(QPen(QColor(192, 192, 192, 192))) + + self.show_legend_spinbox() + self.update_legend_layout() + else: + legend.attachToChart() + legend.setBackgroundVisible(False) + self.hideLegendSpinbox() + self.update() + + def add_barset(self): + series_count = self.series.count() + bar_set = QBarSet(f"set {series_count}") + delta = series_count * 0.1 + bar_set.append([1 + delta, 2 + delta, 3 + delta, 4 + delta]) + self.series.append(bar_set) + + def remove_barset(self): + sets = self.series.barSets() + len_sets = len(sets) + if len_sets > 0: + self.series.remove(sets[len_sets - 1]) + + def set_legend_alignment(self): + button = self.sender() + legend = self.chart.legend() + alignment = legend.alignment() + + if alignment == Qt.AlignTop: + legend.setAlignment(Qt.AlignLeft) + if button: + button.setText("Align (Left)") + elif alignment == Qt.AlignLeft: + legend.setAlignment(Qt.AlignBottom) + if button: + button.setText("Align (Bottom)") + elif alignment == Qt.AlignBottom: + legend.setAlignment(Qt.AlignRight) + if button: + button.setText("Align (Right)") + else: + if button: + button.setText("Align (Top)") + legend.setAlignment(Qt.AlignTop) + + def toggle_bold(self): + legend = self.chart.legend() + font = legend.font() + font.setBold(not font.bold()) + legend.setFont(font) + + def toggle_italic(self): + legend = self.chart.legend() + font = legend.font() + font.setItalic(not font.italic()) + legend.setFont(font) + + def font_size_changed(self): + legend = self.chart.legend() + font = legend.font() + font_size = self.font_size.value() + if font_size < 1: + font_size = 1 + font.setPointSizeF(font_size) + legend.setFont(font) + + def update_legend_layout(self): + legend = self.chart.legend() + + rect = QRectF(self.legend_posx.value(), + self.legend_posy.value(), + self.legend_width.value(), + self.legend_height.value()) + legend.setGeometry(rect) + + legend.update() + +if __name__ == "__main__": + app = QApplication(sys.argv) + w = MainWidget() + available_geometry = w.screen().availableGeometry() + size = available_geometry.height() * 0.75 + w.setFixedSize(size, size) + w.show() + sys.exit(app.exec_()) diff --git a/examples/charts/legend/legend.pyproject b/examples/charts/legend/legend.pyproject new file mode 100644 index 000000000..f130dcf01 --- /dev/null +++ b/examples/charts/legend/legend.pyproject @@ -0,0 +1,3 @@ +{ + "files": ["legend.py"] +} diff --git a/examples/charts/lineandbar.py b/examples/charts/lineandbar.py index e06298e63..871e06853 100644 --- a/examples/charts/lineandbar.py +++ b/examples/charts/lineandbar.py @@ -1,4 +1,3 @@ - ############################################################################# ## ## Copyright (C) 2018 The Qt Company Ltd. diff --git a/examples/charts/lineandbar/lineandbar.py b/examples/charts/lineandbar/lineandbar.py new file mode 100644 index 000000000..219da17b5 --- /dev/null +++ b/examples/charts/lineandbar/lineandbar.py @@ -0,0 +1,117 @@ + +############################################################################# +## +## Copyright (C) 2021 The Qt Company Ltd. +## Contact: http://www.qt.io/licensing/ +## +## This file is part of the Qt for Python examples of the Qt Toolkit. +## +## $QT_BEGIN_LICENSE:BSD$ +## You may use this file under the terms of the BSD license as follows: +## +## "Redistribution and use in source and binary forms, with or without +## modification, are permitted provided that the following conditions are +## met: +## * Redistributions of source code must retain the above copyright +## notice, this list of conditions and the following disclaimer. +## * Redistributions in binary form must reproduce the above copyright +## notice, this list of conditions and the following disclaimer in +## the documentation and/or other materials provided with the +## distribution. +## * Neither the name of The Qt Company Ltd nor the names of its +## contributors may be used to endorse or promote products derived +## from this software without specific prior written permission. +## +## +## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +## "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +## LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +## A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +## OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +## THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +## +## $QT_END_LICENSE$ +## +############################################################################# + +"""PySide6 port of the line/bar example from Qt v5.x""" + +import sys +from PySide6.QtCore import QPoint, Qt +from PySide6.QtGui import QPainter +from PySide6.QtWidgets import QMainWindow, QApplication +from PySide6.QtCharts import (QBarCategoryAxis, QBarSeries, QBarSet, QChart, + QChartView, QLineSeries, QValueAxis) + + +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) + + +if __name__ == "__main__": + app = QApplication(sys.argv) + + window = TestChart() + window.show() + window.resize(440, 300) + + sys.exit(app.exec_()) diff --git a/examples/charts/lineandbar/lineandbar.pyproject b/examples/charts/lineandbar/lineandbar.pyproject new file mode 100644 index 000000000..8d9010cf7 --- /dev/null +++ b/examples/charts/lineandbar/lineandbar.pyproject @@ -0,0 +1,3 @@ +{ + "files": ["lineandbar.py"] +} diff --git a/examples/charts/linechart/linechart.py b/examples/charts/linechart/linechart.py new file mode 100644 index 000000000..84cd0c8b3 --- /dev/null +++ b/examples/charts/linechart/linechart.py @@ -0,0 +1,84 @@ +############################################################################# +## +## Copyright (C) 2021 The Qt Company Ltd. +## Contact: http://www.qt.io/licensing/ +## +## This file is part of the Qt for Python examples of the Qt Toolkit. +## +## $QT_BEGIN_LICENSE:BSD$ +## You may use this file under the terms of the BSD license as follows: +## +## "Redistribution and use in source and binary forms, with or without +## modification, are permitted provided that the following conditions are +## met: +## * Redistributions of source code must retain the above copyright +## notice, this list of conditions and the following disclaimer. +## * Redistributions in binary form must reproduce the above copyright +## notice, this list of conditions and the following disclaimer in +## the documentation and/or other materials provided with the +## distribution. +## * Neither the name of The Qt Company Ltd nor the names of its +## contributors may be used to endorse or promote products derived +## from this software without specific prior written permission. +## +## +## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +## "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +## LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +## A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +## OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +## THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +## +## $QT_END_LICENSE$ +## +############################################################################# + +"""PySide6 port of the linechart example from Qt v5.x""" + +import sys +from PySide6.QtCore import QPointF +from PySide6.QtGui import QPainter +from PySide6.QtWidgets import QMainWindow, QApplication +from PySide6.QtCharts import QChart, QChartView, QLineSeries + + +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) + + +if __name__ == "__main__": + app = QApplication(sys.argv) + + window = TestChart() + window.show() + window.resize(440, 300) + sys.exit(app.exec_()) diff --git a/examples/charts/linechart/linechart.pyproject b/examples/charts/linechart/linechart.pyproject new file mode 100644 index 000000000..a0b84c285 --- /dev/null +++ b/examples/charts/linechart/linechart.pyproject @@ -0,0 +1,3 @@ +{ + "files": ["linechart.py"] +} diff --git a/examples/charts/logvalueaxis/logvalueaxis.py b/examples/charts/logvalueaxis/logvalueaxis.py new file mode 100644 index 000000000..c5e90c584 --- /dev/null +++ b/examples/charts/logvalueaxis/logvalueaxis.py @@ -0,0 +1,95 @@ +############################################################################# +## +## Copyright (C) 2021 The Qt Company Ltd. +## Contact: http://www.qt.io/licensing/ +## +## This file is part of the Qt for Python examples of the Qt Toolkit. +## +## $QT_BEGIN_LICENSE:BSD$ +## You may use this file under the terms of the BSD license as follows: +## +## "Redistribution and use in source and binary forms, with or without +## modification, are permitted provided that the following conditions are +## met: +## * Redistributions of source code must retain the above copyright +## notice, this list of conditions and the following disclaimer. +## * Redistributions in binary form must reproduce the above copyright +## notice, this list of conditions and the following disclaimer in +## the documentation and/or other materials provided with the +## distribution. +## * Neither the name of The Qt Company Ltd nor the names of its +## contributors may be used to endorse or promote products derived +## from this software without specific prior written permission. +## +## +## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +## "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +## LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +## A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +## OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +## THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +## +## $QT_END_LICENSE$ +## +############################################################################# + +"""PySide6 port of the Logarithmic Axis Example from Qt v5.x""" + + + +import sys +from PySide6.QtCore import Qt, QPointF +from PySide6.QtGui import QPainter +from PySide6.QtWidgets import QMainWindow, QApplication +from PySide6.QtCharts import (QChart, QChartView, QLineSeries, QLogValueAxis, + QValueAxis) + + +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) + + +if __name__ == "__main__": + app = QApplication(sys.argv) + + window = TestChart() + window.show() + window.resize(800, 600) + + sys.exit(app.exec_()) diff --git a/examples/charts/logvalueaxis/logvalueaxis.pyproject b/examples/charts/logvalueaxis/logvalueaxis.pyproject new file mode 100644 index 000000000..b6b3a8e74 --- /dev/null +++ b/examples/charts/logvalueaxis/logvalueaxis.pyproject @@ -0,0 +1,3 @@ +{ + "files": ["logvalueaxis.py"] +} diff --git a/examples/charts/memoryusage.py b/examples/charts/memoryusage.py index 9d7a99311..1266f3566 100644 --- a/examples/charts/memoryusage.py +++ b/examples/charts/memoryusage.py @@ -1,4 +1,3 @@ - ############################################################################# ## ## Copyright (C) 2017 The Qt Company Ltd. diff --git a/examples/charts/memoryusage/memoryusage.py b/examples/charts/memoryusage/memoryusage.py new file mode 100644 index 000000000..a6c536de5 --- /dev/null +++ b/examples/charts/memoryusage/memoryusage.py @@ -0,0 +1,126 @@ + +############################################################################# +## +## Copyright (C) 2021 The Qt Company Ltd. +## Contact: http://www.qt.io/licensing/ +## +## This file is part of the Qt for Python examples of the Qt Toolkit. +## +## $QT_BEGIN_LICENSE:BSD$ +## You may use this file under the terms of the BSD license as follows: +## +## "Redistribution and use in source and binary forms, with or without +## modification, are permitted provided that the following conditions are +## met: +## * Redistributions of source code must retain the above copyright +## notice, this list of conditions and the following disclaimer. +## * Redistributions in binary form must reproduce the above copyright +## notice, this list of conditions and the following disclaimer in +## the documentation and/or other materials provided with the +## distribution. +## * Neither the name of The Qt Company Ltd nor the names of its +## contributors may be used to endorse or promote products derived +## from this software without specific prior written permission. +## +## +## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +## "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +## LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +## A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +## OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +## THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +## +## $QT_END_LICENSE$ +## +############################################################################# + +"""PySide6 Charts example: Simple memory usage viewer""" + +import os +import sys +from PySide6.QtCore import QProcess +from PySide6.QtWidgets import QApplication, QMainWindow +from PySide6.QtCharts import QChart, QChartView, QPieSeries + + +def runProcess(command, arguments): + process = QProcess() + process.start(command, arguments) + process.waitForFinished() + std_output = process.readAllStandardOutput().data().decode('utf-8') + return std_output.split('\n') + +def getMemoryUsage(): + result = [] + if sys.platform == 'win32': + # Windows: Obtain memory usage in KB from 'tasklist' + for line in runProcess('tasklist', [])[3:]: + if len(line) >= 74: + command = line[0:23].strip() + if command.endswith('.exe'): + command = command[0:len(command) - 4] + memoryUsage = float(line[64:74].strip().replace(',', '').replace('.', '')) + legend = '' + if memoryUsage > 10240: + legend = '{} {}M'.format(command, round(memoryUsage / 1024)) + else: + legend = '{} {}K'.format(command, round(memoryUsage)) + result.append([legend, memoryUsage]) + else: + # Unix: Obtain memory usage percentage from 'ps' + psOptions = ['-e', 'v'] + memoryColumn = 8 + commandColumn = 9 + if sys.platform == 'darwin': + psOptions = ['-e', '-v'] + memoryColumn = 11 + commandColumn = 12 + for line in runProcess('ps', psOptions): + tokens = line.split(None) + if len(tokens) > commandColumn and "PID" not in tokens: # Percentage and command + command = tokens[commandColumn] + if not command.startswith('['): + command = os.path.basename(command) + memoryUsage = round(float(tokens[memoryColumn].replace(',', '.'))) + legend = '{} {}%'.format(command, memoryUsage) + result.append([legend, memoryUsage]) + + result.sort(key = lambda x: x[1], reverse=True) + return result + +class MainWindow(QMainWindow): + + def __init__(self): + super(MainWindow, self).__init__() + + self.setWindowTitle('Memory Usage') + + memoryUsage = getMemoryUsage() + if len(memoryUsage) > 5: + memoryUsage = memoryUsage[0:4] + + self.series = QPieSeries() + for item in memoryUsage: + self.series.append(item[0], item[1]) + + slice = self.series.slices()[0] + slice.setExploded() + slice.setLabelVisible() + self.chart = QChart() + self.chart.addSeries(self.series) + self.chartView = QChartView(self.chart) + self.setCentralWidget(self.chartView) + +if __name__ == '__main__': + app = QApplication(sys.argv) + mainWin = MainWindow() + availableGeometry = mainWin.screen().availableGeometry() + size = availableGeometry.height() * 3 / 4 + mainWin.resize(size, size) + mainWin.show() + sys.exit(app.exec_()) diff --git a/examples/charts/memoryusage/memoryusage.pyproject b/examples/charts/memoryusage/memoryusage.pyproject new file mode 100644 index 000000000..4a3c69ef6 --- /dev/null +++ b/examples/charts/memoryusage/memoryusage.pyproject @@ -0,0 +1,3 @@ +{ + "files": ["memoryusage.py"] +} diff --git a/examples/charts/modeldata.py b/examples/charts/modeldata.py index 739b73d63..2ee4ee1a0 100644 --- a/examples/charts/modeldata.py +++ b/examples/charts/modeldata.py @@ -1,4 +1,3 @@ - ############################################################################# ## ## Copyright (C) 2018 The Qt Company Ltd. diff --git a/examples/charts/modeldata/modeldata.py b/examples/charts/modeldata/modeldata.py new file mode 100644 index 000000000..1d10190f4 --- /dev/null +++ b/examples/charts/modeldata/modeldata.py @@ -0,0 +1,183 @@ + +############################################################################# +## +## Copyright (C) 2021 The Qt Company Ltd. +## Contact: http://www.qt.io/licensing/ +## +## This file is part of the Qt for Python examples of the Qt Toolkit. +## +## $QT_BEGIN_LICENSE:BSD$ +## You may use this file under the terms of the BSD license as follows: +## +## "Redistribution and use in source and binary forms, with or without +## modification, are permitted provided that the following conditions are +## met: +## * Redistributions of source code must retain the above copyright +## notice, this list of conditions and the following disclaimer. +## * Redistributions in binary form must reproduce the above copyright +## notice, this list of conditions and the following disclaimer in +## the documentation and/or other materials provided with the +## distribution. +## * Neither the name of The Qt Company Ltd nor the names of its +## contributors may be used to endorse or promote products derived +## from this software without specific prior written permission. +## +## +## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +## "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +## LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +## A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +## OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +## THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +## +## $QT_END_LICENSE$ +## +############################################################################# + +"""PySide6 port of the Model Data example from Qt v5.x""" + +import sys +from random import randrange + +from PySide6.QtCore import QAbstractTableModel, QModelIndex, QRect, Qt +from PySide6.QtGui import QColor, QPainter +from PySide6.QtWidgets import (QApplication, QGridLayout, QHeaderView, + QTableView, QWidget) +from PySide6.QtCharts import QChart, QChartView, QLineSeries, QVXYModelMapper + + +class CustomTableModel(QAbstractTableModel): + def __init__(self): + QAbstractTableModel.__init__(self) + self.input_data = [] + self.mapping = {} + self.column_count = 4 + self.row_count = 15 + + for i in range(self.row_count): + data_vec = [0]*self.column_count + for k in range(len(data_vec)): + if k % 2 == 0: + data_vec[k] = i * 50 + randrange(30) + else: + data_vec[k] = randrange(100) + self.input_data.append(data_vec) + + def rowCount(self, parent=QModelIndex()): + return len(self.input_data) + + def columnCount(self, parent=QModelIndex()): + return self.column_count + + def headerData(self, section, orientation, role): + if role != Qt.DisplayRole: + return None + + if orientation == Qt.Horizontal: + if section % 2 == 0: + return "x" + else: + return "y" + else: + return "{}".format(section + 1) + + def data(self, index, role=Qt.DisplayRole): + if role == Qt.DisplayRole: + return self.input_data[index.row()][index.column()] + elif role == Qt.EditRole: + return self.input_data[index.row()][index.column()] + elif role == Qt.BackgroundRole: + for color, rect in self.mapping.items(): + if rect.contains(index.column(), index.row()): + return QColor(color) + # cell not mapped return white color + return QColor(Qt.white) + return None + + def setData(self, index, value, role=Qt.EditRole): + if index.isValid() and role == Qt.EditRole: + self.input_data[index.row()][index.column()] = float(value) + self.dataChanged.emit(index, index) + return True + return False + + def flags(self, index): + return Qt.ItemIsEnabled | Qt.ItemIsEditable | Qt.ItemIsSelectable + + def add_mapping(self, color, area): + self.mapping[color] = area + + def clear_mapping(self): + self.mapping = {} + + + +class TableWidget(QWidget): + def __init__(self): + QWidget.__init__(self) + + self.model = CustomTableModel() + + self.table_view = QTableView() + self.table_view.setModel(self.model) + self.table_view.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch) + self.table_view.verticalHeader().setSectionResizeMode(QHeaderView.Stretch) + + self.chart = QChart() + self.chart.setAnimationOptions(QChart.AllAnimations) + + self.series = QLineSeries() + self.series.setName("Line 1") + self.mapper = QVXYModelMapper(self) + self.mapper.setXColumn(0) + self.mapper.setYColumn(1) + self.mapper.setSeries(self.series) + self.mapper.setModel(self.model) + self.chart.addSeries(self.series) + + # for storing color hex from the series + seriesColorHex = "#000000" + + # get the color of the series and use it for showing the mapped area + seriesColorHex = "{}".format(self.series.pen().color().name()) + self.model.add_mapping(seriesColorHex, QRect(0, 0, 2, self.model.rowCount())) + + # series 2 + self.series = QLineSeries() + self.series.setName("Line 2") + + self.mapper = QVXYModelMapper(self) + self.mapper.setXColumn(2) + self.mapper.setYColumn(3) + self.mapper.setSeries(self.series) + self.mapper.setModel(self.model) + self.chart.addSeries(self.series) + + # get the color of the series and use it for showing the mapped area + seriesColorHex = "{}".format(self.series.pen().color().name()) + self.model.add_mapping(seriesColorHex, QRect(2, 0, 2, self.model.rowCount())) + + self.chart.createDefaultAxes() + self.chart_view = QChartView(self.chart) + self.chart_view.setRenderHint(QPainter.Antialiasing) + self.chart_view.setMinimumSize(640, 480) + + # create main layout + self.main_layout = QGridLayout() + self.main_layout.addWidget(self.table_view, 1, 0) + self.main_layout.addWidget(self.chart_view, 1, 1) + self.main_layout.setColumnStretch(1, 1) + self.main_layout.setColumnStretch(0, 0) + self.setLayout(self.main_layout) + + +if __name__ == "__main__": + app = QApplication(sys.argv) + w = TableWidget() + w.show() + sys.exit(app.exec_()) diff --git a/examples/charts/modeldata/modeldata.pyproject b/examples/charts/modeldata/modeldata.pyproject new file mode 100644 index 000000000..e10d49eb2 --- /dev/null +++ b/examples/charts/modeldata/modeldata.pyproject @@ -0,0 +1,3 @@ +{ + "files": ["modeldata.py"] +} diff --git a/examples/charts/nesteddonuts.py b/examples/charts/nesteddonuts.py index 09c77d022..9ea11a57f 100644 --- a/examples/charts/nesteddonuts.py +++ b/examples/charts/nesteddonuts.py @@ -1,4 +1,3 @@ - ############################################################################# ## ## Copyright (C) 2018 The Qt Company Ltd. diff --git a/examples/charts/nesteddonuts/nesteddonuts.py b/examples/charts/nesteddonuts/nesteddonuts.py new file mode 100644 index 000000000..9005980a2 --- /dev/null +++ b/examples/charts/nesteddonuts/nesteddonuts.py @@ -0,0 +1,137 @@ + +############################################################################# +## +## Copyright (C) 2018 The Qt Company Ltd. +## Contact: http://www.qt.io/licensing/ +## +## This file is part of the Qt for Python examples of the Qt Toolkit. +## +## $QT_BEGIN_LICENSE:BSD$ +## You may use this file under the terms of the BSD license as follows: +## +## "Redistribution and use in source and binary forms, with or without +## modification, are permitted provided that the following conditions are +## met: +## * Redistributions of source code must retain the above copyright +## notice, this list of conditions and the following disclaimer. +## * Redistributions in binary form must reproduce the above copyright +## notice, this list of conditions and the following disclaimer in +## the documentation and/or other materials provided with the +## distribution. +## * Neither the name of The Qt Company Ltd nor the names of its +## contributors may be used to endorse or promote products derived +## from this software without specific prior written permission. +## +## +## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +## "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +## LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +## A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +## OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +## THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +## +## $QT_END_LICENSE$ +## +############################################################################# + +"""PySide6 port of the Nested Donuts example from Qt v5.x""" + +import sys + +from PySide6.QtCore import Qt, QTimer +from PySide6.QtGui import QPainter +from PySide6.QtWidgets import QApplication, QGridLayout, QWidget +from PySide6.QtCharts import QChart, QChartView, QPieSeries, QPieSlice + + +from random import randrange +from functools import partial + +class Widget(QWidget): + def __init__(self): + QWidget.__init__(self) + self.setMinimumSize(800, 600) + self.donuts = [] + self.chart_view = QChartView() + self.chart_view.setRenderHint(QPainter.Antialiasing) + self.chart = self.chart_view.chart() + self.chart.legend().setVisible(False) + self.chart.setTitle("Nested donuts demo") + self.chart.setAnimationOptions(QChart.AllAnimations) + + self.min_size = 0.1 + self.max_size = 0.9 + self.donut_count = 5 + + self.setup_donuts() + + # create main layout + self.main_layout = QGridLayout(self) + self.main_layout.addWidget(self.chart_view, 1, 1) + self.setLayout(self.main_layout) + + self.update_timer = QTimer(self) + self.update_timer.timeout.connect(self.update_rotation) + self.update_timer.start(1250) + + def setup_donuts(self): + for i in range(self.donut_count): + donut = QPieSeries() + slccount = randrange(3, 6) + for j in range(slccount): + value = randrange(100, 200) + + slc = QPieSlice(str(value), value) + slc.setLabelVisible(True) + slc.setLabelColor(Qt.white) + slc.setLabelPosition(QPieSlice.LabelInsideTangential) + + # Connection using an extra parameter for the slot + slc.hovered[bool].connect(partial(self.explode_slice, slc=slc)) + + donut.append(slc) + size = (self.max_size - self.min_size)/self.donut_count + donut.setHoleSize(self.min_size + i * size) + donut.setPieSize(self.min_size + (i + 1) * size) + + self.donuts.append(donut) + self.chart_view.chart().addSeries(donut) + + + + def update_rotation(self): + for donut in self.donuts: + phase_shift = randrange(-50, 100) + donut.setPieStartAngle(donut.pieStartAngle() + phase_shift) + donut.setPieEndAngle(donut.pieEndAngle() + phase_shift) + + def explode_slice(self, exploded, slc): + if exploded: + self.update_timer.stop() + slice_startangle = slc.startAngle() + slice_endangle = slc.startAngle() + slc.angleSpan() + + donut = slc.series() + idx = self.donuts.index(donut) + for i in range(idx + 1, len(self.donuts)): + self.donuts[i].setPieStartAngle(slice_endangle) + self.donuts[i].setPieEndAngle(360 + slice_startangle) + else: + for donut in self.donuts: + donut.setPieStartAngle(0) + donut.setPieEndAngle(360) + + self.update_timer.start() + + slc.setExploded(exploded) + +if __name__ == "__main__": + app = QApplication(sys.argv) + w = Widget() + w.show() + sys.exit(app.exec_()) diff --git a/examples/charts/nesteddonuts/nesteddonuts.pyproject b/examples/charts/nesteddonuts/nesteddonuts.pyproject new file mode 100644 index 000000000..69653ae73 --- /dev/null +++ b/examples/charts/nesteddonuts/nesteddonuts.pyproject @@ -0,0 +1,3 @@ +{ + "files": ["nesteddonuts.py"] +} diff --git a/examples/charts/percentbarchart.py b/examples/charts/percentbarchart.py index a70cae7cf..ffc5742da 100644 --- a/examples/charts/percentbarchart.py +++ b/examples/charts/percentbarchart.py @@ -1,4 +1,3 @@ - ############################################################################# ## ## Copyright (C) 2018 The Qt Company Ltd. diff --git a/examples/charts/percentbarchart/percentbarchart.py b/examples/charts/percentbarchart/percentbarchart.py new file mode 100644 index 000000000..b9301f36e --- /dev/null +++ b/examples/charts/percentbarchart/percentbarchart.py @@ -0,0 +1,99 @@ + +############################################################################# +## +## Copyright (C) 2021 The Qt Company Ltd. +## Contact: http://www.qt.io/licensing/ +## +## This file is part of the Qt for Python examples of the Qt Toolkit. +## +## $QT_BEGIN_LICENSE:BSD$ +## You may use this file under the terms of the BSD license as follows: +## +## "Redistribution and use in source and binary forms, with or without +## modification, are permitted provided that the following conditions are +## met: +## * Redistributions of source code must retain the above copyright +## notice, this list of conditions and the following disclaimer. +## * Redistributions in binary form must reproduce the above copyright +## notice, this list of conditions and the following disclaimer in +## the documentation and/or other materials provided with the +## distribution. +## * Neither the name of The Qt Company Ltd nor the names of its +## contributors may be used to endorse or promote products derived +## from this software without specific prior written permission. +## +## +## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +## "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +## LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +## A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +## OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +## THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +## +## $QT_END_LICENSE$ +## +############################################################################# + +"""PySide6 port of the Percent Bar Chart example from Qt v5.x""" + +import sys +from PySide6.QtCore import Qt +from PySide6.QtGui import QPainter +from PySide6.QtWidgets import (QMainWindow, QApplication) +from PySide6.QtCharts import (QBarCategoryAxis, QBarSet, QChart, QChartView, + QPercentBarSeries) + +class MainWindow(QMainWindow): + def __init__(self): + QMainWindow.__init__(self) + + set0 = QBarSet("Jane") + set1 = QBarSet("John") + set2 = QBarSet("Axel") + set3 = QBarSet("Mary") + set4 = QBarSet("Samantha") + + set0.append([1, 2, 3, 4, 5, 6]) + set1.append([5, 0, 0, 4, 0, 7]) + set2.append([3, 5, 8, 13, 8, 5]) + set3.append([5, 6, 7, 3, 4, 5]) + set4.append([9, 7, 5, 3, 1, 2]) + + series = QPercentBarSeries() + series.append(set0) + series.append(set1) + series.append(set2) + series.append(set3) + series.append(set4) + + chart = QChart() + chart.addSeries(series) + chart.setTitle("Simple percentbarchart example") + chart.setAnimationOptions(QChart.SeriesAnimations) + + categories = ["Jan", "Feb", "Mar", "Apr", "May", "Jun"] + axis = QBarCategoryAxis() + axis.append(categories) + chart.createDefaultAxes() + chart.setAxisX(axis, series) + + chart.legend().setVisible(True) + chart.legend().setAlignment(Qt.AlignBottom) + + chart_view = QChartView(chart) + chart_view.setRenderHint(QPainter.Antialiasing) + + self.setCentralWidget(chart_view) + + +if __name__ == "__main__": + app = QApplication(sys.argv) + w = MainWindow() + w.resize(420, 300) + w.show() + sys.exit(app.exec_()) diff --git a/examples/charts/percentbarchart/percentbarchart.pyproject b/examples/charts/percentbarchart/percentbarchart.pyproject new file mode 100644 index 000000000..0cf3778af --- /dev/null +++ b/examples/charts/percentbarchart/percentbarchart.pyproject @@ -0,0 +1,3 @@ +{ + "files": ["percentbarchart.py"] +} diff --git a/examples/charts/piechart/piechart.py b/examples/charts/piechart/piechart.py new file mode 100644 index 000000000..29947263d --- /dev/null +++ b/examples/charts/piechart/piechart.py @@ -0,0 +1,87 @@ +############################################################################# +## +## Copyright (C) 2021 The Qt Company Ltd. +## Contact: http://www.qt.io/licensing/ +## +## This file is part of the Qt for Python examples of the Qt Toolkit. +## +## $QT_BEGIN_LICENSE:BSD$ +## You may use this file under the terms of the BSD license as follows: +## +## "Redistribution and use in source and binary forms, with or without +## modification, are permitted provided that the following conditions are +## met: +## * Redistributions of source code must retain the above copyright +## notice, this list of conditions and the following disclaimer. +## * Redistributions in binary form must reproduce the above copyright +## notice, this list of conditions and the following disclaimer in +## the documentation and/or other materials provided with the +## distribution. +## * Neither the name of The Qt Company Ltd nor the names of its +## contributors may be used to endorse or promote products derived +## from this software without specific prior written permission. +## +## +## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +## "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +## LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +## A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +## OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +## THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +## +## $QT_END_LICENSE$ +## +############################################################################# + +"""PySide6 port of the Pie Chart Example from Qt v5.x""" + +import sys +from PySide6.QtCore import Qt +from PySide6.QtGui import QPainter, QPen +from PySide6.QtWidgets import QMainWindow, QApplication +from PySide6.QtCharts import QChart, QChartView, QPieSeries + + +class TestChart(QMainWindow): + + def __init__(self): + QMainWindow.__init__(self) + + self.series = QPieSeries() + + self.series.append('Jane', 1) + self.series.append('Joe', 2) + self.series.append('Andy', 3) + self.series.append('Barbara', 4) + self.series.append('Axel', 5) + + self.slice = self.series.slices()[1] + self.slice.setExploded() + self.slice.setLabelVisible() + self.slice.setPen(QPen(Qt.darkGreen, 2)) + self.slice.setBrush(Qt.green) + + self.chart = QChart() + self.chart.addSeries(self.series) + self.chart.setTitle('Simple piechart example') + self.chart.legend().hide() + + self.chartView = QChartView(self.chart) + self.chartView.setRenderHint(QPainter.Antialiasing) + + self.setCentralWidget(self.chartView) + + +if __name__ == "__main__": + app = QApplication(sys.argv) + + window = TestChart() + window.show() + window.resize(440, 300) + + sys.exit(app.exec_()) diff --git a/examples/charts/piechart/piechart.pyproject b/examples/charts/piechart/piechart.pyproject new file mode 100644 index 000000000..2578b435e --- /dev/null +++ b/examples/charts/piechart/piechart.pyproject @@ -0,0 +1,3 @@ +{ + "files": ["piechart.py"] +} diff --git a/examples/charts/temperaturerecords/temperaturerecords.py b/examples/charts/temperaturerecords/temperaturerecords.py new file mode 100644 index 000000000..3a8a86f42 --- /dev/null +++ b/examples/charts/temperaturerecords/temperaturerecords.py @@ -0,0 +1,96 @@ +############################################################################# +## +## Copyright (C) 2021 The Qt Company Ltd. +## Contact: http://www.qt.io/licensing/ +## +## This file is part of the Qt for Python examples of the Qt Toolkit. +## +## $QT_BEGIN_LICENSE:BSD$ +## You may use this file under the terms of the BSD license as follows: +## +## "Redistribution and use in source and binary forms, with or without +## modification, are permitted provided that the following conditions are +## met: +## * Redistributions of source code must retain the above copyright +## notice, this list of conditions and the following disclaimer. +## * Redistributions in binary form must reproduce the above copyright +## notice, this list of conditions and the following disclaimer in +## the documentation and/or other materials provided with the +## distribution. +## * Neither the name of The Qt Company Ltd nor the names of its +## contributors may be used to endorse or promote products derived +## from this software without specific prior written permission. +## +## +## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +## "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +## LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +## A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +## OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +## THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +## +## $QT_END_LICENSE$ +## +############################################################################# + +"""PySide6 port of the Temperature Records example from Qt v5.x""" + +import sys +from PySide6.QtCore import Qt +from PySide6.QtGui import QPainter +from PySide6.QtWidgets import QMainWindow, QApplication +from PySide6.QtCharts import (QBarCategoryAxis, QBarSet, QChart, QChartView, + QStackedBarSeries, QValueAxis) + + +class MainWindow(QMainWindow): + 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) + +if __name__ == "__main__": + app = QApplication(sys.argv) + w = MainWindow() + w.resize(600, 300) + w.show() + sys.exit(app.exec_()) diff --git a/examples/charts/temperaturerecords/temperaturerecords.pyproject b/examples/charts/temperaturerecords/temperaturerecords.pyproject new file mode 100644 index 000000000..8db39e340 --- /dev/null +++ b/examples/charts/temperaturerecords/temperaturerecords.pyproject @@ -0,0 +1,3 @@ +{ + "files": ["temperaturerecords.py"] +} |