功能:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Created on 2019/10/2
@author: Irony
@site: https://pyqt.site , https://github.com/PyQt5
@email: 892768447@qq.com
@file: ChartThemes
@description: 图表主题动画等
"""
#############################################################################
##
## Copyright (C) 2013 Riverbank Computing Limited
## Copyright (C) 2012 Digia Plc
## All rights reserved.
##
## This file is part of the PyQtChart examples.
##
## $QT_BEGIN_LICENSE$
## Licensees holding valid Qt Commercial licenses may use this file in
## accordance with the Qt Commercial License Agreement provided with the
## Software or, alternatively, in accordance with the terms contained in
## a written agreement between you and Digia.
## $QT_END_LICENSE$
##
#############################################################################
import random
try:
from PyQt5.QtChart import (QAreaSeries, QBarSet, QChart, QChartView,
QLineSeries, QPieSeries, QScatterSeries, QSplineSeries,
QStackedBarSeries)
from PyQt5.QtCore import pyqtSlot, QPointF, Qt
from PyQt5.QtGui import QColor, QPainter, QPalette
from PyQt5.QtWidgets import QApplication, QMainWindow, QCheckBox, QComboBox, QGridLayout, QHBoxLayout, \
QLabel, QSizePolicy, QWidget
except ImportError:
from PySide2.QtCore import Slot as pyqtSlot, QPointF, Qt
from PySide2.QtGui import QColor, QPainter, QPalette
from PySide2.QtWidgets import QApplication, QMainWindow, QCheckBox, QComboBox, QGridLayout, QHBoxLayout, \
QLabel, QSizePolicy, QWidget
from PySide2.QtCharts import QtCharts
QChartView = QtCharts.QChartView
QChart = QtCharts.QChart
QAreaSeries = QtCharts.QAreaSeries
QBarSet = QtCharts.QBarSet
QLineSeries = QtCharts.QLineSeries
QPieSeries = QtCharts.QPieSeries
QScatterSeries = QtCharts.QScatterSeries
QSplineSeries = QtCharts.QSplineSeries
QStackedBarSeries = QtCharts.QStackedBarSeries
class ThemeWidget(QWidget):
def __init__(self, parent=None):
super(ThemeWidget, self).__init__(parent)
self.m_charts = []
self.m_listCount = 3
self.m_valueMax = 10
self.m_valueCount = 7
self.m_dataTable = self.generateRandomData(self.m_listCount,
self.m_valueMax, self.m_valueCount)
self.m_themeComboBox = self.createThemeBox()
self.m_antialiasCheckBox = QCheckBox("Anti-aliasing")
self.m_animatedComboBox = self.createAnimationBox()
self.m_legendComboBox = self.createLegendBox()
self.connectSignals()
# Create the layout.
baseLayout = QGridLayout()
settingsLayout = QHBoxLayout()
settingsLayout.addWidget(QLabel("Theme:"))
settingsLayout.addWidget(self.m_themeComboBox)
settingsLayout.addWidget(QLabel("Animation:"))
settingsLayout.addWidget(self.m_animatedComboBox)
settingsLayout.addWidget(QLabel("Legend:"))
settingsLayout.addWidget(self.m_legendComboBox)
settingsLayout.addWidget(self.m_antialiasCheckBox)
settingsLayout.addStretch()
baseLayout.addLayout(settingsLayout, 0, 0, 1, 3)
# Create the charts.
chartView = QChartView(self.createAreaChart())
baseLayout.addWidget(chartView, 1, 0)
self.m_charts.append(chartView)
chartView = QChartView(self.createBarChart(self.m_valueCount))
baseLayout.addWidget(chartView, 1, 1)
self.m_charts.append(chartView)
chartView = QChartView(self.createLineChart())
baseLayout.addWidget(chartView, 1, 2)
self.m_charts.append(chartView)
chartView = QChartView(self.createPieChart())
# Funny things happen if the pie slice labels no not fit the screen...
chartView.setSizePolicy(QSizePolicy.Ignored, QSizePolicy.Ignored)
baseLayout.addWidget(chartView, 2, 0)
self.m_charts.append(chartView)
chartView = QChartView(self.createSplineChart())
baseLayout.addWidget(chartView, 2, 1)
self.m_charts.append(chartView)
chartView = QChartView(self.createScatterChart())
baseLayout.addWidget(chartView, 2, 2)
self.m_charts.append(chartView)
self.setLayout(baseLayout)
# Set the defaults.
self.m_antialiasCheckBox.setChecked(True)
self.updateUI()
def connectSignals(self):
self.m_themeComboBox.currentIndexChanged.connect(self.updateUI)
self.m_antialiasCheckBox.toggled.connect(self.updateUI)
self.m_animatedComboBox.currentIndexChanged.connect(self.updateUI)
self.m_legendComboBox.currentIndexChanged.connect(self.updateUI)
def generateRandomData(self, listCount, valueMax, valueCount):
random.seed()
dataTable = []
for i in range(listCount):
dataList = []
yValue = 0.0
f_valueCount = float(valueCount)
for j in range(valueCount):
yValue += random.uniform(0, valueMax) / f_valueCount
value = QPointF(
j + random.random() * self.m_valueMax / f_valueCount,
yValue)
label = "Slice " + str(i) + ":" + str(j)
dataList.append((value, label))
dataTable.append(dataList)
return dataTable
def createThemeBox(self):
themeComboBox = QComboBox()
themeComboBox.addItem("Light", QChart.ChartThemeLight)
themeComboBox.addItem("Blue Cerulean", QChart.ChartThemeBlueCerulean)
themeComboBox.addItem("Dark", QChart.ChartThemeDark)
themeComboBox.addItem("Brown Sand", QChart.ChartThemeBrownSand)
themeComboBox.addItem("Blue NCS", QChart.ChartThemeBlueNcs)
themeComboBox.addItem("High Contrast", QChart.ChartThemeHighContrast)
themeComboBox.addItem("Blue Icy", QChart.ChartThemeBlueIcy)
return themeComboBox
def createAnimationBox(self):
animationComboBox = QComboBox()
animationComboBox.addItem("No Animations", QChart.NoAnimation)
animationComboBox.addItem("GridAxis Animations", QChart.GridAxisAnimations)
animationComboBox.addItem("Series Animations", QChart.SeriesAnimations)
animationComboBox.addItem("All Animations", QChart.AllAnimations)
return animationComboBox
def createLegendBox(self):
legendComboBox = QComboBox()
legendComboBox.addItem("No Legend ", 0)
legendComboBox.addItem("Legend Top", Qt.AlignTop)
legendComboBox.addItem("Legend Bottom", Qt.AlignBottom)
legendComboBox.addItem("Legend Left", Qt.AlignLeft)
legendComboBox.addItem("Legend Right", Qt.AlignRight)
return legendComboBox
def createAreaChart(self):
chart = QChart()
chart.setTitle("Area chart")
# The lower series is initialized to zero values.
lowerSeries = None
y_points = []
for i, data_list in enumerate(self.m_dataTable):
upperSeries = QLineSeries(chart)
for j, (value, _) in enumerate(data_list):
y = value.y()
if lowerSeries is None:
upperSeries.append(QPointF(j, y))
y_points.append(y)
else:
new_y = y_points[i] + y
upperSeries.append(QPointF(j, new_y))
y_points[j] += new_y
area = QAreaSeries(upperSeries, lowerSeries)
area.setName("Series " + str(i))
chart.addSeries(area)
lowerSeries = upperSeries
chart.createDefaultAxes()
return chart
def createBarChart(self, valueCount):
chart = QChart()
chart.setTitle("Bar chart")
series = QStackedBarSeries(chart)
for i, data_list in enumerate(self.m_dataTable):
set = QBarSet("Bar set " + str(i))
for value, _ in data_list:
set << value.y()
series.append(set)
chart.addSeries(series)
chart.createDefaultAxes()
return chart
def createLineChart(self):
chart = QChart()
chart.setTitle("Line chart")
for i, data_list in enumerate(self.m_dataTable):
series = QLineSeries(chart)
for value, _ in data_list:
series.append(value)
series.setName("Series " + str(i))
chart.addSeries(series)
chart.createDefaultAxes()
return chart
def createPieChart(self):
chart = QChart()
chart.setTitle("Pie chart")
pieSize = 1.0 / len(self.m_dataTable)
for i, data_list in enumerate(self.m_dataTable):
series = QPieSeries(chart)
for value, label in data_list:
slice = series.append(label, value.y())
if series.count() == 1:
slice.setLabelVisible()
slice.setExploded()
hPos = (pieSize / 2) + (i / float(len(self.m_dataTable)))
series.setPieSize(pieSize)
series.setHorizontalPosition(hPos)
series.setVerticalPosition(0.5)
chart.addSeries(series)
return chart
def createSplineChart(self):
chart = QChart()
chart.setTitle("Spline chart")
for i, data_list in enumerate(self.m_dataTable):
series = QSplineSeries(chart)
for value, _ in data_list:
series.append(value)
series.setName("Series " + str(i))
chart.addSeries(series)
chart.createDefaultAxes()
return chart
def createScatterChart(self):
chart = QChart()
chart.setTitle("Scatter chart")
for i, data_list in enumerate(self.m_dataTable):
series = QScatterSeries(chart)
for value, _ in data_list:
series.append(value)
series.setName("Series " + str(i))
chart.addSeries(series)
chart.createDefaultAxes()
return chart
@pyqtSlot()
def updateUI(self):
theme = self.m_themeComboBox.itemData(
self.m_themeComboBox.currentIndex())
if self.m_charts[0].chart().theme() != theme:
for chartView in self.m_charts:
chartView.chart().setTheme(QChart.ChartTheme(theme))
pal = self.window().palette()
if theme == QChart.ChartThemeLight:
pal.setColor(QPalette.Window, QColor(0xf0f0f0))
pal.setColor(QPalette.WindowText, QColor(0x404044))
elif theme == QChart.ChartThemeDark:
pal.setColor(QPalette.Window, QColor(0x121218))
pal.setColor(QPalette.WindowText, QColor(0xd6d6d6))
elif theme == QChart.ChartThemeBlueCerulean:
pal.setColor(QPalette.Window, QColor(0x40434a))
pal.setColor(QPalette.WindowText, QColor(0xd6d6d6))
elif theme == QChart.ChartThemeBrownSand:
pal.setColor(QPalette.Window, QColor(0x9e8965))
pal.setColor(QPalette.WindowText, QColor(0x404044))
elif theme == QChart.ChartThemeBlueNcs:
pal.setColor(QPalette.Window, QColor(0x018bba))
pal.setColor(QPalette.WindowText, QColor(0x404044))
elif theme == QChart.ChartThemeHighContrast:
pal.setColor(QPalette.Window, QColor(0xffab03))
pal.setColor(QPalette.WindowText, QColor(0x181818))
elif theme == QChart.ChartThemeBlueIcy:
pal.setColor(QPalette.Window, QColor(0xcee7f0))
pal.setColor(QPalette.WindowText, QColor(0x404044))
else:
pal.setColor(QPalette.Window, QColor(0xf0f0f0))
pal.setColor(QPalette.WindowText, QColor(0x404044))
self.window().setPalette(pal)
checked = self.m_antialiasCheckBox.isChecked()
for chartView in self.m_charts:
chartView.setRenderHint(QPainter.Antialiasing, checked)
options = QChart.AnimationOptions(
self.m_animatedComboBox.itemData(
self.m_animatedComboBox.currentIndex()))
if self.m_charts[0].chart().animationOptions() != options:
for chartView in self.m_charts:
chartView.chart().setAnimationOptions(options)
alignment = self.m_legendComboBox.itemData(
self.m_legendComboBox.currentIndex())
for chartView in self.m_charts:
legend = chartView.chart().legend()
if alignment == 0:
legend.hide()
else:
legend.setAlignment(Qt.Alignment(alignment))
legend.show()
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
window = QMainWindow()
widget = ThemeWidget()
window.setCentralWidget(widget)
window.resize(900, 600)
window.show()
sys.exit(app.exec_())
QChart是Qt框架中用于创建图表的类。它是Qt Charts模块的一部分,提供了创建、显示和交互式操作各种类型的图表的功能。
QChart可以用于绘制多种类型的图表,包括线图、柱状图、饼图、散点图等。它提供了丰富的图表设置选项,包括轴的设置、图例、网格线、背景样式等,以及数据系列的管理和配置。
通过QChart,可以实现以下功能:
QChart是一个强大而灵活的图表类,可以满足各种数据可视化的需求。它与Qt框架的其他组件和模块结合使用,可以创建出富有交互性和美观的图表应用程序。
下边为使用QChart的综合案例:
from PyQt5.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QWidget, QComboBox
from PyQt5.QtChart import QChart, QChartView, QLineSeries
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
# 创建主窗口布局和中心窗口部件
layout = QVBoxLayout()
central_widget = QWidget()
central_widget.setLayout(layout)
self.setCentralWidget(central_widget)
# 创建QChart和QChartView
chart = QChart()
chart_view = QChartView(chart)
layout.addWidget(chart_view)
# 创建数据系列并添加到图表中
series = QLineSeries()
series.append(0, 0)
series.append(1, 1)
series.append(2, 2)
chart.addSeries(series)
# 设置图表标题和坐标轴标签
chart.setTitle("Line Chart")
chart.setAxisX(QValueAxis(), series)
chart.setAxisY(QValueAxis(), series)
# 创建下拉框用于选择图表主题
theme_combo_box = self.createThemeBox(chart)
layout.addWidget(theme_combo_box)
def createThemeBox(self, chart):
theme_combo_box = QComboBox()
theme_combo_box.addItem("Light")
theme_combo_box.addItem("Dark")
theme_combo_box.addItem("Blue Cerulean")
theme_combo_box.addItem("Brown Sand")
theme_combo_box.addItem("Blue NCS")
theme_combo_box.addItem("High Contrast")
theme_combo_box.addItem("Blue Icy")
theme_combo_box.currentIndexChanged.connect(lambda: self.setChartTheme(chart, theme_combo_box.currentText()))
return theme_combo_box
def setChartTheme(self, chart, theme):
if theme == "Light":
chart.setTheme(QChart.ChartThemeLight)
elif theme == "Dark":
chart.setTheme(QChart.ChartThemeDark)
elif theme == "Blue Cerulean":
chart.setTheme(QChart.ChartThemeBlueCerulean)
elif theme == "Brown Sand":
chart.setTheme(QChart.ChartThemeBrownSand)
elif theme == "Blue NCS":
chart.setTheme(QChart.ChartThemeBlueNcs)
elif theme == "High Contrast":
chart.setTheme(QChart.ChartThemeHighContrast)
elif theme == "Blue Icy":
chart.setTheme(QChart.ChartThemeBlueIcy)
if __name__ == '__main__':
app = QApplication([])
window = MainWindow()
window.show()
app.exec_()
这个例子创建了一个主窗口,并在主窗口中显示一个简单的线图。用户可以通过下拉框选择图表的主题(“Light"或"Dark”)。当选择不同的主题时,图表的样式会相应地改变。
在这个例子中,使用了QChart、QChartView、QLineSeries等类来创建和显示图表。通过添加数据系列、设置图表标题和坐标轴标签,并通过下拉框控制图表的主题,实现了一个简单的图表应用程序。
PyQt5和PySide2都是Python中使用Qt框架的工具包,它们提供了对Qt库的Python绑定。它们的主要区别在于它们的开发和使用许可证不同。PyQt5是由Riverbank Computing有限公司开发的,使用的是GPL和商业许可证,而PySide2是由Qt公司开发的,使用的是LGPL许可证。此外,PyQt5包含了Qt的全部功能,而PySide2只包含了其中的一部分。
在使用上,两者提供了类似的API和功能,但PyQt5相对而言更稳定,文档和教程也更全面,因为它已经存在更长的时间并且有更多的用户和开发者。但是,由于PySide2使用的是LGPL许可证,因此在一些特定的情况下,可能更适合某些项目的使用。
在该代码段中:作者试图导入PyQt5或者是PySide2的任意一种库:
import random
try:
from PyQt5.QtChart import (QAreaSeries, QBarSet, QChart, QChartView,
QLineSeries, QPieSeries, QScatterSeries, QSplineSeries,
QStackedBarSeries)
from PyQt5.QtCore import pyqtSlot, QPointF, Qt
from PyQt5.QtGui import QColor, QPainter, QPalette
from PyQt5.QtWidgets import QApplication, QMainWindow, QCheckBox, QComboBox, QGridLayout, QHBoxLayout, \
QLabel, QSizePolicy, QWidget
except ImportError:
from PySide2.QtCore import Slot as pyqtSlot, QPointF, Qt
from PySide2.QtGui import QColor, QPainter, QPalette
from PySide2.QtWidgets import QApplication, QMainWindow, QCheckBox, QComboBox, QGridLayout, QHBoxLayout, \
QLabel, QSizePolicy, QWidget
from PySide2.QtCharts import QtCharts
QChartView = QtCharts.QChartView
QChart = QtCharts.QChart
QAreaSeries = QtCharts.QAreaSeries
QBarSet = QtCharts.QBarSet
QLineSeries = QtCharts.QLineSeries
QPieSeries = QtCharts.QPieSeries
QScatterSeries = QtCharts.QScatterSeries
QSplineSeries = QtCharts.QSplineSeries
QStackedBarSeries = QtCharts.QStackedBarSeries
这段代码定义了一个名为ThemeWidget的QWidget类,用于展示多种不同主题下的图表,并提供一些设置选项。
代码如下:
class ThemeWidget(QWidget):
def __init__(self, parent=None):
super(ThemeWidget, self).__init__(parent)
self.m_charts = []
self.m_listCount = 3
self.m_valueMax = 10
self.m_valueCount = 7
# 数据生成函数
self.m_dataTable = self.generateRandomData(self.m_listCount,
self.m_valueMax, self.m_valueCount)
# 创建下拉框
self.m_themeComboBox = self.createThemeBox()
# 创建复选框
self.m_antialiasCheckBox = QCheckBox("Anti-aliasing")
# 创建复选框
self.m_animatedComboBox = self.createAnimationBox()
# 创建复选框,是否设置标签栏
self.m_legendComboBox = self.createLegendBox()
# 连接信号与槽,通过调用connectSignals()方法来实现。
self.connectSignals()
# Create the layout.
baseLayout = QGridLayout()
# 把各个组件都添加到水平布局中去
settingsLayout = QHBoxLayout()
settingsLayout.addWidget(QLabel("Theme:"))
settingsLayout.addWidget(self.m_themeComboBox)
settingsLayout.addWidget(QLabel("Animation:"))
settingsLayout.addWidget(self.m_animatedComboBox)
settingsLayout.addWidget(QLabel("Legend:"))
settingsLayout.addWidget(self.m_legendComboBox)
settingsLayout.addWidget(self.m_antialiasCheckBox)
# 将一个弹性空间添加到settingLayout水平布局中去。弹性控件会占据任何剩余的可用空间,并在布局中创建一个可拉伸的空白区域。addStretch()的作用是在这些控件之后添加一个可拉伸的空白区域,使得控件在水平方向上不会紧密排列,而是具有一定的间距。!保持美观
settingsLayout.addStretch()
# 0行0列,指布局行跨度为1,列跨度为3。
baseLayout.addLayout(settingsLayout, 0, 0, 1, 3)
# Create the charts.
# 创建图表,并将其添加到m_charts中去
chartView = QChartView(self.createAreaChart())
baseLayout.addWidget(chartView, 1, 0)
self.m_charts.append(chartView)
chartView = QChartView(self.createBarChart(self.m_valueCount))
baseLayout.addWidget(chartView, 1, 1)
self.m_charts.append(chartView)
chartView = QChartView(self.createLineChart())
baseLayout.addWidget(chartView, 1, 2)
self.m_charts.append(chartView)
chartView = QChartView(self.createPieChart())
# Funny things happen if the pie slice labels no not fit the screen...
chartView.setSizePolicy(QSizePolicy.Ignored, QSizePolicy.Ignored)
baseLayout.addWidget(chartView, 2, 0)
self.m_charts.append(chartView)
chartView = QChartView(self.createSplineChart())
baseLayout.addWidget(chartView, 2, 1)
self.m_charts.append(chartView)
chartView = QChartView(self.createScatterChart())
baseLayout.addWidget(chartView, 2, 2)
self.m_charts.append(chartView)
self.setLayout(baseLayout)
# Set the defaults.
# 将抗锯齿复选框设置为选中状态。
self.m_antialiasCheckBox.setChecked(True)
# 调用updateUI方法,更新图表的外观和行为。
self.updateUI()
信号与槽的连接:
def connectSignals(self):
self.m_themeComboBox.currentIndexChanged.connect(self.updateUI)
self.m_antialiasCheckBox.toggled.connect(self.updateUI)
self.m_animatedComboBox.currentIndexChanged.connect(self.updateUI)
self.m_legendComboBox.currentIndexChanged.connect(self.updateUI)
数据生成函数: 构造二维点,以及他们对应的标签。形如(PyQt5.QtCore.QPointF(1.2604348685342468, 0.1913492455939141), ‘Slice 0:1’)
def generateRandomData(self, listCount, valueMax, valueCount):
random.seed()
dataTable = []
for i in range(listCount):
dataList = []
yValue = 0.0
f_valueCount = float(valueCount)
for j in range(valueCount):
# random.uniform: 生成指定范围内均匀分布的随机浮点数。
yValue += random.uniform(0, valueMax) / f_valueCount
# QPointF是Qt库中用于表示2D点的类。QPointF的构造函数接受两个参数,分别是点的 x 坐标和 y 坐标。
value = QPointF(
# random.random()函数生成一个0到1之间的随机浮点数
j + random.random() * self.m_valueMax / f_valueCount,
yValue)
label = "Slice " + str(i) + ":" + str(j)
# 形如(PyQt5.QtCore.QPointF(1.2604348685342468, 0.1913492455939141), 'Slice 0:1')的元素
dataList.append((value, label))
dataTable.append(dataList)
return dataTable
QComboBox():QComboBox是Qt框架中的一个类,用于创建下拉列表框。它提供了一种用户界面元素,允许用户从预定义的选项列表中选择一个或多个选项。QComboBox通常与用户交互,并且可以显示当前选择的选项。
通过实例化QComboBox对象并使用addItem方法,可以向下拉列表框中添加选项。每个选项由文本标签和关联的值组成。用户可以通过下拉列表框来浏览和选择这些选项。
QComboBox提供了一些方法和信号,可以对选择的选项进行操作,例如获取当前选择的选项、设置默认选项、监听选项变化等。通过使用QComboBox,可以方便地创建具有选项选择功能的用户界面组件。
代码如下:
def createThemeBox(self):
themeComboBox = QComboBox()
themeComboBox.addItem("Light", QChart.ChartThemeLight)
themeComboBox.addItem("Blue Cerulean", QChart.ChartThemeBlueCerulean)
themeComboBox.addItem("Dark", QChart.ChartThemeDark)
themeComboBox.addItem("Brown Sand", QChart.ChartThemeBrownSand)
themeComboBox.addItem("Blue NCS", QChart.ChartThemeBlueNcs)
themeComboBox.addItem("High Contrast", QChart.ChartThemeHighContrast)
themeComboBox.addItem("Blue Icy", QChart.ChartThemeBlueIcy)
return themeComboBox
def createAnimationBox(self):
animationComboBox = QComboBox()
animationComboBox.addItem("No Animations", QChart.NoAnimation)
animationComboBox.addItem("GridAxis Animations", QChart.GridAxisAnimations)
animationComboBox.addItem("Series Animations", QChart.SeriesAnimations)
animationComboBox.addItem("All Animations", QChart.AllAnimations)
return animationComboBox
def createLegendBox(self):
legendComboBox = QComboBox()
legendComboBox.addItem("No Legend ", 0)
legendComboBox.addItem("Legend Top", Qt.AlignTop)
legendComboBox.addItem("Legend Bottom", Qt.AlignBottom)
legendComboBox.addItem("Legend Left", Qt.AlignLeft)
legendComboBox.addItem("Legend Right", Qt.AlignRight)
return legendComboBox
createAreaChart() 函数创建一个面积图表(Area chart):
createBarChart() 函数创建一个堆叠条形图(Stacked bar chart)。
createLineChart() 函数创建一个折线图(Line chart)
代码如下:
def createAreaChart(self):
#
chart = QChart()
chart.setTitle("Area chart")
# The lower series is initialized to zero values.
lowerSeries = None
y_points = []
for i, data_list in enumerate(self.m_dataTable):
# 遍历每一批点。
upperSeries = QLineSeries(chart)
for j, (value, _) in enumerate(data_list):
y = value.y()
if lowerSeries is None:
upperSeries.append(QPointF(j, y))
y_points.append(y)
else:
new_y = y_points[i] + y
upperSeries.append(QPointF(j, new_y))
y_points[j] += new_y
area = QAreaSeries(upperSeries, lowerSeries)
area.setName("Series " + str(i))
chart.addSeries(area)
lowerSeries = upperSeries
chart.createDefaultAxes()
return chart
def createBarChart(self, valueCount):
chart = QChart()
chart.setTitle("Bar chart")
series = QStackedBarSeries(chart)
for i, data_list in enumerate(self.m_dataTable):
set = QBarSet("Bar set " + str(i))
for value, _ in data_list:
set << value.y()
series.append(set)
chart.addSeries(series)
chart.createDefaultAxes()
return chart
def createLineChart(self):
chart = QChart()
chart.setTitle("Line chart")
for i, data_list in enumerate(self.m_dataTable):
series = QLineSeries(chart)
for value, _ in data_list:
series.append(value)
series.setName("Series " + str(i))
# 添加每一批点
chart.addSeries(series)
# 用于创建默认坐标轴的方法,会自动根据添加到图中的数据系列的范围和类型生成适当的坐标轴。
chart.createDefaultAxes()
return chart
createPieChart 方法用于创建饼图。
createSplineChart 方法用于创建样条图。
createScatterChart 方法用于创建散点图。
如下为代码:
def createPieChart(self):
chart = QChart()
chart.setTitle("Pie chart")
pieSize = 1.0 / len(self.m_dataTable)
for i, data_list in enumerate(self.m_dataTable):
series = QPieSeries(chart)
for value, label in data_list:
slice = series.append(label, value.y())
if series.count() == 1:
slice.setLabelVisible()
slice.setExploded()
hPos = (pieSize / 2) + (i / float(len(self.m_dataTable)))
series.setPieSize(pieSize)
series.setHorizontalPosition(hPos)
series.setVerticalPosition(0.5)
chart.addSeries(series)
return chart
def createSplineChart(self):
chart = QChart()
chart.setTitle("Spline chart")
for i, data_list in enumerate(self.m_dataTable):
series = QSplineSeries(chart)
for value, _ in data_list:
series.append(value)
series.setName("Series " + str(i))
chart.addSeries(series)
chart.createDefaultAxes()
return chart
def createScatterChart(self):
chart = QChart()
chart.setTitle("Scatter chart")
for i, data_list in enumerate(self.m_dataTable):
series = QScatterSeries(chart)
for value, _ in data_list:
series.append(value)
series.setName("Series " + str(i))
chart.addSeries(series)
chart.createDefaultAxes()
return chart
使用PyQt库创建图表的应用程序中的一个槽函数(slot function)。槽函数通常用于响应特定的信号(signal),在这种情况下,该函数与某个用户界面事件关联,例如按钮点击或下拉列表选择变化。
# 槽函数被注释为pyqtSlot,用于指定该函数为pyqt的槽函数。
@pyqtSlot()
def updateUI(self):
# 从组合框中获取到当前选中的数据
theme = self.m_themeComboBox.itemData(
self.m_themeComboBox.currentIndex())
# 检查第一个图表的主题是否与theme变量的值不同。
if self.m_charts[0].chart().theme() != theme:
# 循环遍历m_charts图表,为每个图表设置主题。
for chartView in self.m_charts:
chartView.chart().setTheme(QChart.ChartTheme(theme))
# 获取当前窗口的调色板。
pal = self.window().palette()
# 根据主题的不同,设置调色板中窗口和窗口文本的颜色,主题不同,使用的颜色方案不同。
if theme == QChart.ChartThemeLight:
pal.setColor(QPalette.Window, QColor(0xf0f0f0))
pal.setColor(QPalette.WindowText, QColor(0x404044))
elif theme == QChart.ChartThemeDark:
pal.setColor(QPalette.Window, QColor(0x121218))
pal.setColor(QPalette.WindowText, QColor(0xd6d6d6))
elif theme == QChart.ChartThemeBlueCerulean:
pal.setColor(QPalette.Window, QColor(0x40434a))
pal.setColor(QPalette.WindowText, QColor(0xd6d6d6))
elif theme == QChart.ChartThemeBrownSand:
pal.setColor(QPalette.Window, QColor(0x9e8965))
pal.setColor(QPalette.WindowText, QColor(0x404044))
elif theme == QChart.ChartThemeBlueNcs:
pal.setColor(QPalette.Window, QColor(0x018bba))
pal.setColor(QPalette.WindowText, QColor(0x404044))
elif theme == QChart.ChartThemeHighContrast:
pal.setColor(QPalette.Window, QColor(0xffab03))
pal.setColor(QPalette.WindowText, QColor(0x181818))
elif theme == QChart.ChartThemeBlueIcy:
pal.setColor(QPalette.Window, QColor(0xcee7f0))
pal.setColor(QPalette.WindowText, QColor(0x404044))
else:
pal.setColor(QPalette.Window, QColor(0xf0f0f0))
pal.setColor(QPalette.WindowText, QColor(0x404044))
# 将更新后的调色版应用于窗口
self.window().setPalette(pal)
# 检测复选框是否被选中,将结果赋值给checked变量
checked = self.m_antialiasCheckBox.isChecked()
# 为每个图标设置抗锯齿渲染
for chartView in self.m_charts:
chartView.setRenderHint(QPainter.Antialiasing, checked)
# 获取到组合框中当前项的数值,并将其转换为其他数据类型。
options = QChart.AnimationOptions(
self.m_animatedComboBox.itemData(
self.m_animatedComboBox.currentIndex()))
# 检查m_charts,把其动画选项设置为对应的参数。
if self.m_charts[0].chart().animationOptions() != options:
for chartView in self.m_charts:
chartView.chart().setAnimationOptions(options)
# 设置m_legendComboBox的对其。
alignment = self.m_legendComboBox.itemData(
self.m_legendComboBox.currentIndex())
for chartView in self.m_charts:
legend = chartView.chart().legend()
if alignment == 0:
legend.hide()
else:
legend.setAlignment(Qt.Alignment(alignment))
legend.show()
GitHub项目:各种各样的PyQt测试和例子
人与人的悲欢离合不尽相同。