LISHUZUOXUN_yangjiang/LSZXPagesManagerLibrary/echarts_pie.py

81 lines
3.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# coding: gb2312
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
class PieCircleWidget(QWidget):
def __init__(self):
super().__init__()
self.setFixedSize(360, 360)
# 五个等级的数据
self.data_list = []
self.title = None
self.data = []
self.colors = [QColor("#ef6667"), QColor("#fac85d"), QColor("#91cd77"), QColor("#eb7ccc"), QColor("#73c0e0")]
self.texts = ["优秀", "良好", "及格", "不及格", "缺考"] # 新增:每个等级的文字提醒
def set_data(self, data, title):
self.data_list = data
self.title = title
for index, data in enumerate(self.data_list):
param = data / 100
self.data.append(param)
def paintEvent(self, event):
painter = QPainter(self)
painter.setRenderHint(QPainter.Antialiasing)
# 绘制标题
font = QFont("\"Microsoft YaHei UI\"", 14, QFont.Bold)
painter.setFont(font)
painter.setPen(Qt.black)
painter.drawText(20, 30, self.title)
# 圆环的中心
center_x = self.width() / 2
center_y = self.height() / 2
# 外圆半径
outer_radius = min(self.width(), self.height()) / 2 - 60
# 内圆半径(比如外圆半径的 70%
inner_radius = outer_radius * 0.7
start_angle = 0
for i, portion in enumerate(self.data):
painter.setPen(Qt.NoPen)
angle = 360 * portion
painter.setBrush(self.colors[i])
painter.drawPie(int(center_x - outer_radius), int(center_y - outer_radius), int(2 * outer_radius),
int(2 * outer_radius), int(start_angle * 16), int(angle * 16))
painter.setFont(QFont("\"Microsoft YaHei UI\"", 10))
painter.setPen(Qt.black) # 设置扇形文字颜色为黑色
start_angle += angle
painter.setPen(Qt.NoPen)
# 绘制内圆
painter.setBrush(Qt.white)
painter.drawEllipse(int(center_x - inner_radius), int(center_y - inner_radius),
int(2 * inner_radius), int(2 * inner_radius))
# 绘制横向排列的图例
legend_x = 60
legend_y = self.height() - 40 # 将图例放在底部
for i, label in enumerate(self.texts):
painter.setPen(Qt.NoPen)
painter.setBrush(self.colors[i])
# 判断图例是否是最后一项,调整位置,如果是最后一项,要往后挪一点位置
if i == len(self.texts)-1:
painter.drawRect(int(legend_x + i * 53), legend_y, 15, 15) # 横向排列,间隔 50 像素
painter.setFont(QFont("\"Microsoft YaHei UI\"", 10))
painter.setPen(Qt.black) # 设置图例文字颜色为黑色
painter.drawText(int(legend_x + i * 50 + 32), legend_y + 12, label) # 文字跟随
else:
painter.drawRect(int(legend_x + i * 50), legend_y, 15, 15) # 横向排列,间隔 50 像素
painter.setFont(QFont("\"Microsoft YaHei UI\"", 10))
painter.setPen(Qt.black) # 设置图例文字颜色为黑色
painter.drawText(int(legend_x + i * 50 + 20), legend_y + 12, label) # 文字跟随