首页 > 其他分享 >绘制背景颜色等问题

绘制背景颜色等问题

时间:2023-05-15 20:12:39浏览次数:45  
标签:QColor __ plotAreaGradient 颜色 背景 axisX chart axisY 绘制

import sys
from PyQt5.QtCore import Qt,QPointF
from PyQt5.QtGui import (QPainter, QPen, QColor, QFont, QBrush,
QLinearGradient, QGradient)
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.QtChart import QChart, QChartView, QLineSeries, QCategoryAxis

class DemoCustomChart(QMainWindow):
def __init__(self, parent=None):
super(DemoCustomChart, self).__init__(parent)

# 设置窗口标题
self.setWindowTitle('实战 Qt for Python: 定制图表演示')
# 设置窗口大小
self.resize(480, 360)

self.createChart()

def createChart(self):

#设置饼图数据
lineSeries = QLineSeries()
lineSeries << QPointF(0, 6) << QPointF(9, 4) << QPointF(15, 20) << QPointF(25, 12) << QPointF(29, 26)

#创建图表
chart = QChart()
chart.legend().hide()
chart.addSeries(lineSeries)

#=== 图表外观部分===#
#设置画笔,改变线的颜色和线宽
pen = QPen(QColor(0xfdb157))
pen.setWidth(5)
lineSeries.setPen(pen)

#定制图表标题
font = QFont()
font.setPixelSize(18)
chart.setTitleFont(font)
chart.setTitleBrush(QBrush(Qt.white))
chart.setTitle('定制图表示例')

#定制图表背景
bkGradient = QLinearGradient()
bkGradient.setStart(QPointF(0, 0))
bkGradient.setFinalStop(QPointF(0, 1))
bkGradient.setColorAt(0.0, QColor(0xd2d0d1))
bkGradient.setColorAt(1.0, QColor(0x4c4547))
bkGradient.setCoordinateMode(QGradient.ObjectBoundingMode)
chart.setBackgroundBrush(bkGradient)

#定制图表绘图区域的背景
plotAreaGradient = QLinearGradient()
plotAreaGradient.setStart(QPointF(0, 1))
plotAreaGradient.setFinalStop(QPointF(1, 0))
plotAreaGradient.setColorAt(0.0, QColor(0x555555))
plotAreaGradient.setColorAt(1.0, QColor(0x55aa55))
plotAreaGradient.setCoordinateMode(QGradient.ObjectBoundingMode)
chart.setPlotAreaBackgroundBrush(plotAreaGradient)
chart.setPlotAreaBackgroundVisible(True)

#=== 坐标轴部分 ===#
axisX = QCategoryAxis()
axisY = QCategoryAxis()

#定制坐标标签字体
labelsFont = QFont()
labelsFont.setPixelSize(12)
axisX.setLabelsFont(labelsFont)
axisY.setLabelsFont(labelsFont)

#定制坐标轴的颜色
axisPen = QPen(QColor(0xd18952))
axisPen.setWidth(2)
axisX.setLinePen(axisPen)
axisY.setLinePen(axisPen)

#定制坐标标签文字的颜色
axisBrush = QBrush(Qt.white)
axisX.setLabelsBrush(axisBrush)
axisY.setLabelsBrush(axisBrush)

#定制网格线和它的阴影
axisX.setGridLineVisible(False)
axisY.setGridLineVisible(False)
axisY.setShadesPen(QPen(Qt.NoPen))
axisY.setShadesBrush(QBrush(QColor(0x99, 0xcc, 0xcc, 0x55)))
axisY.setShadesVisible(True)

#为坐标轴设置刻度并添加到图表中
axisX.append('低', 10)
axisX.append('最理想', 20)
axisX.append('高', 30)

axisY.append('慢速', 10)
axisY.append('中速', 20)
axisY.append('快速', 30)
chart.addAxis(axisX, Qt.AlignBottom)
chart.addAxis(axisY, Qt.AlignLeft)
lineSeries.attachAxis(axisX)
lineSeries.attachAxis(axisY)

#图表视图
chartView = QChartView(chart)
chartView.setRenderHint(QPainter.Antialiasing)

self.setCentralWidget(chartView)


if __name__ == '__main__':
app = QApplication(sys.argv)
window = DemoCustomChart()
window.show()
sys.exit(app.exec())

标签:QColor,__,plotAreaGradient,颜色,背景,axisX,chart,axisY,绘制
From: https://www.cnblogs.com/hahaah/p/17402936.html

相关文章