增加Qt Chart例子

This commit is contained in:
Irony 2019-10-02 14:15:37 +08:00
parent 1dfc61ddbb
commit b8fe2b9727
16 changed files with 556 additions and 0 deletions

75
QChart/AreaChart.py Normal file
View file

@ -0,0 +1,75 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Created on 2019/10/2
@author: Irony
@site: https://pyqt5.com , https://github.com/892768447
@email: 892768447@qq.com
@file: AreaChart
@description: 区域图表
"""
from PyQt5.QtChart import QChartView, QChart, QLineSeries, QAreaSeries
from PyQt5.QtCore import QPointF
from PyQt5.QtGui import QColor, QGradient, QLinearGradient, QPainter, QPen
class Window(QChartView):
def __init__(self, *args, **kwargs):
super(Window, self).__init__(*args, **kwargs)
self.resize(400, 300)
# 抗锯齿
self.setRenderHint(QPainter.Antialiasing)
# 图表
chart = QChart()
self.setChart(chart)
# 设置标题
chart.setTitle('Simple areachart example')
# 添加Series
chart.addSeries(self.getSeries())
# 创建默认轴线
chart.createDefaultAxes()
# 设置xy轴的范围
chart.axisX().setRange(0, 20)
chart.axisY().setRange(0, 10)
def getSeries(self):
# 创建Series
series0 = QLineSeries(self)
series1 = QLineSeries(self)
# 添加数据
series0 << QPointF(1, 5) << QPointF(3, 7) << QPointF(7, 6) << QPointF(9, 7) \
<< QPointF(12, 6) << QPointF(16, 7) << QPointF(18, 5)
series1 << QPointF(1, 3) << QPointF(3, 4) << QPointF(7, 3) << QPointF(8, 2) \
<< QPointF(12, 3) << QPointF(16, 4) << QPointF(18, 3)
# 创建区域图
series = QAreaSeries(series0, series1)
series.setName('Batman')
# 画笔
pen = QPen(0x059605)
pen.setWidth(3)
series.setPen(pen)
# 设置画刷
gradient = QLinearGradient(QPointF(0, 0), QPointF(0, 1))
gradient.setColorAt(0.0, QColor(0x3cc63c))
gradient.setColorAt(1.0, QColor(0x26f626))
gradient.setCoordinateMode(QGradient.ObjectBoundingMode)
series.setBrush(gradient)
return series
if __name__ == '__main__':
import sys
from PyQt5.QtWidgets import QApplication
app = QApplication(sys.argv)
w = Window()
w.show()
sys.exit(app.exec_())

80
QChart/BarChart.py Normal file
View file

@ -0,0 +1,80 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Created on 2019/10/2
@author: Irony
@site: https://pyqt5.com , https://github.com/892768447
@email: 892768447@qq.com
@file: BarChart
@description: 柱状图表
"""
from PyQt5.QtChart import QChartView, QChart, QBarSet, QBarSeries, QBarCategoryAxis
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPainter
class Window(QChartView):
def __init__(self, *args, **kwargs):
super(Window, self).__init__(*args, **kwargs)
self.resize(400, 300)
# 抗锯齿
self.setRenderHint(QPainter.Antialiasing)
# 图表
chart = QChart()
self.setChart(chart)
# 设置标题
chart.setTitle('Simple barchart example')
# 开启动画效果
chart.setAnimationOptions(QChart.SeriesAnimations)
# 添加Series
series = self.getSeries()
chart.addSeries(series)
# 分类
categories = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun']
# 分类x轴
axis = QBarCategoryAxis()
axis.append(categories)
# 创建默认轴线
chart.createDefaultAxes()
# 替换默认x轴
chart.setAxisX(axis, series)
# 显示图例
chart.legend().setVisible(True)
chart.legend().setAlignment(Qt.AlignBottom)
def getSeries(self):
# 创建5个柱子
set0 = QBarSet('Jane')
set1 = QBarSet('John')
set2 = QBarSet('Axel')
set3 = QBarSet('Mary')
set4 = QBarSet('Samantha')
# 添加数据
set0 << 1 << 2 << 3 << 4 << 5 << 6
set1 << 5 << 0 << 0 << 4 << 0 << 7
set2 << 3 << 5 << 8 << 13 << 8 << 5
set3 << 5 << 6 << 7 << 3 << 4 << 5
set4 << 9 << 7 << 5 << 3 << 1 << 2
# 创建柱状条
series = QBarSeries()
series.append(set0)
series.append(set1)
series.append(set2)
series.append(set3)
series.append(set4)
return series
if __name__ == '__main__':
import sys
from PyQt5.QtWidgets import QApplication
app = QApplication(sys.argv)
w = Window()
w.show()
sys.exit(app.exec_())

