76 lines
3.3 KiB
Python
76 lines
3.3 KiB
Python
# coding: gb2312
|
|
from PyQt5.QtWidgets import QApplication, QMainWindow
|
|
from PyQt5.QtGui import QPainter, QColor, QFont
|
|
from PyQt5.QtCore import Qt
|
|
|
|
class MainWindow(QMainWindow):
|
|
def __init__(self):
|
|
super().__init__()
|
|
|
|
self.resize(800, 200)
|
|
|
|
# 新的日期数据
|
|
self.x = ["2024年4月5日8时", "2024年4月10日8时", "2024年4月13日15时", "2024年4月17日8时", "2024年4月20日15时", "2024年4月24日8时",
|
|
"2024年4月26日15时", "2024年4月30日8时", "2024年5月3日8时", "2024年5月8日8时", "2024年5月10日15时"]
|
|
|
|
self.bar_width = self.width() // len(self.x) - 30 # 根据窗口宽度和柱子数量计算新的柱子宽度
|
|
self.x_offset = 50 # 调整 x 轴起始偏移
|
|
|
|
def resizeEvent(self, event):
|
|
self.height_ratio = self.height() / 100 # 计算高度比例
|
|
|
|
def paintEvent(self, event):
|
|
painter = QPainter(self)
|
|
|
|
y2 = [7.14, 7.14, 7.14, 7.14, 7.14, 7.14, 12.5, 14.29, 14.29, 16.07, 17.86]
|
|
y1 = [42.86, 42.86, 42.86, 42.86, 42.86, 42.86, 37.5, 41.07, 42.86, 41.07, 39.29]
|
|
y3 = [17.86, 17.86, 17.86, 17.86, 17.86, 17.86, 17.86, 12.5, 12.5, 14.29, 16.07]
|
|
y4 = [2.14, 22.14, 32.14, 32.14, 32.14, 32.14, 32.14, 32.14, 30.36, 28.57, 26.79]
|
|
y5 = [30, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0]
|
|
|
|
gap = 15 # 柱子之间的间隔
|
|
|
|
# 绘制 x 轴
|
|
painter.setPen(QColor('black'))
|
|
font = QFont()
|
|
font.setPointSize(10)
|
|
painter.setFont(font)
|
|
for i in range(len(self.x)):
|
|
painter.drawText(self.x_offset - 20 + i * (self.bar_width + gap) + self.bar_width // 2, self.height() - 10, self.x[i])
|
|
|
|
# 绘制 y 轴
|
|
painter.setPen(QColor('black'))
|
|
for y_value in range(0, 101, 20):
|
|
painter.drawText(20, int(self.height() - 40 - y_value * self.height_ratio), str(y_value))
|
|
|
|
for i in range(len(self.x)):
|
|
# 绘制 y1 柱子
|
|
painter.setBrush(QColor('green'))
|
|
painter.drawRect(self.x_offset + i * (self.bar_width + gap), self.height() - int((y1[i] + 20) * self.height_ratio), self.bar_width, int(y1[i] * self.height_ratio))
|
|
|
|
# 绘制 y2 柱子
|
|
painter.setBrush(QColor('blue'))
|
|
painter.drawRect(self.x_offset + i * (self.bar_width + gap), self.height() - int((y1[i] + y2[i]) * self.height_ratio), self.bar_width,
|
|
int(y2[i] * self.height_ratio))
|
|
|
|
# 绘制 y3 柱子
|
|
painter.setBrush(QColor('red'))
|
|
painter.drawRect(self.x_offset + i * (self.bar_width + gap), self.height() - int((y1[i] + y2[i] + y3[i]) * self.height_ratio), self.bar_width,
|
|
int(y3[i] * self.height_ratio))
|
|
|
|
# 绘制 y4 柱子
|
|
painter.setBrush(QColor('yellow'))
|
|
painter.drawRect(self.x_offset + i * (self.bar_width + gap), self.height() - int((y1[i] + y2[i] + y3[i] + y4[i]) * self.height_ratio),
|
|
self.bar_width, int(y4[i] * self.height_ratio))
|
|
|
|
# 绘制 y5 柱子
|
|
painter.setBrush(QColor('purple'))
|
|
painter.drawRect(self.x_offset + i * (self.bar_width + gap),
|
|
self.height() - int((y1[i] + y2[i] + y3[i] + y4[i] + y5[i]) * self.height_ratio), self.bar_width, int(y5[i] * self.height_ratio))
|
|
|
|
|
|
if __name__ == '__main__':
|
|
app = QApplication([])
|
|
window = MainWindow()
|
|
window.show()
|
|
app.exec_() |