• 实战PyQt5: 150-QChart图表之如何使用图例标记


    图例标记由图标和标签组成。QChart中QLegendMarker类可用于访问图例中的标记,因此可以对图例标记进行交互和控制。

    QLegendMarker

    QLegendMarker类是一个抽象对象,可用于访问图例中的标记。图例标记由图标和标签组成。图标颜色对应于用于绘制系列的颜色,标签显示系列的名称(或饼图系列的切片标签或柱状系列的条形标签)。图例标记始终与一个系列,切片或柱状系列集相关。

    QLegendMarker常用函数:

    • setBrush(self, brush):将用于填充图标的画刷设置为brush
    • setFont(self, font):将标签的字体设置为font
    • setLabel(self, label):将标记的标签设置为label
    • setLabelBrush(self, brush):将用于绘制标签的画刷设置为brush
    • setPen(self, pen):将用于绘制图标轮廓的画笔设置为pen
    • setShape(self, shape):设置图例标记的图形形状,shape参数可取值为枚举量QLegend. LegendMarkerType中的值。
    • setVisible(self, visible): 设置图例标记是否可见。

    枚举量QLegend. LegendMarkerType控制不同图表序列的标记样式:

    • QLegendMarker.LegendMarkerTypeArea(0): 面积系列的图例标记。
    • QLegendMarker.LegendMarkerTypeBar (1): 柱状图集的图例标记。
    • QLegendMarker.LegendMarkerTypePie (2): 饼图的图例标记。
    • QLegendMarker.LegendMarkerTypeXY (3): 线,样条曲线或散点图系列的图例标记。
    • QLegendMarker.LegendMarkerTypeBoxPlot (4): 箱形图系列的图例标记。
    • QLegendMarker.LegendMarkerTypeCandlestick (5): 烛台图系列的图例标记。

    QLegendMarker常用信号:

    • brushChanged(self):图例标记的画刷已更改时,将发出此信号。
    • fontChanged(self): 图例标记的(标签)字体已更改时,将发出此信号。
    • labelBrushChanged(self): 图例标记的标签画刷已更改时,将发出此信号。
    • labelChanged(self):图例标记的标签已更改时,将发出此信号。
    • penChanged(self):图例标记的画笔已更改时,将发出此信号。
    • shapeChanged(self):图例标记的形状已更改时,将发出此信号。
    • clicked(self):单击图例标记时,将发出此信号。
    • hovered(self, status):当鼠标悬停在图例标记上时,将发出此信号。当鼠标移到标记上时,state将变为True,而当鼠标再次移开时,state将变为False。
    • visibleChanged(self):图例标记的可见性已更改时,将发出此信号。

    图例标记示例

    在此示例中,我们创建一个使用QLegendMarker单击信号显示/隐藏图表中相应序列的应用程序。完整代码如下:

    1. import sys,math
    2. from PyQt5.QtCore import Qt, QPointF, pyqtSignal
    3. from PyQt5.QtGui import QPainter, QBrush, QColor, QPen
    4. from PyQt5.QtWidgets import (QApplicationQMainWindow)
    5. from PyQt5.QtChart import QChart, QChartView, QLineSeries, QLegendMarker
    6.  
    7. class DemoLegendMarker(QMainWindow):
    8.     def __init__(selfparent=None):
    9.         super(DemoLegendMarkerself).__init__(parent)   
    10.         
    11.          # 设置窗口标题
    12.         self.setWindowTitle('实战 Qt for PythonQChart图例标记演示')      
    13.         # 设置窗口大小
    14.         self.resize(720, 480)
    15.       
    16.         self.initUi()
    17.         
    18.     def initUi(self):
    19.         #创建图表和图标视图
    20.         self.chart = QChart()
    21.         chartView = QChartView(self.chart)
    22.         
    23.         self.series = []
    24.  
    25.         #添加一些曲线
    26.         self.addSeries()
    27.         self.addSeries()
    28.         self.addSeries()
    29.         self.addSeries()
    30.         
    31.         self.connectMarkers()
    32.         
    33.         #设置标题和显示图例
    34.         self.chart.setTitle('图例标记示例(点击图例)')
    35.         self.chart.legend().setVisible(True)
    36.         self.chart.legend().setAlignment(Qt.AlignBottom)
    37.         
    38.         chartView.setRenderHint(QPainter.Antialiasing)
    39.         self.setCentralWidget(chartView)
    40.  
    41.     def addSeries(self):
    42.         line = QLineSeries()
    43.         self.series.append(line)
    44.         
    45.         line.setName('line' + str(len(self.series)))
    46.         
    47.         #构建一些正弦波数据
    48.         data = []
    49.         offset = len(self.chart.series())
    50.         for i in range(360):
    51.             x = offset * 20 + i
    52.             data.append(QPointF(imath.sin(x * math.pi / 180)))
    53.         
    54.         line.append(data)
    55.         self.chart.addSeries(line)
    56.         
    57.         if len(self.series) == 1:
    58.             self.chart.createDefaultAxes()
    59.     
    60.     '''        
    61.     def removeSeries(self):
    62.         #从图表中移除最后一条曲线
    63.         if len(self.series) > 0 :
    64.             line = self.series[-1]
    65.             self.chart.removeSeries(line)
    66.             self.series.remove(line)
    67.             del line
    68.     '''
    69.     
    70.     def connectMarkers(self):
    71.         #连接所有标记到控制句柄
    72.         markers = self.chart.legend().markers()
    73.         for marker in markers:
    74.             #断开可能的现有连接,以避免多个连接????
    75.             #marker.clicked.disconnect(self.handleMarkerClicked)
    76.             marker.clicked.connect(self.handleMarkerClicked)
    77.             pass
    78.             
    79.     def handleMarkerClicked(self):
    80.         marker = self.sender()
    81.         if marker.type() == QLegendMarker.LegendMarkerTypeXY:
    82.             #切换曲线的可见属性
    83.             marker.series().setVisible(not marker.series().isVisible())
    84.             #把图例标记设置为可见,因为隐藏曲线时同时也会隐藏图例标记,但这里我们希望显示出来。
    85.             marker.setVisible(True)
    86.             
    87.             #=== 半透明设置 ===#
    88.             alpha = 1.0
    89.             
    90.             #如果曲线不可见,则需要定义标记的尺寸大小
    91.             if not marker.series().isVisible():
    92.                 alpha = 0.5
    93.                
    94.             brush = marker.labelBrush()
    95.             color = brush.color()
    96.             color.setAlphaF(alpha)
    97.             brush.setColor(color)
    98.             marker.setLabelBrush(brush)
    99.             
    100.             brush = marker.brush()
    101.             color = brush.color()
    102.             color.setAlphaF(alpha)
    103.             brush.setColor(color)
    104.             marker.setBrush(brush)
    105.             
    106.             pen = marker.pen()
    107.             color = pen.color()
    108.             color.setAlphaF(alpha)
    109.             pen.setColor(color)
    110.             marker.setPen(pen)
    111.             
    112.  
    113. if __name__ == '__main__':
    114.     app = QApplication(sys.argv)
    115.     window = DemoLegendMarker()
    116.     window.show()
    117.     sys.exit(app.exec())   

    运行结果如下图:

     QChart图例标记演示

    本文知识点

    • 使用图例标记类QLegendMarker实例不能直接创建,需要通过访问图表的legend()方法来获得。
    • 不同类型的图表序列所使用的图例标记形状是不一样的。
    • 使用过图例标记控制显示和隐藏图表序列。


    前一篇:实战PyQt5:149-QChart图表之图例设置

  • 相关阅读:
    Git clone时报错: OpenSSL SSL_read: Connection was reset, errno 10054
    【Java】运算符以及JShell脚本工具
    C语言打印日志0718
    使用Harbor搭建Docker仓库
    【Java】lambda表达式,Stream API,函数式编程接口
    css 实现虚线效果的3种方式详解
    宝塔面板安装部署Vue项目,Vue项目从打包到上线
    中级宏观经济学复习范围(马工程)
    MacBook Pro完整卸载及安装激活VMware Fusion13.0.0教程
    用面向对象的方式操作 JSON 甚至还能做四则运算 JSON 库
  • 原文地址:https://blog.csdn.net/seniorwizard/article/details/125614185