diff options
Diffstat (limited to 'examples/charts/chartthemes/main.py')
-rw-r--r-- | examples/charts/chartthemes/main.py | 247 |
1 files changed, 95 insertions, 152 deletions
diff --git a/examples/charts/chartthemes/main.py b/examples/charts/chartthemes/main.py index e18e92cf2..5787710ca 100644 --- a/examples/charts/chartthemes/main.py +++ b/examples/charts/chartthemes/main.py @@ -1,56 +1,22 @@ -############################################################################# -## -## 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$ -## -############################################################################# - -"""PySide2 port of the Chart Themes example from Qt v5.x""" +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +"""PySide6 port of the Chart Themes example from Qt v5.x""" import sys -from PySide2.QtCore import QPointF, Qt -from PySide2.QtGui import QColor, QPainter, QPalette -from PySide2.QtWidgets import (QApplication, QMainWindow, QSizePolicy, - QWidget) -from PySide2.QtCharts import QtCharts +from PySide6.QtCore import QPointF, Qt +from PySide6.QtGui import QColor, QPainter, QPalette +from PySide6.QtWidgets import (QApplication, QMainWindow, QSizePolicy, + QWidget) +from PySide6.QtCharts import (QAreaSeries, QBarSet, QChart, QChartView, + QLineSeries, QPieSeries, QScatterSeries, + QSplineSeries, QStackedBarSeries) from ui_themewidget import Ui_ThemeWidgetForm as ui from random import random, uniform + class ThemeWidget(QWidget): def __init__(self, parent): QWidget.__init__(self, parent) @@ -61,7 +27,7 @@ class ThemeWidget(QWidget): self.value_max = 10 self.value_count = 7 self.data_table = self.generate_random_data(self.list_count, - self.value_max, self.value_count) + self.value_max, self.value_count) self.ui.setupUi(self) self.populate_themebox() @@ -69,34 +35,33 @@ class ThemeWidget(QWidget): self.populate_legendbox() # Area Chart - chart_view = QtCharts.QChartView(self.create_areachart()) + chart_view = QChartView(self.create_areachart()) self.ui.gridLayout.addWidget(chart_view, 1, 0) self.charts.append(chart_view) # Pie Chart - chart_view = QtCharts.QChartView(self.createPieChart()) - chart_view.setSizePolicy(QSizePolicy.Ignored, - QSizePolicy.Ignored) + chart_view = QChartView(self.create_pie_chart()) + chart_view.setSizePolicy(QSizePolicy.Ignored, QSizePolicy.Ignored) self.ui.gridLayout.addWidget(chart_view, 1, 1) self.charts.append(chart_view) # Line Chart - chart_view = QtCharts.QChartView(self.createLineChart()) + chart_view = QChartView(self.create_line_chart()) self.ui.gridLayout.addWidget(chart_view, 1, 2) self.charts.append(chart_view) # Bar Chart - chart_view = QtCharts.QChartView(self.createBarChart()) + chart_view = QChartView(self.create_bar_chart()) self.ui.gridLayout.addWidget(chart_view, 2, 0) self.charts.append(chart_view) # Spline Chart - chart_view = QtCharts.QChartView(self.createSplineChart()) + chart_view = QChartView(self.create_spline_chart()) self.ui.gridLayout.addWidget(chart_view, 2, 1) self.charts.append(chart_view) # Scatter Chart - chart_view = QtCharts.QChartView(self.create_scatterchart()) + chart_view = QChartView(self.create_scatterchart()) self.ui.gridLayout.addWidget(chart_view, 2, 2) self.charts.append(chart_view) @@ -104,13 +69,12 @@ class ThemeWidget(QWidget): self.ui.antialiasCheckBox.setChecked(True) # Set the colors from the light theme as default ones - pal = qApp.palette() + pal = qApp.palette() # noqa: F821 pal.setColor(QPalette.Window, QColor(0xf0f0f0)) pal.setColor(QPalette.WindowText, QColor(0x404044)) - qApp.setPalette(pal) - - self.updateUI() + qApp.setPalette(pal) # noqa: F821 + self.update_ui() def generate_random_data(self, list_count, value_max, value_count): data_table = [] @@ -122,7 +86,7 @@ class ThemeWidget(QWidget): y_value += uniform(0, constant) x_value = (j + random()) * constant value = QPointF(x_value, y_value) - label = "Slice {}: {}".format(i, j) + label = f"Slice {i}: {j}" data_list.append((value, label)) data_table.append(data_list) @@ -131,22 +95,22 @@ class ThemeWidget(QWidget): def populate_themebox(self): theme = self.ui.themeComboBox - theme.addItem("Light", QtCharts.QChart.ChartThemeLight) - theme.addItem("Blue Cerulean", QtCharts.QChart.ChartThemeBlueCerulean) - theme.addItem("Dark", QtCharts.QChart.ChartThemeDark) - theme.addItem("Brown Sand", QtCharts.QChart.ChartThemeBrownSand) - theme.addItem("Blue NCS", QtCharts.QChart.ChartThemeBlueNcs) - theme.addItem("High Contrast", QtCharts.QChart.ChartThemeHighContrast) - theme.addItem("Blue Icy", QtCharts.QChart.ChartThemeBlueIcy) - theme.addItem("Qt", QtCharts.QChart.ChartThemeQt) + theme.addItem("Light", QChart.ChartThemeLight) + theme.addItem("Blue Cerulean", QChart.ChartThemeBlueCerulean) + theme.addItem("Dark", QChart.ChartThemeDark) + theme.addItem("Brown Sand", QChart.ChartThemeBrownSand) + theme.addItem("Blue NCS", QChart.ChartThemeBlueNcs) + theme.addItem("High Contrast", QChart.ChartThemeHighContrast) + theme.addItem("Blue Icy", QChart.ChartThemeBlueIcy) + theme.addItem("Qt", QChart.ChartThemeQt) def populate_animationbox(self): animated = self.ui.animatedComboBox - animated.addItem("No Animations", QtCharts.QChart.NoAnimation) - animated.addItem("GridAxis Animations", QtCharts.QChart.GridAxisAnimations) - animated.addItem("Series Animations", QtCharts.QChart.SeriesAnimations) - animated.addItem("All Animations", QtCharts.QChart.AllAnimations) + animated.addItem("No Animations", QChart.NoAnimation) + animated.addItem("GridAxis Animations", QChart.GridAxisAnimations) + animated.addItem("Series Animations", QChart.SeriesAnimations) + animated.addItem("All Animations", QChart.AllAnimations) def populate_legendbox(self): legend = self.ui.legendComboBox @@ -158,42 +122,44 @@ class ThemeWidget(QWidget): legend.addItem("Legend Right", Qt.AlignRight) def create_areachart(self): - chart = QtCharts.QChart() + chart = QChart() chart.setTitle("Area Chart") # The lower series initialized to zero values lower_series = None name = "Series " for i in range(len(self.data_table)): - upper_series = QtCharts.QLineSeries(chart) + upper_series = QLineSeries(chart) for j in range(len(self.data_table[i])): data = self.data_table[i][j] if lower_series: - points = lower_series.pointsVector() + points = lower_series.points() y_value = points[i].y() + data[0].y() upper_series.append(QPointF(j, y_value)) else: upper_series.append(QPointF(j, data[0].y())) - area = QtCharts.QAreaSeries(upper_series, lower_series) - area.setName("{}{}".format(name, i)) + area = QAreaSeries(upper_series, lower_series) + area.setName(f"{name}{i}") chart.addSeries(area) lower_series = upper_series chart.createDefaultAxes() - chart.axisX().setRange(0, self.value_count - 1) - chart.axisY().setRange(0, self.value_max) + axis_x = chart.axes(Qt.Horizontal)[0] + axis_x.setRange(0, self.value_count - 1) + axis_y = chart.axes(Qt.Vertical)[0] + axis_y.setRange(0, self.value_max) # Add space to label to add space between labels and axis - chart.axisY().setLabelFormat("%.1f ") + axis_y.setLabelFormat("%.1f ") return chart - def createBarChart(self): - chart = QtCharts.QChart() + def create_bar_chart(self): + chart = QChart() chart.setTitle("Bar chart") - series = QtCharts.QStackedBarSeries(chart) + series = QStackedBarSeries(chart) for i in range(len(self.data_table)): - barset = QtCharts.QBarSet("Bar set {}".format(i)) + barset = QBarSet(f"Bar set {i}") for data in self.data_table[i]: barset.append(data[0].y()) series.append(barset) @@ -201,37 +167,40 @@ class ThemeWidget(QWidget): chart.addSeries(series) chart.createDefaultAxes() - chart.axisY().setRange(0, self.value_max * 2) + axis_y = chart.axes(Qt.Vertical)[0] + axis_y.setRange(0, self.value_max * 2) # Add space to label to add space between labels and axis - chart.axisY().setLabelFormat("%.1f ") + axis_y.setLabelFormat("%.1f ") return chart - def createLineChart(self): - chart = QtCharts.QChart() + def create_line_chart(self): + chart = QChart() chart.setTitle("Line chart") name = "Series " for i, lst in enumerate(self.data_table): - series = QtCharts.QLineSeries(chart) + series = QLineSeries(chart) for data in lst: series.append(data[0]) - series.setName("{}{}".format(name, i)) + series.setName(f"{name}{i}") chart.addSeries(series) chart.createDefaultAxes() - chart.axisX().setRange(0, self.value_max) - chart.axisY().setRange(0, self.value_count) + axis_x = chart.axes(Qt.Horizontal)[0] + axis_x.setRange(0, self.value_max) + axis_y = chart.axes(Qt.Vertical)[0] + axis_y.setRange(0, self.value_count) # Add space to label to add space between labels and axis - chart.axisY().setLabelFormat("%.1f ") + axis_y.setLabelFormat("%.1f ") return chart - def createPieChart(self): - chart = QtCharts.QChart() + def create_pie_chart(self): + chart = QChart() chart.setTitle("Pie chart") - series = QtCharts.QPieSeries(chart) + series = QPieSeries(chart) for data in self.data_table[0]: slc = series.append(data[1], data[0].y()) if data == self.data_table[0][0]: @@ -245,45 +214,49 @@ class ThemeWidget(QWidget): return chart - def createSplineChart(self): - chart = QtCharts.QChart() + def create_spline_chart(self): + chart = QChart() chart.setTitle("Spline chart") name = "Series " for i, lst in enumerate(self.data_table): - series = QtCharts.QSplineSeries(chart) + series = QSplineSeries(chart) for data in lst: series.append(data[0]) - series.setName("{}{}".format(name, i)) + series.setName(f"{name}{i}") chart.addSeries(series) chart.createDefaultAxes() - chart.axisX().setRange(0, self.value_max) - chart.axisY().setRange(0, self.value_count) + axis_x = chart.axes(Qt.Horizontal)[0] + axis_x.setRange(0, self.value_max) + axis_y = chart.axes(Qt.Vertical)[0] + axis_y.setRange(0, self.value_count) # Add space to label to add space between labels and axis - chart.axisY().setLabelFormat("%.1f ") + axis_y.setLabelFormat("%.1f ") return chart def create_scatterchart(self): - chart = QtCharts.QChart() + chart = QChart() chart.setTitle("Scatter chart") name = "Series " for i, lst in enumerate(self.data_table): - series = QtCharts.QScatterSeries(chart) + series = QScatterSeries(chart) for data in lst: series.append(data[0]) - series.setName("{}{}".format(name, i)) + series.setName(f"{name}{i}") chart.addSeries(series) chart.createDefaultAxes() - chart.axisX().setRange(0, self.value_max) - chart.axisY().setRange(0, self.value_count) + axis_x = chart.axes(Qt.Horizontal)[0] + axis_x.setRange(0, self.value_max) + axis_y = chart.axes(Qt.Vertical)[0] + axis_y.setRange(0, self.value_count) # Add space to label to add space between labels and axis - chart.axisY().setLabelFormat("%.1f ") + axis_y.setLabelFormat("%.1f ") return chart - def updateUI(self): + def update_ui(self): def set_colors(window_color, text_color): pal = self.window().palette() pal.setColor(QPalette.Window, window_color) @@ -297,46 +270,26 @@ class ThemeWidget(QWidget): chart_theme = self.charts[0].chart().theme() if chart_theme != theme: for chart_view in self.charts: - if theme == 0: - theme_name = QtCharts.QChart.ChartThemeLight - elif theme == 1: - theme_name = QtCharts.QChart.ChartThemeBlueCerulean - elif theme == 2: - theme_name = QtCharts.QChart.ChartThemeDark - elif theme == 3: - theme_name = QtCharts.QChart.ChartThemeBrownSand - elif theme == 4: - theme_name = QtCharts.QChart.ChartThemeBlueNcs - elif theme == 5: - theme_name = QtCharts.QChart.ChartThemeHighContrast - elif theme == 6: - theme_name = QtCharts.QChart.ChartThemeBlueIcy - elif theme == 7: - theme_name = QtCharts.QChart.ChartThemeQt - else: - theme_name = QtCharts.QChart.ChartThemeLight - - chart_view.chart().setTheme(theme_name) + chart_view.chart().setTheme(theme) # Set palette colors based on selected theme - if theme == QtCharts.QChart.ChartThemeLight: + if theme == QChart.ChartThemeLight: set_colors(QColor(0xf0f0f0), QColor(0x404044)) - elif theme == QtCharts.QChart.ChartThemeDark: + elif theme == QChart.ChartThemeDark: set_colors(QColor(0x121218), QColor(0xd6d6d6)) - elif theme == QtCharts.QChart.ChartThemeBlueCerulean: + elif theme == QChart.ChartThemeBlueCerulean: set_colors(QColor(0x40434a), QColor(0xd6d6d6)) - elif theme == QtCharts.QChart.ChartThemeBrownSand: + elif theme == QChart.ChartThemeBrownSand: set_colors(QColor(0x9e8965), QColor(0x404044)) - elif theme == QtCharts.QChart.ChartThemeBlueNcs: + elif theme == QChart.ChartThemeBlueNcs: set_colors(QColor(0x018bba), QColor(0x404044)) - elif theme == QtCharts.QChart.ChartThemeHighContrast: + elif theme == QChart.ChartThemeHighContrast: set_colors(QColor(0xffab03), QColor(0x181818)) - elif theme == QtCharts.QChart.ChartThemeBlueIcy: + elif theme == QChart.ChartThemeBlueIcy: set_colors(QColor(0xcee7f0), QColor(0x404044)) else: set_colors(QColor(0xf0f0f0), QColor(0x404044)) - # Update antialiasing checked = self.ui.antialiasCheckBox.isChecked() for chart in self.charts: @@ -347,20 +300,10 @@ class ThemeWidget(QWidget): options = self.ui.animatedComboBox.itemData(idx) if len(self.charts): - chart = self.charts[0].chart() - animation_options = chart.animationOptions() + animation_options = self.charts[0].chart().animationOptions() if animation_options != options: for chart_view in self.charts: - options_name = QtCharts.QChart.NoAnimation - if options == 0: - options_name = QtCharts.QChart.NoAnimation - elif options == 1: - options_name = QtCharts.QChart.GridAxisAnimations - elif options == 2: - options_name = QtCharts.QChart.SeriesAnimations - elif options == 3: - options_name = QtCharts.QChart.AllAnimations - chart_view.chart().setAnimationOptions(options_name) + chart_view.chart().setAnimationOptions(options) # Update legend alignment idx = self.ui.legendComboBox.currentIndex() @@ -389,8 +332,8 @@ if __name__ == "__main__": window = QMainWindow() widget = ThemeWidget(None) window.setCentralWidget(widget) - available_geometry = app.desktop().availableGeometry(window) + available_geometry = window.screen().availableGeometry() size = available_geometry.height() * 0.75 window.setFixedSize(size, size * 0.8) window.show() - sys.exit(app.exec_()) + sys.exit(app.exec()) |