diff --git a/QChart/AreaChart.py b/QChart/AreaChart.py new file mode 100644 index 0000000..9e2d022 --- /dev/null +++ b/QChart/AreaChart.py @@ -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_()) diff --git a/QChart/BarChart.py b/QChart/BarChart.py new file mode 100644 index 0000000..73bd762 --- /dev/null +++ b/QChart/BarChart.py @@ -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_()) diff --git a/QChart/HorizontalBarChart.py b/QChart/HorizontalBarChart.py new file mode 100644 index 0000000..1f4000d --- /dev/null +++ b/QChart/HorizontalBarChart.py @@ -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_()) diff --git a/QChart/HorizontalPercentBarChart.py b/QChart/HorizontalPercentBarChart.py new file mode 100644 index 0000000..6f807e2 --- /dev/null +++ b/QChart/HorizontalPercentBarChart.py @@ -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_()) diff --git a/QChart/PercentBarChart.py b/QChart/PercentBarChart.py new file mode 100644 index 0000000..1cd4d06 --- /dev/null +++ b/QChart/PercentBarChart.py @@ -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_()) diff --git a/QChart/PieChart.py b/QChart/PieChart.py new file mode 100644 index 0000000..f3bb7a0 --- /dev/null +++ b/QChart/PieChart.py @@ -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_()) diff --git a/QChart/README.md b/QChart/README.md index 13577ea..f7702bf 100644 --- a/QChart/README.md +++ b/QChart/README.md @@ -7,6 +7,13 @@ - [LineChart自定义xy轴](#4LineChart自定义xy轴) - [ToolTip提示](#5ToolTip提示) - [动态曲线图](#6动态曲线图) + - [区域图表](#7区域图表) + - [柱状图表](#8柱状图表) + - [饼状图表](#9饼状图表) + - [样条图表](#10样条图表) + - [百分比柱状图表](#11百分比柱状图表) + - [横向柱状图表](#12横向柱状图表) + - [横向百分比柱状图表](#13横向百分比柱状图表) ## 1、折线图 [运行 LineChart.py](LineChart.py) @@ -41,3 +48,38 @@ [运行 DynamicSpline.py](DynamicSpline.py) ![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) diff --git a/QChart/ScreenShot/AreaChart.png b/QChart/ScreenShot/AreaChart.png new file mode 100644 index 0000000..701c436 Binary files /dev/null and b/QChart/ScreenShot/AreaChart.png differ diff --git a/QChart/ScreenShot/BarChart.png b/QChart/ScreenShot/BarChart.png new file mode 100644 index 0000000..182e1f4 Binary files /dev/null and b/QChart/ScreenShot/BarChart.png differ diff --git a/QChart/ScreenShot/HorizontalBarChart.png b/QChart/ScreenShot/HorizontalBarChart.png new file mode 100644 index 0000000..5e4830a Binary files /dev/null and b/QChart/ScreenShot/HorizontalBarChart.png differ diff --git a/QChart/ScreenShot/HorizontalPercentBarChart.png b/QChart/ScreenShot/HorizontalPercentBarChart.png new file mode 100644 index 0000000..554e8ae Binary files /dev/null and b/QChart/ScreenShot/HorizontalPercentBarChart.png differ diff --git a/QChart/ScreenShot/PercentBarChart.png b/QChart/ScreenShot/PercentBarChart.png new file mode 100644 index 0000000..d6cf331 Binary files /dev/null and b/QChart/ScreenShot/PercentBarChart.png differ diff --git a/QChart/ScreenShot/PieChart.png b/QChart/ScreenShot/PieChart.png new file mode 100644 index 0000000..4fc450a Binary files /dev/null and b/QChart/ScreenShot/PieChart.png differ diff --git a/QChart/ScreenShot/SplineChart.png b/QChart/ScreenShot/SplineChart.png new file mode 100644 index 0000000..170eb92 Binary files /dev/null and b/QChart/ScreenShot/SplineChart.png differ diff --git a/QChart/SplineChart.py b/QChart/SplineChart.py new file mode 100644 index 0000000..69a5436 --- /dev/null +++ b/QChart/SplineChart.py @@ -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_()) diff --git a/README.md b/README.md index 2bb5bcf..deefdb4 100644 --- a/README.md +++ b/README.md @@ -158,6 +158,13 @@ https://pyqt5.com 社区是专门针对PyQt5学习和提升开设的博客网站 - [LineChart自定义xy轴](QChart/CustomXYaxis.py) - [ToolTip提示](QChart/ToolTip.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) - [鼠标获取X轴坐标](PyQtGraph/mouseFlow.py)