简体   繁体   中英

Image in pyqtgraph is hidden due to zoom

I use pyqtgraph to draw the candles, but if I zoom graph very much, image hide.

I noticed that when zoom very much, “paint” method stops being called. Code with http request to exchange below.

image example

在此处输入图片说明

import pyqtgraph as pg
from pyqtgraph import QtCore, QtGui
from PyQt5 import QtWidgets

class CandlestickItem(QtWidgets.QGraphicsRectItem):
    def __init__(self, data):
        super(CandlestickItem, self).__init__()
        self.data = data
        self.generatePicture()

    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:
            if max != min:
                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):
        print('paint call')
        p.drawPicture(0, 0, self.picture)

    def boundingRect(self):
        return QtCore.QRectF(self.picture.boundingRect())


if __name__ == '__main__':
    import sys
    import urllib.request
    import json

    get_symbols_url = 'https://api.hitbtc.com/api/2/public/candles/BCNBTC?period=M30&limit=1000'
    response = urllib.request.urlopen(get_symbols_url)
    request_data = json.loads(response.read())
    data_list = []
    for i in range(0, len(request_data)):
        data_list.append((float(i), float(request_data[i]['open']), float(request_data[i]['close']),
                          float(request_data[i]['min']), float(request_data[i]['max'])))

    item = CandlestickItem(data_list)
    plt = pg.plot()
    plt.addItem(item)

    if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
        QtGui.QApplication.instance().exec_()

I need to display very small values, for example, 0.0000001-0.00000015. What can I do to prevent image hidden at high zoom?

I was answered on the support forum and it works for me. question on forum

It seems to be a rounding error. Need replace method of boundingRect.

    # import numpy as np
    def boundingRect(self):
        data = np.array(self.data)
        xmin = data[:,0].min()
        xmax = data[:,0].max()
        ymin = data[:,1:].min()
        ymax = data[:,1:].max()
        return QtCore.QRectF(xmin, ymin, xmax-xmin, ymax-ymin)

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