File: VTickGroup.py

package info (click to toggle)
python-pyqtgraph 0.9.8-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 3,552 kB
  • ctags: 4,262
  • sloc: python: 30,181; makefile: 116; sh: 1
file content (113 lines) | stat: -rw-r--r-- 3,743 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
if __name__ == '__main__':
    import os, sys
    path = os.path.abspath(os.path.dirname(__file__))
    sys.path.insert(0, os.path.join(path, '..', '..'))

from pyqtgraph.Qt import QtGui, QtCore
import pyqtgraph.functions as fn
import weakref
from .UIGraphicsItem import UIGraphicsItem

__all__ = ['VTickGroup']
class VTickGroup(UIGraphicsItem):
    """
    **Bases:** :class:`UIGraphicsItem <pyqtgraph.UIGraphicsItem>`
    
    Draws a set of tick marks which always occupy the same vertical range of the view,
    but have x coordinates relative to the data within the view.
    
    """
    def __init__(self, xvals=None, yrange=None, pen=None):
        """
        ============= ===================================================================
        **Arguments**
        xvals         A list of x values (in data coordinates) at which to draw ticks.
        yrange        A list of [low, high] limits for the tick. 0 is the bottom of 
                      the view, 1 is the top. [0.8, 1] would draw ticks in the top 
                      fifth of the view.
        pen           The pen to use for drawing ticks. Default is grey. Can be specified
                      as any argument valid for :func:`mkPen<pyqtgraph.mkPen>`
        ============= ===================================================================
        """
        if yrange is None:
            yrange = [0, 1]
        if xvals is None:
            xvals = []
            
        UIGraphicsItem.__init__(self)
            
        if pen is None:
            pen = (200, 200, 200)
            
        self.path = QtGui.QGraphicsPathItem()
        
        self.ticks = []
        self.xvals = []
        self.yrange = [0,1]
        self.setPen(pen)
        self.setYRange(yrange)
        self.setXVals(xvals)
        
    def setPen(self, *args, **kwargs):
        """Set the pen to use for drawing ticks. Can be specified as any arguments valid
        for :func:`mkPen<pyqtgraph.mkPen>`"""        
        self.pen = fn.mkPen(*args, **kwargs)

    def setXVals(self, vals):
        """Set the x values for the ticks. 
        
        ============= =====================================================================
        **Arguments** 
        vals          A list of x values (in data/plot coordinates) at which to draw ticks.
        ============= =====================================================================
        """
        self.xvals = vals
        self.rebuildTicks()
        #self.valid = False
        
    def setYRange(self, vals):
        """Set the y range [low, high] that the ticks are drawn on. 0 is the bottom of 
        the view, 1 is the top."""
        self.yrange = vals
        self.rebuildTicks()
        
    def dataBounds(self, *args, **kargs):
        return None  ## item should never affect view autoscaling
            
    def yRange(self):
        return self.yrange
            
    def rebuildTicks(self):
        self.path = QtGui.QPainterPath()
        yrange = self.yRange()
        for x in self.xvals:
            self.path.moveTo(x, 0.)
            self.path.lineTo(x, 1.)
        
    def paint(self, p, *args):
        UIGraphicsItem.paint(self, p, *args)
        
        br = self.boundingRect()
        h = br.height()
        br.setY(br.y() + self.yrange[0] * h)
        br.setHeight(h - (1.0-self.yrange[1]) * h)
        p.translate(0, br.y())
        p.scale(1.0, br.height())
        p.setPen(self.pen)
        p.drawPath(self.path)


if __name__ == '__main__':
    app = QtGui.QApplication([])
    import pyqtgraph as pg
    vt = VTickGroup([1,3,4,7,9], [0.8, 1.0])
    p = pg.plot()
    p.addItem(vt)
    
    if sys.flags.interactive == 0:
        app.exec_()