-
-
Notifications
You must be signed in to change notification settings - Fork 2k
/
PieChart.py
65 lines (54 loc) · 1.61 KB
/
PieChart.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Created on 2019/10/2
@author: Irony
@site: https://pyqt.site , https://github.com/PyQt5
@email: [email protected]
@file: PieChart
@description: 饼状图表
"""
try:
from PyQt5.QtChart import QChartView, QChart, QPieSeries
from PyQt5.QtGui import QPainter, QColor
from PyQt5.QtWidgets import QApplication
except ImportError:
from PySide2.QtGui import QPainter, QColor
from PySide2.QtWidgets import QApplication
from PySide2.QtCharts import QtCharts
QChartView = QtCharts.QChartView
QChart = QtCharts.QChart
QPieSeries = QtCharts.QPieSeries
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
app = QApplication(sys.argv)
w = Window()
w.show()
sys.exit(app.exec_())