View file

@ -0,0 +1,80 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Created on 2019/10/2
@author: Irony
@site: https://pyqt5.com , https://github.com/892768447
@email: 892768447@qq.com
@file: HorizontalBarChart
@description: 横向柱状图表
"""
from PyQt5.QtChart import QChartView, QChart, QBarSet, QHorizontalBarSeries, QBarCategoryAxis
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPainter
class Window(QChartView):
def __init__(self, *args, **kwargs):
super(Window, self).__init__(*args, **kwargs)
self.resize(400, 300)
# 抗锯齿
self.setRenderHint(QPainter.Antialiasing)
# 图表
chart = QChart()
self.setChart(chart)
# 设置标题
chart.setTitle('Simple horizontal barchart example')
# 开启动画效果
chart.setAnimationOptions(QChart.SeriesAnimations)
# 添加Series
series = self.getSeries()
chart.addSeries(series)
# 分类
categories = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun']
# 分类x轴
axis = QBarCategoryAxis()
axis.append(categories)
# 创建默认轴线
chart.createDefaultAxes()
# 替换默认y轴
chart.setAxisY(axis, series)
# 显示图例
chart.legend().setVisible(True)
chart.legend().setAlignment(Qt.AlignBottom)
def getSeries(self):
# 创建5个柱子
set0 = QBarSet('Jane')
set1 = QBarSet('John')
set2 = QBarSet('Axel')
set3 = QBarSet('Mary')
set4 = QBarSet('Samantha')
# 添加数据
set0 << 1 << 2 << 3 << 4 << 5 << 6
set1 << 5 << 0 << 0 << 4 << 0 << 7
set2 << 3 << 5 << 8 << 13 << 8 << 5
set3 << 5 << 6 << 7 << 3 << 4 << 5
set4 << 9 << 7 << 5 << 3 << 1 << 2
# 创建柱状条
series = QHorizontalBarSeries()
series.append(set0)
series.append(set1)
series.append(set2)
series.append(set3)
series.append(set4)
return series
if __name__ == '__main__':
import sys
from PyQt5.QtWidgets import QApplication
app = QApplication(sys.argv)
w = Window()
w.show()
sys.exit(app.exec_())

View file

@ -0,0 +1,80 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Created on 2019/10/2
@author: Irony
@site: https://pyqt5.com , https://github.com/892768447
@email: 892768447@qq.com
@file: HorizontalPercentBarChart
@description: 横向百分比柱状图表
"""
from PyQt5.QtChart import QChartView, QChart, QBarSet, QHorizontalPercentBarSeries, QBarCategoryAxis
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPainter
class Window(QChartView):
def __init__(self, *args, **kwargs):
super(Window, self).__init__(*args, **kwargs)
self.resize(400, 300)
# 抗锯齿
self.setRenderHint(QPainter.Antialiasing)
# 图表
chart = QChart()
self.setChart(chart)
# 设置标题
chart.setTitle('Simple horizontal percent barchart example')
# 开启动画效果
chart.setAnimationOptions(QChart.SeriesAnimations)
# 添加Series
series = self.getSeries()
chart.addSeries(series)
# 分类
categories = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun']
# 分类x轴
axis = QBarCategoryAxis()
axis.append(categories)
# 创建默认轴线
chart.createDefaultAxes()
# 替换默认y轴
chart.setAxisY(axis, series)
# 显示图例
chart.legend().setVisible(True)
chart.legend().setAlignment(Qt.AlignBottom)
def getSeries(self):
# 创建5个柱子
set0 = QBarSet('Jane')
set1 = QBarSet('John')
set2 = QBarSet('Axel')
set3 = QBarSet('Mary')
set4 = QBarSet('Samantha')
# 添加数据
set0 << 1 << 2 << 3 << 4 << 5 << 6
set1 << 5 << 0 << 0 << 4 << 0 << 7
set2 << 3 << 5 << 8 << 13 << 8 << 5
set3 << 5 << 6 << 7 << 3 << 4 << 5
set4 << 9 << 7 << 5 << 3 << 1 << 2
# 创建柱状条
series = QHorizontalPercentBarSeries()
series.append(set0)
series.append(set1)
series.append(set2)
series.append(set3)
series.append(set4)
return series
if __name__ == '__main__':
import sys
from PyQt5.QtWidgets import QApplication
app = QApplication(sys.argv)
w = Window()
w.show()
sys.exit(app.exec_())

