当前位置:   article > 正文

实战PyQt5: 139-QChart图表之面积图_pyqt qareaseries

pyqt qareaseries

面积图(Area Chart)又称区域图,面积图强调数据随基线数据而变化的程度,也可用于引起人们对总值趋势的注意。例如,表示随时间而变化的利润的数据可以绘制在面积图中以强调总利润。在QChart中使用QAreaSeries来实现面积图的绘制。

QAreaSeries

QAreaSeries类实现用面积图的方式显示数据。面积图用于显示定量数据。它根据高低两条边界线来确定面积区域。两条边界线之间的区域将被填充。

QAreaSeries常用函数:

  • setBorderColor(self, color):设置边界线条颜色为color。
  • setBrush(self, brush):设置填充所用的画刷为brush。
  • setColor(self, color):设置用于填充(画刷)的颜色color。
  • setPen(self, pen): 设置用于绘制区域轮廓的画笔为pen。
  • setPointLabelsClipping(self, enabled): 设置数据点标签的剪裁方式。默认为True。启用裁剪后,将剪切掉绘图区域边缘上的标签。
  • setPointLabelsColor(self, color):设置数据点标签的颜色为color,默认情况下,颜色是为主题中的标签定义的画笔的颜色。
  • setPointLabelsFont(self, font):设置用于数据点标签的字体为font。
  • setPointLabelsFormat(self, format):设置用于显示带有序列点的标签的格式为format。 QAreaSeries支持以下的标签格式为'@xPont'( 数据点的x值)和'@yPont'( 数据点的y值) 。如:
series. setPointLabelsFormat('(@xPoint, @yPoint)')
  • setPointLabelsVisible(self, visible):设置数据点标签是否可见。
  • setPointsVisible(self, visible):设置数据点是否visible, 并确定是否在线上绘制这些点。
  • setLowerSeries(self, series):设置下边界线。
  • setUpperSeries(self, series):设置上边界线。

QAreaSeries常用信号:

  • borderColorChanged(self, color):当线条(画笔)颜色变为color时,发出此信号。
  • clicked(self, point):当用户在面积图中单击以触发按下时,将发出此信号。
  • colorChanged(self, color):当填充(画刷)颜色变为color时,发出此信号。
  • doubleClicked(self, point):当用户通过双击point触发面积图中的第一次按下时,将发出此信号。
  • hovered(self, point, state):当用户将鼠标指针悬停在系列上或将其从系列中移开时,将发出此信号。point显示悬停事件的原点(坐标)。当光标悬停在面积图上时state为True。否则state为False。
  • pointLabelsClippingChanged(self, clipping):当数据点标签的剪裁设置clipping发生改变时,将发出此信号。
  • pointLabelsColorChanged(self, color):当数据点标签的颜色改变为color时,将发出此信号。
  • pointLabelsFontChanged(self, font):当数据点标签的字体改变为font时,将发出此信号。
  • pointLabelsFormatChanged(self, format):当数据点标签的格式改变为format时,将发出此信号。
  • pointLabelsVisibilityChanged(self, visible):当数据点标签的可见属性改变为visible时,将发出此信号。
  • pressed(self, point):当用户按下面积图中由point指定的点时,将发出此信号。
  • released(self, point):当用户释放在面积图中某个point上释放按下状态时,将发出此信号。

面积图示例

示例代码显示了如何创建简单的面积图,并使用渐变方式填充整个区域。完整代码如下:

  1. import sys
  2. from PyQt5.QtCore import Qt, QPointF
  3. from PyQt5.QtGui import QPainter, QPen, QLinearGradient, QColor
  4. from PyQt5.QtWidgets import QApplication, QMainWindow
  5. from  PyQt5.QtChart import QChartView, QChart, QLineSeries, QAreaSeries
  6.  
  7. class DemoChartArea(QMainWindow):
  8.     def __init__(self, parent=None):
  9.         super(DemoChartArea, self).__init__(parent)   
  10.         
  11.          # 设置窗口标题
  12.         self.setWindowTitle('实战 Qt for Python: QChart面积图演示')      
  13.         # 设置窗口大小
  14.         self.resize(480360)
  15.         
  16.         self.createChart()
  17.         
  18.     def createChart(self):
  19.         
  20.         #面积图的上下两条曲线
  21.         #注意line0line1的生存周期!!!!!
  22.         self.line0 = QLineSeries()
  23.         self.line1 = QLineSeries()
  24.         
  25.         self.line0 << QPointF(15<< QPointF(37<< QPointF(76<< QPointF(97<< QPointF(126<< QPointF(167<< QPointF(185)
  26.         self.line1 << QPointF(13<< QPointF(34<< QPointF(73<< QPointF(82<< QPointF(123<< QPointF(164<< QPointF(183)
  27.         
  28.         #面积图
  29.         areaSeries = QAreaSeries(self.line0self.line1)
  30.         areaSeries.setName('蝙蝠侠') #Batman
  31.         
  32.         pen = QPen(QColor(0x059605))
  33.         pen.setWidth(3)
  34.         areaSeries.setPen(pen)
  35.         
  36.         #渐变设置
  37.         gradient = QLinearGradient(QPointF(0,0), QPointF(01))
  38.         gradient.setColorAt(0.0, QColor(0x3cc63c))
  39.         gradient.setColorAt(1.0, QColor(0x26f626))
  40.         gradient.setCoordinateMode(QLinearGradient.ObjectBoundingMode)
  41.         areaSeries.setBrush(gradient)
  42.               
  43.         #创建图表
  44.         chart = QChart()
  45.         chart.addSeries(areaSeries)
  46.         chart.setTitle('简单面积图示例')
  47.         chart.createDefaultAxes()
  48.         chart.axes(Qt.Horizontal)[0].setRange(020) #横坐标数值范围
  49.         chart.axes(Qt.Vertical)[0].setRange(010) #纵坐标数值范围
  50.          
  51.         #图表视图
  52.         chartView = QChartView(chart)
  53.         chartView.setRenderHint(QPainter.Antialiasing)
  54.         
  55.         self.setCentralWidget(chartView)
  56.       
  57.         
  58. if __name__ == '__main__':
  59.     app = QApplication(sys.argv)
  60.     window = DemoChartArea()
  61.     window.show()
  62.     sys.exit(app.exec())   

运行结果如下图:

 QChart面积图演示

本文知识点

  • 面积图强调数据随基线数据而变化的程度。
  • 使用QAreaSeries绘制面积图。
  • 面积图并不拥有它的上下基线的控制权,因此要上下基线对象的生存周期。

前一篇:实战PyQt5: 138-QChart图表之样条曲线

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/正经夜光杯/article/detail/781396
推荐阅读
  

闽ICP备14008679号