问题
I'd like to refresh a candle graph in the fastest way. But following the modified sample, I've got to draw whole chart every time even when I add just one new bar. The reason is that the QPainter object is cleared when I call the object again to draw additional data. It can be slow. Literally I'd like to just "add" a bar data.
So could anybody kindly teach me how to restart painting or give me good ideas?
"""
Demonstrate creation of a custom graphic (a candlestick plot)
"""
import pyqtgraph as pg
from pyqtgraph import QtCore, QtGui
import random
## Create a subclass of GraphicsObject.
## The only required methods are paint() and boundingRect()
## (see QGraphicsItem documentation)
class CandlestickItem(pg.GraphicsObject):
def __init__(self):
pg.GraphicsObject.__init__(self)
self.flagHasData = False
def set_data(self, data):
self.data = data ## data must have fields: time, open, close, min, max
self.flagHasData = True
self.generatePicture()
self.informViewBoundsChanged()
def generatePicture(self):
## pre-computing a QPicture object allows paint() to run much more quickly,
## rather than re-drawing the shapes every time.
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):
if self.flagHasData:
p.drawPicture(0, 0, self.picture)
def boundingRect(self):
## boundingRect _must_ indicate the entire area that will be drawn on
## or else we will get artifacts and possibly crashing.
## (in this case, QPicture does all the work of computing the bouning rect for us)
return QtCore.QRectF(self.picture.boundingRect())
app = QtGui.QApplication([])
data = [ ## fields are (time, open, close, min, max).
[1., 10, 13, 5, 15],
[2., 13, 17, 9, 20],
[3., 17, 14, 11, 23],
[4., 14, 15, 5, 19],
[5., 15, 9, 8, 22],
[6., 9, 15, 8, 16],
]
item = CandlestickItem()
item.set_data(data)
plt = pg.plot()
plt.addItem(item)
plt.setWindowTitle('pyqtgraph example: customGraphicsItem')
def update():
global item, data
data_len = len(data)
rand = random.randint(0, len(data)-1)
new_bar = data[rand][:]
new_bar[0] = data_len
data.append(new_bar)
item.set_data(data)
app.processEvents() ## force complete redraw for every plot
timer = QtCore.QTimer()
timer.timeout.connect(update)
timer.start(100)
## Start Qt event loop unless running in interactive mode or using pyside.
if __name__ == '__main__':
import sys
if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
QtGui.QApplication.instance().exec_()
回答1:
Here are a couple of options:
Make your custom item draw only one bar, and simply add a new item for each new data point. This would avoid the need to regenerate the entire picture, but might have slower performance with interactive zoom / pan when you are displaying many items.
Use a hybrid approach where you add new bars to CandleStickItem until it reaches some maximum size, and then start a new CandleStickItem (but keep the old ones as well). That way, each new bar will only cause a small amount of effort in generatePicture(), but you keep the total number of calls to paint() smaller.
来源:https://stackoverflow.com/questions/24670856/the-fastest-way-to-add-a-new-data-bar-with-pyqtgraph