80
QChart/PercentBarChart.py Normal file
View file

@ -0,0 +1,80 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Created on 2019/10/2
@author: Irony
@site: https://pyqt5.com , https://github.com/892768447
@email: 892768447@qq.com
@file: PercentBarChart
@description: 百分比柱状图表
"""
from PyQt5.QtChart import QChartView, QChart, QBarSet, QPercentBarSeries, QBarCategoryAxis
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPainter
class Window(QChartView):
def __init__(self, *args, **kwargs):
super(Window, self).__init__(*args, **kwargs)
self.resize(400, 300)
# 抗锯齿
self.setRenderHint(QPainter.Antialiasing)
# 图表
chart = QChart()
self.setChart(chart)
# 设置标题
chart.setTitle('Simple percentbarchart example')
# 开启动画效果
chart.setAnimationOptions(QChart.SeriesAnimations)
# 添加Series
series = self.getSeries()
chart.addSeries(series)
# 分类
categories = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun']
# 分类x轴
axis = QBarCategoryAxis()
axis.append(categories)
# 创建默认轴线
chart.createDefaultAxes()
# 替换默认x轴
chart.setAxisX(axis, series)
# 显示图例
chart.legend().setVisible(True)
chart.legend().setAlignment(Qt.AlignBottom)
def getSeries(self):
# 创建5个柱子
set0 = QBarSet('Jane')
set1 = QBarSet('John')
set2 = QBarSet('Axel')
set3 = QBarSet('Mary')
set4 = QBarSet('Samantha')
# 添加数据
set0 << 1 << 2 << 3 << 4 << 5 << 6
set1 << 5 << 0 << 0 << 4 << 0 << 7
set2 << 3 << 5 << 8 << 13 << 8 << 5
set3 << 5 << 6 << 7 << 3 << 4 << 5
set4 << 9 << 7 << 5 << 3 << 1 << 2
# 创建柱状条
series = QPercentBarSeries()
series.append(set0)
series.append(set1)
series.append(set2)
series.append(set3)
series.append(set4)
return series
if __name__ == '__main__':
import sys
from PyQt5.QtWidgets import QApplication
app = QApplication(sys.argv)
w = Window()
w.show()
sys.exit(app.exec_())

55
QChart/PieChart.py Normal file
View file

@ -0,0 +1,55 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Created on 2019/10/2
@author: Irony
@site: https://pyqt5.com , https://github.com/892768447
@email: 892768447@qq.com
@file: PieChart
@description: 饼状图表
"""
from PyQt5.QtChart import QChartView, QChart, QPieSeries
from PyQt5.QtGui import QPainter, QColor
class Window(QChartView):
def __init__(self, *args, **kwargs):
super(Window, self).__init__(*args, **kwargs)
self.resize(400, 300)
# 抗锯齿
self.setRenderHint(QPainter.Antialiasing)
# 图表
chart = QChart()
self.setChart(chart)
# 设置标题
chart.setTitle('Simple piechart example')
# 添加Series
chart.addSeries(self.getSeries())
def getSeries(self):
series = QPieSeries()
slice0 = series.append('10%', 1)
series.append('20%', 2)
series.append('70%', 7)
# 显示label文字
series.setLabelsVisible()
series.setPieSize(0.5)
# 使第一块突出显示
slice0.setLabelVisible()
slice0.setExploded()
# 设置第一块颜色
slice0.setColor(QColor(255, 0, 0, 100))
return series
if __name__ == '__main__':
import sys
from PyQt5.QtWidgets import QApplication
app = QApplication(sys.argv)
w = Window()
w.show()
sys.exit(app.exec_())

View file

