简体   繁体   中英

Crosshair class in Pyqtgraph

I would like to define a class called Crosshair that can be attached to a plot in pyqtgraph. I found the following code snippet in the examples:

#cross hair
vLine = pg.InfiniteLine(angle=90, movable=False)
hLine = pg.InfiniteLine(angle=0, movable=False)
p1.addItem(vLine, ignoreBounds=True)
p1.addItem(hLine, ignoreBounds=True)


vb = p1.vb

def mouseMoved(evt):
    pos = evt[0]  ## using signal proxy turns original arguments into a tuple
    if p1.sceneBoundingRect().contains(pos):
        mousePoint = vb.mapSceneToView(pos)
        index = int(mousePoint.x())
        if index > 0 and index < len(data1):
            label.setText("<span style='font-size: 12pt'>x=%0.1f,   <span style='color: red'>y1=%0.1f</span>,   <span style='color: green'>y2=%0.1f</span>" % (mousePoint.x(), data1[index], data2[index]))
        vLine.setPos(mousePoint.x())
        hLine.setPos(mousePoint.y())

proxy = pg.SignalProxy(p1.scene().sigMouseMoved, rateLimit=60, slot=mouseMoved)

I started transforming it to a class as follows:

class CrossHair():
    def __init__(self, p1):
        self.vLine = pg.InfiniteLine(angle=90, movable=False)
        self.hLine = pg.InfiniteLine(angle=0, movable=False)
        self.p1 = p1
        self.vb = self.p1.vb
        p1.addItem(self.vLine, ignoreBounds=True)
        p1.addItem(self.hLine, ignoreBounds=True)
        self.proxy = pg.SignalProxy(self.p1.scene().sigMouseMoved, rateLimit=60, slot=self.mouseMoved)

    def mouseMoved(self, evt):
        pos = evt[0]  ## using signal proxy turns original arguments into a tuple
        if self.p1.sceneBoundingRect().contains(pos):
            mousePoint = self.vb.mapSceneToView(pos)
            index = int(mousePoint.x())
            # if index > 0 and index < len(data1):
            #     label.setText("<span style='font-size: 12pt'>x=%0.1f,   <span style='color: red'>y1=%0.1f</span>,   <span style='color: green'>y2=%0.1f</span>" % (mousePoint.x(), data1[index], data2[index]))
            self.vLine.setPos(mousePoint.x())
            self.hLine.setPos(mousePoint.y())

ch = CrossHair(p1)

Is this the correct way to do it? In other words, am I doing the right thing to attach the plot to the crosshair? I would have liked to do the opposite, but I'm not sure how to do that, nor if it's right to do that.

Also, how do I retrieve the data values (commented part) from the plot itself?

A better way to do this is to create a subclass of pg.GraphicsObject that includes the two InfiniteLines as children. References: QtGui.QGraphicsItem , pg.GraphicsItem , also see the customGraphicsItem example.

The class should have a setPos() method that sets the location of the crosshair origin. Then, you can add some application-level code that tracks the mouse position and updates the crosshair accordingly, as shown in the crosshair example. Alternatively, you could have the crosshair itself automatically track the mouse position.

Regarding the second question: you will at least need to tell the CrossHair which PlotDataItem(s) or PlotCurveItem(s) it should interrogate to determine the y-position that intersects the vertical line.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM