如何在pyqtgraph中的烛台上绘制TextItems?

时间:2017-10-29 11:52:07

标签: python pyqtgraph

我试图使用TextItems绘制烛台,显示每个蜡烛的每个价格水平。

我提到了'自定义图形'示例,只是向CandlestickItem添加了一个方法,如下所示。

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)

enter image description here 我有两个问题:

1)如何让它在烛台上显示TextItems?

2)除了我选择的方式之外,还有更好的方式来显示TextItems吗?我想知道如果有很多蜡烛并且步长较小,这种方式是否会损害性能,例如:0.01。

1 个答案:

答案 0 :(得分:0)

尝试移动该行:     self.add_text()

在方法末尾:     def paint(self,p,* args):         p.drawPicture(0,0,self.picture)         self.add_text()

after the change