Handle left axis case for x-axis label placement

For wtv cucked reason all the viewbox/scene coordinate calcs do **not**
include a left axis in the geo (likely because it's a hacked in widget
+ layout thing managed by `PlotItem`). Detect if there's a left axis and
if so use it in the label placement scene coords calc. ToDo: probably
make this a non-move calc and only recompute any time the axis changes.

Other:
- rate limit mouse events down to the 60 (ish) Hz for now
- change one last lingering `'ohlc'` array lookup
- fix `.mouseMoved()` "event" type annot
fsp_ui_mod
Tyler Goodlet 2022-01-11 16:06:51 -05:00
parent e69af9e291
commit 09fc901b0d
1 changed files with 39 additions and 23 deletions

View File

@ -24,7 +24,7 @@ from typing import Optional, Callable
import inspect import inspect
import numpy as np import numpy as np
import pyqtgraph as pg import pyqtgraph as pg
from PyQt5 import QtCore, QtGui, QtWidgets from PyQt5 import QtCore, QtWidgets
from PyQt5.QtCore import QPointF, QRectF from PyQt5.QtCore import QPointF, QRectF
from ._style import ( from ._style import (
@ -43,8 +43,8 @@ log = get_logger(__name__)
# latency (in terms of perceived lag in cross hair) so really be sure # latency (in terms of perceived lag in cross hair) so really be sure
# there's an improvement if you want to change it! # there's an improvement if you want to change it!
_mouse_rate_limit = 120 # TODO; should we calc current screen refresh rate? _mouse_rate_limit = 58 # TODO; should we calc current screen refresh rate?
_debounce_delay = 1 / 40 _debounce_delay = 1 / 60
_ch_label_opac = 1 _ch_label_opac = 1
@ -246,12 +246,16 @@ class ContentsLabels:
# for name, (label, update) in self._labels.items(): # for name, (label, update) in self._labels.items():
for chart, name, label, update in self._labels: for chart, name, label, update in self._labels:
if not (index >= 0 and index < chart._arrays['ohlc'][-1]['index']): array = chart._arrays[name]
if not (
index >= 0
and index < array[-1]['index']
):
# out of range # out of range
print('out of range?') print('out of range?')
continue continue
array = chart._arrays[name] # array = chart._arrays[name]
# call provided update func with data point # call provided update func with data point
try: try:
@ -462,12 +466,15 @@ class Cursor(pg.GraphicsObject):
def mouseMoved( def mouseMoved(
self, self,
evt: 'tuple[QMouseEvent]', # noqa coords: tuple[QPointF], # noqa
) -> None: # noqa
"""Update horizonal and vertical lines when mouse moves inside ) -> None:
'''
Update horizonal and vertical lines when mouse moves inside
either the main chart or any indicator subplot. either the main chart or any indicator subplot.
"""
pos = evt[0] '''
pos = coords[0]
# find position inside active plot # find position inside active plot
try: try:
@ -516,28 +523,37 @@ class Cursor(pg.GraphicsObject):
# with cursor movement # with cursor movement
self.contents_labels.update_labels(ix) self.contents_labels.update_labels(ix)
vl_x = ix + line_offset
for plot, opts in self.graphics.items(): for plot, opts in self.graphics.items():
# update the chart's "contents" label
# plot.update_contents_labels(ix)
# move the vertical line to the current "center of bar" # move the vertical line to the current "center of bar"
opts['vl'].setX(ix + line_offset) opts['vl'].setX(vl_x)
# update all subscribed curve dots # update all subscribed curve dots
for cursor in opts.get('cursors', ()): for cursor in opts.get('cursors', ()):
cursor.setIndex(ix) cursor.setIndex(ix)
# update the label on the bottom of the crosshair # update the label on the bottom of the crosshair
if 'bottom' in plot.plotItem.axes: axes = plot.plotItem.axes
self.xaxis_label.update_label(
# XXX: requires: # TODO: make this an up-front calc that we update
# https://github.com/pyqtgraph/pyqtgraph/pull/1418 # on axis-widget resize events.
# otherwise gobbles tons of CPU.. # left axis offset width for calcuating
# absolute x-axis label placement.
left_axis_width = 0
if 'bottom' in axes:
left = axes.get('left')
if left:
left_axis_width = left['item'].width()
# map back to abs (label-local) coordinates # map back to abs (label-local) coordinates
abs_pos=plot.mapFromView(QPointF(ix + line_offset, iy)), self.xaxis_label.update_label(
abs_pos=(
plot.mapFromView(QPointF(vl_x, iy)) -
QPointF(left_axis_width, 0)
),
value=ix, value=ix,
) )