#Encoding:utf-8ImportSys,osImportPyqtgraph as PGImportdatetime as DTImportNumPy as NPImportTraceback fromPyqtgraph. QtImportQtgui, Qtcore fromPyqtgraph. PointImport Point#########################################################################Cross cursor Support########################################################################classCrosshairtool (qtcore.qobject):"""This class is given to PG. Plotwidget () Add Crosshair function, plotwidget instance needs to be initialized when incoming"""Signal=qtcore.pyqtsignal (Type (tuple ([]) ))#---------------------------------------------------------------------- def __init__(self,pw,xaxis,viwe,parent=None): Self.__view=Viwe SELF.PW=PW Self.xdata=Xaxis Super (Crosshairtool, self).__init__() Self.xaxis=0 Self.yaxis=0#on the y-axis dynamic mid to follow the latest price show latest price and latest timeSelf.rects = [self.__view. Vb.sceneboundingrect ()] self.__textdate=pg. Textitem () self.__textsig=pg. Textitem () self.__textdate. Setzvalue (2) self.__textsig. Setzvalue (2) #registering the crosshair cursorSelf.vline = pg. InfiniteLine (angle=90, movable=False) Self.hline= pg. InfiniteLine (angle=0, movable=False) self.vLine.setPos (0) self.hLine.setPos (0) self.__view. Vb.additem (Self.vline, ignorebounds=True) self.__view. Vb.additem (Self.hline, ignorebounds=True) self.__view. Vb.additem (self.__textdate, ignorebounds=True) self.__view. Vb.additem (self.__textsig, ignorebounds=True) Self.proxy= pg. Signalproxy (Self.pw.scene (). sigmousemoved, ratelimit=60, slot=self.mousemoved)#cross-Thread refresh interface supportSelf.signal.connect (self.update)# ---------------------------------------------------------------------- defUpdate (self, POS):"""Refresh Interface Display"""Xaxis, YAxis=Pos Xaxis, YAxis= (Self.xaxis, Self.yaxis)ifXaxis isNoneElse(Xaxis, YAxis) Self.moveto (Xaxis, YAxis)# ---------------------------------------------------------------------- defmousemoved (Self, evt): Pos= Evt[0]## using signal proxy turns original arguments into a tupleSelf.rects = [self.__view. Vb.sceneboundingrect ()]#if Self.pw.sceneBoundingRect (). Contains (POS):Mousepoint = self.__view. Vb.mapscenetoview (POS) Xaxis=mousepoint.x () YAxis=Mousepoint.y () Self.moveto (Xaxis, YAxis)# ---------------------------------------------------------------------- defmoveTo (self, Xaxis, YAxis): Xaxis, YAxis= (Self.xaxis, Self.yaxis)ifXaxis isNoneElse(Xaxis, YAxis) self.rects= [Self.__view. Sceneboundingrect ()]if notXaxisor notYAxis:returnSelf.xaxis=Xaxis Self.yaxis=YAxis self.vhlinessetxy (Xaxis, YAxis) self.plotvolue (Xaxis,yaxis)# ---------------------------------------------------------------------- defVhlinessetxy (self, Xaxis, YAxis):"""Horizontal and vertical position settings"""Self.vLine.setPos (Xaxis) Self.hLine.setPos (YAxis)# ---------------------------------------------------------------------- defPlotvolue (self,xaxis,yaxis):ifSelf.xdata:ifInt (round (Xaxis)) <=len (self.xdata)-1: XValue=Self.xdata[int (Round (Xaxis))]elifInt (Xaxis) >len (self.xdata)-1: XValue=self.xdata[len (Self.xdata)-1] Else: XValue=Self.xdata[int (Xaxis)]if(Isinstance (XValue, Dt.datetime)): Xvaluetext= Dt.datetime.strftime (XValue,'%y-%m-%d%h:%m:%s') elif(Isinstance (XValue, (str))): Xvaluetext=XValueelif(Isinstance (xValue, (int))): Xvaluetext=XValueElse: Xvaluetext="" Else: Xvaluetext=Xaxis self.__textdate. sethtml ('<div style= "Text-align:center" > <span style= "color:yellow; font-size:20px;" >x=%s</span> </div>' %(Xvaluetext)) self.__textsig. sethtml ('<div style= "Text-align:right" > <span style= "color:yellow; font-size:20px;" >y=%0.2f</span> </div>' %(YAxis))#y, upper right corner displayRightaxis = self.__view. Getaxis (' Right') Rightaxiswidth=rightaxis.width () Recttextsig= self.__textdate. Boundingrect () Recttextsigwidth=recttextsig.width () topright= self.__view. Vb.mapscenetoview (qtcore.qpointf (Self.rects[0].width ()-(rightaxiswidth+recttextsigwidth), Self.rects[0].top ()))
if yaxis<self.rects[0].top (): Self . __textsig. Anchor=point (()); Else : Self . __textsig. Anchor = Point ((1, 0));
Self .__textsig. SetPos (Topright.x (), YAxis)#x-coordinateRecttextdate = self.__textdate. Boundingrect () Recttextdateheight=recttextdate.height () Bottomaxis= self.__view. Getaxis ('Bottom') Bottomaxisheight=bottomaxis.height () bottomright= self.__view. Vb.mapscenetoview (qtcore.qpointf (Self.rects[0].width (), Self.rects[0].bottom ()-(Bottomaxisheight+recttextdateheight )))
# # Modify symmetry to prevent occlusion if xaxis >self.rects[0].width (): Self . __textdate. Anchor = Point ((1, 0)) Else: Self . __textdate. Anchor = Point ((0, 0))
Self . __textdate. SetPos (Xaxis, Bottomright.y ())
Python----PYQT Cross cursor