How can I make it show TextItems over QPainter objects?

29 views
Skip to first unread message

zero zero

unread,
Oct 18, 2017, 9:01:36 PM10/18/17
to pyqtgraph

Hello. I'm trying to plot candlesticks with TextItems that show every price level for each candle.

I referred to 'custom graphics' example and just added a method to CandlestickItem as follows.


class CandlestickItem(pg.GraphicsObject):
    def __init__(self, data, plt):
        pg.GraphicsObject.__init__(self)
        self.data = data
        self.generatePicture()
        self.plt = plt
        self.add_text()
   
    def generatePicture(self):
        self.picture = QtGui.QPicture()
        p = QtGui.QPainter(self.picture)
        p.setPen(pg.mkPen('w'))
        w = (self.data[1][0] - self.data[0][0]) / 3.
        for (t, open, close, min, max) in self.data:
            p.drawLine(QtCore.QPointF(t, min), QtCore.QPointF(t, max))
            if open > close:
                p.setBrush(pg.mkBrush('r'))
            else:
                p.setBrush(pg.mkBrush('g'))
            p.drawRect(QtCore.QRectF(t-w, open, w*2, close-open))
        p.end()
   
    def paint(self, p, *args):
        p.drawPicture(0, 0, self.picture)
   
    def boundingRect(self):
        return QtCore.QRectF(self.picture.boundingRect())
       
    def add_text(self):
        prices = self.data
       
        for price in prices:
           
            index = price[0]
            high = max(price[1:])
            low = min(price[1:])
           
            price_range = np.arange(low, high+1, 1)
           
            for price_level in price_range:
           
                text = 'text_' + str(price_level)
               
                text = pg.TextItem(str(price_level), anchor=(-0.1, 0.5))
                plt.addItem(text)
                text.setPos(index, price_level)





I have two questions:


1) How can I make it show the TextItems over the candlesticks?


2) Are there better ways to show TextItems other than the way I chose? I wonder if this way would harm the performance if there are many candles and the step size is smaller, for example: 0.01.

Reply all
Reply to author
Forward
0 new messages