forked from PyQt5/PyQt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HorizontalBarChart.py
80 lines (69 loc) · 2.16 KB
/
HorizontalBarChart.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Created on 2019/10/2
@author: Irony
@site: https://pyqt5.com , https://github.com/892768447
@email: [email protected]
@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_())