在数据量比较的折线图或者曲线图,受图表本身显示区域大小的限制,不能精细地显示某一小块区域的数据变化,QChartView提供了方法setRubberBand()可以实现一种类似放大镜的功能,可以将局部区域的数据放大到整个图表显示区域中显示。这样就可以方便地实现在图表中对图形序列的局部放大观察。
示例显示了如何使用鼠标利用QRubberBand 来创建自定义的缩放效果。完整代码如下:
- import sys,math
- from PyQt5.QtCore import Qt, QTimer, QRandomGenerator, QEvent, QPointF
- from PyQt5.QtGui import QPainter, QPen
- from PyQt5.QtWidgets import QApplication, QMainWindow, QGraphicsView
- from PyQt5.QtChart import QChart, QChartView,QLineSeries, QValueAxis
-
- class MyChartView(QChartView):
- def __init__(self, chart, parent = None):
- super(MyChartView, self).__init__(chart, parent)
- self.isTouching = False
- self.setRubberBand(QChartView.RectangleRubberBand)
-
- def keyPressEvent(self, event):
- if event.key() == Qt.Key_Plus:
- self.chart().zoomIn()
- elif event.key() == Qt.Key_Minus:
- self.chart().zoomOut()
- elif event.key() == Qt.Key_Left:
- self.chart().scroll(-10, 0)
- elif event.key() == Qt.Key_Right:
- self.chart().scroll(10, 0)
- elif event.key() == Qt.Key_Up:
- self.chart().scroll(0, 10)
- elif event.key() == Qt.Key_Down:
- self.chart().scroll(0, -10)
- else:
- QGraphicsView.keyPressEvent(self, event)
-
- class DemoZoomLineChart(QMainWindow):
- def __init__(self, parent=None):
- super(DemoZoomLineChart, self).__init__(parent)
-
- # 设置窗口标题
- self.setWindowTitle('实战 Qt for Python: 线条缩放演示')
- # 设置窗口大小
- self.resize(480, 360)
-
- self.createChart()
-
- def createChart(self):
- series = QLineSeries()
- for i in range(500):
- pnt = QPointF(i, math.sin(math.pi * i / 50) * 100 + QRandomGenerator.global_().bounded(20))
- series.append(pnt)
-
- chart = QChart()
- chart.setTitle('线条缩放显示')
- chart.addSeries(series)
- chart.setAnimationOptions(QChart.SeriesAnimations)
- chart.legend().hide()
- chart.createDefaultAxes()
-
- chartView =MyChartView(chart)
- chartView.setRenderHint(QPainter.Antialiasing)
- self.setCentralWidget(chartView)
-
- if __name__ == '__main__':
- app = QApplication(sys.argv)
- window = DemoZoomLineChart()
- window.show()
- sys.exit(app.exec())
运行效果如下图:
曲线缩放演示