赞
踩
默认情况下,图例(legend)在与图表相同的视图内绘制图例。在某些情况下,用户可能希望将图例绘制到其他位置。为此,可以将图例从图表中分离出来(这意味着图表不会绘制图例或尝试更改其布局)。然后可以在用户希望的任何地方(例如,在不同的图形场景中)绘制分离的图例。在QChart中,图例由类QLegend来实现。
QLegend类显示图表的图例。QLegend是显示图表图例的图形对象。当图形序列发生更改时,图例状态由QChart更新。默认情况下,图例附加在图表上,但是可以将其分离以使其独立于图表布局。图例对象无法创建或删除,但可以通过QChart类进行引用。
QLegend常用函数:
QLegend常用信号:
在示例中,我们使用柱形图来演示图例的各种设置效果,在其中添加或删除柱形条。图例反映了图形序列的变化。可以将图例分离或附加回图表,并且可以修改其对齐方式,可以修改图例的标记文字等。断开图例后,可以自由调整其大小和位置。完整代码如下:
- import sys
- from PyQt5.QtCore import Qt, QRectF
- from PyQt5.QtGui import QPainter, QColor, QPen, QBrush
- from PyQt5.QtWidgets import (QApplication, QWidget, QPushButton, QDoubleSpinBox,
- QGridLayout, QFormLayout, QGroupBox)
- from PyQt5.QtChart import QChart, QChartView, QBarSeries, QBarSet
-
- class DemoLegendWidget(QWidget):
- def __init__(self, parent=None):
- super(DemoLegendWidget, self).__init__(parent)
-
- # 设置窗口标题
- self.setWindowTitle('实战 Qt for Python: QChart图例演示')
- # 设置窗口大小
- self.resize(720, 480)
-
- self.initUi()
-
- def initUi(self):
- buttonLayout = QGridLayout()
-
- #图例与图表连接和分离的切换按钮
- btnDetachLegend = QPushButton('分离连接切换')
- btnDetachLegend.clicked.connect(self.toggleAttached)
- buttonLayout.addWidget(btnDetachLegend, 0, 0)
-
- #添加和删除按钮
- btnAddSet = QPushButton('添加柱状条')
- btnAddSet.clicked.connect(self.addBarSet)
- buttonLayout.addWidget(btnAddSet, 2, 0)
- btnRemoveSet = QPushButton('移除柱状条')
- btnRemoveSet.clicked.connect(self.removeBarset)
- buttonLayout.addWidget(btnRemoveSet, 3, 0)
-
- #对齐按钮
- btnAlign = QPushButton('对齐(底端)')
- btnAlign.clicked.connect(self.setLegendAlignment)
- buttonLayout.addWidget(btnAlign, 4, 0)
-
- #字体加黑
- btnBold = QPushButton('加黑字体')
- btnBold.clicked.connect(self.toggleBold)
- buttonLayout.addWidget(btnBold, 8, 0)
-
- #字体变斜
- btnItalic = QPushButton('斜体')
- btnItalic.clicked.connect(self.toggleItalic)
- buttonLayout.addWidget(btnItalic, 9, 0)
-
- #图例的位置参数
- self.legendPosX = QDoubleSpinBox()
- self.legendPosY = QDoubleSpinBox()
- self.legendWidth = QDoubleSpinBox()
- self.legendHeight = QDoubleSpinBox()
- self.legendPosX.valueChanged.connect(self.updateLegendLayout)
- self.legendPosY.valueChanged.connect(self.updateLegendLayout)
- self.legendWidth.valueChanged.connect(self.updateLegendLayout)
- self.legendHeight.valueChanged.connect(self.updateLegendLayout)
-
- legendLayout = QFormLayout()
- legendLayout.addRow('水平位置', self.legendPosX)
- legendLayout.addRow('垂直位置', self.legendPosY)
- legendLayout.addRow('宽度', self.legendWidth)
- legendLayout.addRow('高度', self.legendHeight)
- self.legendSettings = QGroupBox('分离图例')
- self.legendSettings.setLayout(legendLayout)
- buttonLayout.addWidget(self.legendSettings)
- self.legendSettings.setVisible(False)
-
- #创建图表
- self.chart = QChart()
- self.chartView = QChartView(self.chart, self)
-
- #生成修改字体的spinbox
- self.fontSize = QDoubleSpinBox()
- self.fontSize.setValue(self.chart.legend().font().pointSizeF())
- self.fontSize.valueChanged.connect(self.fontSizeChanged)
-
- fontLayout = QFormLayout()
- fontLayout.addRow('图例字体尺寸', self.fontSize)
-
- #主布局
- mainLayout = QGridLayout()
- mainLayout.addLayout(buttonLayout, 0, 0)
- mainLayout.addLayout(fontLayout, 1, 0)
- mainLayout.addWidget(self.chartView, 0, 1, 3, 1)
- self.setLayout(mainLayout)
-
- self.createSeries()
-
- def createSeries(self):
- self.series = QBarSeries()
- self.addBarSet()
- self.addBarSet()
- self.addBarSet()
- self.addBarSet()
-
- self.chart.addSeries(self.series)
- self.chart.setTitle('图例分离样例')
- self.chart.createDefaultAxes()
- self.chart.legend().setVisible(True)
- self.chart.legend().setAlignment(Qt.AlignBottom)
-
- self.chartView.setRenderHint(QPainter.Antialiasing)
-
- def showLegendSpinbox(self):
- self.legendSettings.setVisible(True)
- chartViewRect = self.chart.rect()
-
- self.legendPosX.setMinimum(0)
- self.legendPosX.setMaximum(chartViewRect.width())
- self.legendPosX.setValue(150)
-
- self.legendPosY.setMinimum(0)
- self.legendPosY.setMaximum(chartViewRect.height())
- self.legendPosY.setValue(150)
-
- self.legendWidth.setMinimum(0)
- self.legendWidth.setMaximum(chartViewRect.width())
- self.legendWidth.setValue(150)
-
- self.legendHeight.setMinimum(0)
- self.legendHeight.setMaximum(chartViewRect.height())
- self.legendHeight.setValue(75)
-
- def hideLegendSpinbox(self):
- self.legendSettings.setVisible(False)
-
- def toggleAttached(self):
- legend = self.chart.legend()
- if legend.isAttachedToChart():
- legend.detachFromChart()
- legend.setBackgroundVisible(True)
- legend.setBrush(QBrush(QColor(128, 128, 128, 128)))
- legend.setPen(QPen(QColor(192, 192, 192, 192)))
- self.showLegendSpinbox()
- self.updateLegendLayout()
- else:
- legend.attachToChart()
- legend.setBackgroundVisible(False)
- self.hideLegendSpinbox()
- self.update()
-
- def addBarSet(self):
- barSet = QBarSet('set' + str(self.series.count()))
- delta = self.series.count() * 0.1
- barSet << 1 + delta << 2 + delta << 3 + delta << 4 + delta
- self.series.append(barSet)
-
- def removeBarset(self):
- barSets = self.series.barSets()
- if len(barSets) > 0 :
- self.series.remove(barSets[-1])
-
- def setLegendAlignment(self):
- button = self.sender()
- align = self.chart.legend().alignment()
- if align == Qt.AlignTop:
- self.chart.legend().setAlignment(Qt.AlignLeft)
- if not button is None:
- button.setText('对齐(左端)')
- elif align == Qt.AlignLeft:
- self.chart.legend().setAlignment(Qt.AlignBottom)
- if not button is None:
- button.setText('对齐(底端)')
- elif align == Qt.AlignBottom:
- self.chart.legend().setAlignment(Qt.AlignRight)
- if not button is None:
- button.setText('对齐(右端)')
- else:
- self.chart.legend().setAlignment(Qt.AlignTop)
- if not button is None:
- button.setText('对齐(顶端)')
-
- def toggleBold(self):
- font = self.chart.legend().font()
- font.setBold(not font.bold())
- self.chart.legend().setFont(font)
-
- def toggleItalic(self):
- font = self.chart.legend().font()
- font.setItalic(not font.italic())
- self.chart.legend().setFont(font)
-
- def fontSizeChanged(self):
- font = self.chart.legend().font()
- font.setPointSizeF(self.fontSize.value())
- self.chart.legend().setFont(font)
-
- def updateLegendLayout(self):
- self.chart.legend().setGeometry(QRectF(self.legendPosX.value(),
- self.legendPosY.value(),
- self.legendWidth.value(),
- self.legendHeight.value()))
- self.chart.legend().update()
-
- if __name__ == '__main__':
- app = QApplication(sys.argv)
- window = DemoLegendWidget()
- window.show()
- sys.exit(app.exec())
运行效果如下图:
QChart图例设置
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。