Try cache mode on a bunch of "static" graphics

chart_trader
Tyler Goodlet 2020-12-28 12:54:22 -05:00
parent 588213a230
commit b467e8fd8d
1 changed files with 13 additions and 57 deletions

View File

@ -222,6 +222,7 @@ class CrossHair(pg.GraphicsObject):
self.active_plot = None self.active_plot = None
self.digits = digits self.digits = digits
self._lastx = None self._lastx = None
# self.setCacheMode(QtGui.QGraphicsItem.DeviceCoordinateCache)
def add_plot( def add_plot(
self, self,
@ -231,8 +232,10 @@ class CrossHair(pg.GraphicsObject):
# add ``pg.graphicsItems.InfiniteLine``s # add ``pg.graphicsItems.InfiniteLine``s
# vertical and horizonal lines and a y-axis label # vertical and horizonal lines and a y-axis label
vl = plot.addLine(x=0, pen=self.lines_pen, movable=False) vl = plot.addLine(x=0, pen=self.lines_pen, movable=False)
vl.setCacheMode(QtGui.QGraphicsItem.DeviceCoordinateCache)
hl = plot.addLine(y=0, pen=self.lines_pen, movable=False) hl = plot.addLine(y=0, pen=self.lines_pen, movable=False)
hl.setCacheMode(QtGui.QGraphicsItem.DeviceCoordinateCache)
hl.hide() hl.hide()
yl = YAxisLabel( yl = YAxisLabel(
@ -241,6 +244,7 @@ class CrossHair(pg.GraphicsObject):
opacity=_ch_label_opac, opacity=_ch_label_opac,
bg_color='default', bg_color='default',
) )
yl.setCacheMode(QtGui.QGraphicsItem.DeviceCoordinateCache)
yl.hide() # on startup if mouse is off screen yl.hide() # on startup if mouse is off screen
# TODO: checkout what ``.sigDelayed`` can be used for # TODO: checkout what ``.sigDelayed`` can be used for
@ -283,6 +287,7 @@ class CrossHair(pg.GraphicsObject):
) )
# place label off-screen during startup # place label off-screen during startup
self.xaxis_label.setPos(self.plots[0].mapFromView(QPointF(0, 0))) self.xaxis_label.setPos(self.plots[0].mapFromView(QPointF(0, 0)))
self.xaxis_label.setCacheMode(QtGui.QGraphicsItem.DeviceCoordinateCache)
def add_curve_cursor( def add_curve_cursor(
self, self,
@ -495,7 +500,7 @@ def gen_qpath(
w, w,
) -> QtGui.QPainterPath: ) -> QtGui.QPainterPath:
profiler = pg.debug.Profiler(disabled=False) profiler = pg.debug.Profiler(disabled=True)
x, y, c = path_arrays_from_ohlc(data, start, bar_gap=w) x, y, c = path_arrays_from_ohlc(data, start, bar_gap=w)
profiler("generate stream with numba") profiler("generate stream with numba")
@ -539,7 +544,6 @@ class BarItems(pg.GraphicsObject):
# was worth a shot: # was worth a shot:
# self.path.reserve(int(100e3 * 6)) # self.path.reserve(int(100e3 * 6))
# self.last_bar = QtGui.QPicture()
self.path = QtGui.QPainterPath() self.path = QtGui.QPainterPath()
self._pi = plotitem self._pi = plotitem
@ -547,11 +551,6 @@ class BarItems(pg.GraphicsObject):
self._xrange: Tuple[int, int] self._xrange: Tuple[int, int]
self._yrange: Tuple[float, float] self._yrange: Tuple[float, float]
# XXX: not sure this actually needs to be an array other
# then for the old tina mode calcs for up/down bars below?
# lines container
# self.lines = _mk_lines_array([], 50e3, 6)
# TODO: don't render the full backing array each time # TODO: don't render the full backing array each time
# self._path_data = None # self._path_data = None
self._last_bar_lines: Optional[Tuple[QLineF, ...]] = None self._last_bar_lines: Optional[Tuple[QLineF, ...]] = None
@ -585,36 +584,12 @@ class BarItems(pg.GraphicsObject):
# up to last to avoid double draw of last bar # up to last to avoid double draw of last bar
self._last_bar_lines = lines_from_ohlc(data[-1], self.w) self._last_bar_lines = lines_from_ohlc(data[-1], self.w)
# create pics
# self.draw_history()
# self.draw_last_bar()
# trigger render # trigger render
# https://doc.qt.io/qt-5/qgraphicsitem.html#update # https://doc.qt.io/qt-5/qgraphicsitem.html#update
self.update() self.update()
return self.path return self.path
# def update_ranges(
# self,
# xmn: int,
# xmx: int,
# ymn: float,
# ymx: float,
# ) -> None:
# ...
# def draw_last_bar(self) -> None:
# """Currently this draws lines to a cached ``QPicture`` which
# is supposed to speed things up on ``.paint()`` calls (which
# is a call to ``QPainter.drawPicture()`` but I'm not so sure.
# """
# p = QtGui.QPainter(self.last_bar)
# # p.setPen(self.bars_pen)
# p.drawLines(*tuple(filter(bool, self._last_bar_lines)))
# p.end()
# @timeit # @timeit
def update_from_array( def update_from_array(
self, self,
@ -642,6 +617,8 @@ class BarItems(pg.GraphicsObject):
prepend_length = istart - first_index prepend_length = istart - first_index
append_length = last_index - istop append_length = last_index - istop
flip_cache = False
# TODO: allow mapping only a range of lines thus # TODO: allow mapping only a range of lines thus
# only drawing as many bars as exactly specified. # only drawing as many bars as exactly specified.
@ -658,7 +635,6 @@ class BarItems(pg.GraphicsObject):
# update path # update path
old_path = self.path old_path = self.path
self.path = prepend_path self.path = prepend_path
# self.path.reserve(int(100e3 * 6))
self.path.addPath(old_path) self.path.addPath(old_path)
# trigger redraw despite caching # trigger redraw despite caching
@ -668,10 +644,6 @@ class BarItems(pg.GraphicsObject):
# generate new lines objects for updatable "current bar" # generate new lines objects for updatable "current bar"
self._last_bar_lines = lines_from_ohlc(array[-1], self.w) self._last_bar_lines = lines_from_ohlc(array[-1], self.w)
# self.draw_last_bar()
# self.update()
# generate new graphics to match provided array # generate new graphics to match provided array
# path appending logic: # path appending logic:
# we need to get the previous "current bar(s)" for the time step # we need to get the previous "current bar(s)" for the time step
@ -684,13 +656,11 @@ class BarItems(pg.GraphicsObject):
# trigger redraw despite caching # trigger redraw despite caching
self.prepareGeometryChange() self.prepareGeometryChange()
self.setCacheMode(QtGui.QGraphicsItem.NoCache)
flip_cache = True
self._xrange = first_index, last_index self._xrange = first_index, last_index
# if just_history:
# self.update()
# return
# last bar update # last bar update
i, o, h, l, last, v = array[-1][ i, o, h, l, last, v = array[-1][
['index', 'open', 'high', 'low', 'close', 'volume'] ['index', 'open', 'high', 'low', 'close', 'volume']
@ -719,17 +689,11 @@ class BarItems(pg.GraphicsObject):
# now out of date / from some previous sample. It's weird # now out of date / from some previous sample. It's weird
# though because i've seen it do this to bars i - 3 back? # though because i've seen it do this to bars i - 3 back?
# else:
# # XXX: h == l -> remove any HL line to avoid render bug
# if body is not None:
# self._last_bar_lines = (None, larm, rarm)
# # body = self.lines[index - 1][0] = None
# self.draw_last_bar()
self.resetTransform()
self.setTransform(self.transform())
self.update() self.update()
if flip_cache:
self.setCacheMode(QtGui.QGraphicsItem.DeviceCoordinateCache)
# @timeit # @timeit
def paint(self, p, opt, widget): def paint(self, p, opt, widget):
@ -744,7 +708,6 @@ class BarItems(pg.GraphicsObject):
# as is necesarry for what's in "view". Not sure if this will # as is necesarry for what's in "view". Not sure if this will
# lead to any perf gains other then when zoomed in to less bars # lead to any perf gains other then when zoomed in to less bars
# in view. # in view.
# p.drawPicture(0, 0, self.last_bar)
p.drawLines(*tuple(filter(bool, self._last_bar_lines))) p.drawLines(*tuple(filter(bool, self._last_bar_lines)))
profiler('draw last bar') profiler('draw last bar')
@ -766,11 +729,6 @@ class BarItems(pg.GraphicsObject):
# (in this case, QPicture does all the work of computing the # (in this case, QPicture does all the work of computing the
# bounding rect for us). # bounding rect for us).
# compute aggregate bounding rectangle
# lb = self.last_bar.boundingRect()
# hb = self.path.boundingRect()
# apparently this a lot faster says the docs? # apparently this a lot faster says the docs?
# https://doc.qt.io/qt-5/qpainterpath.html#controlPointRect # https://doc.qt.io/qt-5/qpainterpath.html#controlPointRect
hb = self.path.controlPointRect() hb = self.path.controlPointRect()
@ -786,9 +744,7 @@ class BarItems(pg.GraphicsObject):
QtCore.QPointF(hb.topLeft()), QtCore.QPointF(hb.topLeft()),
# total size # total size
# QtCore.QSizeF(QtCore.QSizeF(lb.size()) + hb.size())
QtCore.QSizeF(w, h) QtCore.QSizeF(w, h)
# QtCore.QSizeF(lb.size() + hb.size())
) )
# print(f'bounding rect: {br}') # print(f'bounding rect: {br}')
return br return br