diff --git a/PyQtGraph/README.md b/PyQtGraph/README.md index 848a782..9b89f87 100644 --- a/PyQtGraph/README.md +++ b/PyQtGraph/README.md @@ -15,8 +15,17 @@ ## 目录 - [鼠标获取X轴坐标](#1、鼠标获取X轴坐标) +- [禁止右键点击功能、鼠标滚轮,添加滚动条等功能](#2、禁止右键点击功能、鼠标滚轮,添加滚动条等功能) ## 1、鼠标获取X轴坐标 [运行 mouseFlow.py](mouseFlow.py) -![mouseFlow](ScreenShot/mouseFlow.gif) \ No newline at end of file +![mouseFlow](ScreenShot/mouseFlow.gif) + +## 2、禁止右键点击功能、鼠标滚轮,添加滚动条等功能 +[运行 graph1.py](graph1.py) + +![mouseFlow](ScreenShot/function.gif) + +## 2、不用修改源码,重加载,解决右键保存图片异常;解决自定义坐标轴密集显示;禁止鼠标事件; +[加载 tools.py](tools.py) \ No newline at end of file diff --git a/PyQtGraph/ScreenShot/function.gif b/PyQtGraph/ScreenShot/function.gif new file mode 100644 index 0000000..708e0f0 Binary files /dev/null and b/PyQtGraph/ScreenShot/function.gif differ diff --git a/PyQtGraph/graph1.py b/PyQtGraph/graph1.py new file mode 100644 index 0000000..1a1e581 --- /dev/null +++ b/PyQtGraph/graph1.py @@ -0,0 +1,73 @@ +#!/usr/bin/env python +# encoding: utf-8 +''' +Created on 2019年5月21日 +@author: weike32 +@site: https://pyqt5.com ,https://github.com/weike32 +@email: 394967319@qq.com +@file: CopyContent +@description: 禁止右键,添加滑动窗口,点击按钮生成图片,自定义Y轴坐标,背景颜色调整 +''' +import sys +from PyQt5.QtWidgets import QDialog, QApplication, QWidget +from qtpy import QtWidgets +import pyqtgraph as pg +from PyQtGraph.graphAnalysis import graph_Form + +class CustomViewBox(pg.ViewBox): + def __init__(self, *args, **kwds): + pg.ViewBox.__init__(self, *args, **kwds) + self.RectMode = 3 + self.setMouseMode(self.RectMode) + + def mouseClickEvent(self, ev): + if ev.button() == pg.QtCore.Qt.RightButton: + self.autoRange() + + def mouseDragEvent(self, ev): + pg.ViewBox.mouseDragEvent(self, ev) + + def wheelEvent(self,ev, axis=None): + # pg.ViewBox.wheelEvent(self, ev, axis) + ev.ignore() + +class graphAnalysis(QDialog,graph_Form): + def __init__(self): + super(graphAnalysis, self).__init__() + self.setupUi(self) + self.pushButton_7.clicked.connect(self.test) + self.tabWidget.clear() + + def test(self): + tab1 = QtWidgets.QWidget() + scrollArea = QtWidgets.QScrollArea(tab1) + scrollArea.setMinimumSize(984,550) + scrollArea.setWidgetResizable(True) + labelsContainer = QWidget() + labelsContainer.setMinimumSize(0,1500) + scrollArea.setWidget(labelsContainer) + layout = QtWidgets.QVBoxLayout(labelsContainer) + time = ['2019-04-20 08:09:00', '2019-04-20 08:09:00', '2019-04-20 08:09:00', '2019-04-20 08:09:00'] + value = [1.2, 2, 1, 4] + xdict = dict(enumerate(time)) + ticks = [list(zip(range(4), tuple(time)))] + vb = CustomViewBox() + plt = pg.PlotWidget(title="标题这里填写",viewBox=vb) + plt.setBackground(background=None) + plt.plot(list(xdict.keys()), value) + plt.getPlotItem().getAxis("bottom").setTicks(ticks) + temp = QtWidgets.QWidget() + temp.setMinimumSize(900,300) + temp.setMaximumSize(900,300) + layout1 = QtWidgets.QVBoxLayout(temp) + layout1.addWidget(plt) + layout.addWidget(temp) + spacerItem = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding) + layout.addItem(spacerItem) + self.tabWidget.addTab(tab1, '这里tabWidget修改标签') + +if __name__ =="__main__": + app = QApplication(sys.argv) + w = graphAnalysis() + w.show() + sys.exit(app.exec_()) \ No newline at end of file diff --git a/PyQtGraph/graphAnalysis.ui b/PyQtGraph/graphAnalysis.ui new file mode 100644 index 0000000..2dbb64f --- /dev/null +++ b/PyQtGraph/graphAnalysis.ui @@ -0,0 +1,86 @@ + + + Form + + + + 0 + 0 + 1024 + 768 + + + + Form + + + + 20 + + + 20 + + + 20 + + + 20 + + + 20 + + + + + + 15 + + + + 图形分析 + + + Qt::AlignCenter + + + + + + + 0 + + + + 折线图 + + + + + Tab 2 + + + + + + + + + 0 + 80 + + + + + 15 + + + + 分析 + + + + + + + + diff --git a/PyQtGraph/mouseFlow.py b/PyQtGraph/mouseFlow.py index 9efe804..9d9f9fb 100644 --- a/PyQtGraph/mouseFlow.py +++ b/PyQtGraph/mouseFlow.py @@ -1,7 +1,7 @@ #!/usr/bin/env python # encoding: utf-8 ''' -Created on 2017年5月2日 +Created on 2019年5月2日 @author: weike32 @site: https://pyqt5.com ,https://github.com/weike32 @email: 394967319@qq.com diff --git a/PyQtGraph/tools.py b/PyQtGraph/tools.py new file mode 100644 index 0000000..edfb931 --- /dev/null +++ b/PyQtGraph/tools.py @@ -0,0 +1,80 @@ +#!/usr/bin/env python +# encoding: utf-8 +''' +Created on 2019年5月21日 +@author: weike32 +@site: https://pyqt5.com ,https://github.com/weike32 +@email: 394967319@qq.com +@file: CopyContent +@description: 工具类 +''' +from pyqtgraph.exporters.ImageExporter import ImageExporter, Exporter +from pyqtgraph.parametertree import Parameter +import pyqtgraph as pg +#不用修改源码,重加载,解决右键保存图片异常 +def widthChanged(self): + sr = self.getSourceRect() + ar = float(sr.height()) / sr.width() + self.params.param('height').setValue(int(self.params['width'] * ar), blockSignal=self.heightChanged) + +def heightChanged(self): + sr = self.getSourceRect() + ar = float(sr.width()) / sr.height() + self.params.param('width').setValue(int(self.params['height'] * ar), blockSignal=self.widthChanged) + +def New__init__(self, item): + Exporter.__init__(self, item) + tr = self.getTargetRect() + if isinstance(item, pg.Qt.QtGui.QGraphicsItem): + scene = item.scene() + else: + scene = item + bgbrush = scene.views()[0].backgroundBrush() + bg = bgbrush.color() + if bgbrush.style() == pg.Qt.QtCore.Qt.NoBrush: + bg.setAlpha(0) + + self.params = Parameter(name='params', type='group', children=[ + {'name': 'width', 'type': 'int', 'value': int(tr.width()), 'limits': (0, None)}, + {'name': 'height', 'type': 'int', 'value': int(tr.height()), 'limits': (0, None)}, + {'name': 'antialias', 'type': 'bool', 'value': True}, + {'name': 'background', 'type': 'color', 'value': bg}, + ]) + self.params.param('width').sigValueChanged.connect(self.widthChanged) + self.params.param('height').sigValueChanged.connect(self.heightChanged) +ImageExporter.heightChanged = heightChanged +ImageExporter.widthChanged = widthChanged +ImageExporter.__init__ = New__init__ + +#解决自定义坐标轴密集显示 +class MyStringAxis(pg.AxisItem): + def __init__(self, xdict, *args, **kwargs): + pg.AxisItem.__init__(self, *args, **kwargs) + self.xdict = xdict + + def tickStrings(self, values, scale, spacing): + strings = [] + for v in values: + vs = v * scale + if vs in self.xdict.keys(): + vstr = self.xdict[vs] + else: + vstr = "" + strings.append(vstr) + return strings +# 禁止鼠标事件 +class CustomViewBox(pg.ViewBox): + def __init__(self, *args, **kwds): + pg.ViewBox.__init__(self, *args, **kwds) + self.RectMode = 3 + self.setMouseMode(self.RectMode) + + def mouseClickEvent(self, ev): + if ev.button() == pg.QtCore.Qt.RightButton: + self.autoRange() + + def mouseDragEvent(self, ev): + pg.ViewBox.mouseDragEvent(self, ev) + + def wheelEvent(self,ev, axis=None): + pg.ViewBox.wheelEvent(self, ev, axis) \ No newline at end of file