@ -7,6 +7,13 @@
- [LineChart自定义xy轴](#4LineChart自定义xy轴) - [LineChart自定义xy轴](#4LineChart自定义xy轴)
- [ToolTip提示](#5ToolTip提示) - [ToolTip提示](#5ToolTip提示)
- [动态曲线图](#6动态曲线图) - [动态曲线图](#6动态曲线图)
- [区域图表](#7区域图表)
- [柱状图表](#8柱状图表)
- [饼状图表](#9饼状图表)
- [样条图表](#10样条图表)
- [百分比柱状图表](#11百分比柱状图表)
- [横向柱状图表](#12横向柱状图表)
- [横向百分比柱状图表](#13横向百分比柱状图表)
## 1、折线图 ## 1、折线图
[运行 LineChart.py](LineChart.py) [运行 LineChart.py](LineChart.py)
@ -41,3 +48,38 @@
[运行 DynamicSpline.py](DynamicSpline.py) [运行 DynamicSpline.py](DynamicSpline.py)
![DynamicSpline](ScreenShot/DynamicSplineChart.gif) ![DynamicSpline](ScreenShot/DynamicSplineChart.gif)
## 7、区域图表
[运行 AreaChart.py](AreaChart.py)
![AreaChart](ScreenShot/AreaChart.png)
## 8、柱状图表
[运行 BarChart.py](BarChart.py)
![BarChart](ScreenShot/BarChart.png)
## 9、饼状图表
[运行 PieChart.py](PieChart.py)
![PieChart](ScreenShot/PieChart.png)
## 10、样条图表
[运行 SplineChart.py](SplineChart.py)
![SplineChart](ScreenShot/SplineChart.png)
## 11、百分比柱状图表
[运行 PercentBarChart.py](PercentBarChart.py)
![PercentBarChart](ScreenShot/PercentBarChart.png)
## 12、横向柱状图表
[运行 HorizontalBarChart.py](HorizontalBarChart.py)
![HorizontalBarChart](ScreenShot/HorizontalBarChart.png)
## 13、横向百分比柱状图表
[运行 HorizontalPercentBarChart.py](HorizontalPercentBarChart.py)
![HorizontalPercentBarChart](ScreenShot/HorizontalPercentBarChart.png)

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

57
QChart/SplineChart.py Normal file
View file

@ -0,0 +1,57 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Created on 2019/10/2
@author: Irony
@site: https://pyqt5.com , https://github.com/892768447
@email: 892768447@qq.com
@file: SplineChart
@description: 样条图表
"""
from PyQt5.QtChart import QChartView, QChart, QSplineSeries
from PyQt5.QtCore import QPointF
from PyQt5.QtGui import QPainter
class Window(QChartView):
def __init__(self, *args, **kwargs):
super(Window, self).__init__(*args, **kwargs)
self.resize(400, 300)
# 抗锯齿
self.setRenderHint(QPainter.Antialiasing)
# 图表
chart = QChart()
self.setChart(chart)
# 设置标题
chart.setTitle('Simple splinechart example')
# 添加Series
self.getSeries(chart)
# 创建默认xy轴
chart.createDefaultAxes()
chart.legend().setVisible(False)
def getSeries(self, chart):
# 第一组
series = QSplineSeries(chart)
series << QPointF(1, 5) << QPointF(3, 7) << QPointF(7, 6) << QPointF(9, 7) \
<< QPointF(12, 6) << QPointF(16, 7) << QPointF(18, 5)
chart.addSeries(series)
# 第二组
series = QSplineSeries(chart)
series << QPointF(1, 3) << QPointF(3, 4) << QPointF(7, 3) << QPointF(8, 2) \
<< QPointF(12, 3) << QPointF(16, 4) << QPointF(18, 3)
chart.addSeries(series)
if __name__ == '__main__':
import sys
from PyQt5.QtWidgets import QApplication
app = QApplication(sys.argv)
w = Window()
w.show()
sys.exit(app.exec_())

View file

@ -158,6 +158,13 @@ https://pyqt5.com 社区是专门针对PyQt5学习和提升开设的博客网站
- [LineChart自定义xy轴](QChart/CustomXYaxis.py) - [LineChart自定义xy轴](QChart/CustomXYaxis.py)
- [ToolTip提示](QChart/ToolTip.py) - [ToolTip提示](QChart/ToolTip.py)
- [DynamicSpline动态曲线图](QChart/DynamicSpline.py) - [DynamicSpline动态曲线图](QChart/DynamicSpline.py)
- [区域图表](QChart/AreaChart.py)
- [柱状图表](QChart/BarChart.py)
- [饼状图表](QChart/PieChart.py)
- [样条图表](QChart/SplineChart.py)
- [百分比柱状图表](QChart/PercentBarChart.py)
- [横向柱状图表](QChart/HorizontalBarChart.py)
- [横向百分比柱状图表](QChart/HorizontalPercentBarChart.py)
- [PyQtGraph](PyQtGraph) - [PyQtGraph](PyQtGraph)
- [鼠标获取X轴坐标](PyQtGraph/mouseFlow.py) - [鼠标获取X轴坐标](PyQtGraph/mouseFlow.py)