2020-12-29 13:43:25 +00:00
|
|
|
# piker: trading gear for hackers
|
2021-01-23 03:59:00 +00:00
|
|
|
# Copyright (C) Tyler Goodlet (in stewardship for piker0)
|
2020-12-29 13:43:25 +00:00
|
|
|
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2021-02-08 12:03:28 +00:00
|
|
|
|
2020-12-29 13:43:25 +00:00
|
|
|
"""
|
|
|
|
Mouse interaction graphics
|
|
|
|
|
|
|
|
"""
|
2021-06-21 20:45:27 +00:00
|
|
|
from functools import partial
|
|
|
|
from typing import Optional, Callable
|
2020-12-29 13:43:25 +00:00
|
|
|
|
|
|
|
import inspect
|
|
|
|
import numpy as np
|
|
|
|
import pyqtgraph as pg
|
2022-01-11 21:06:51 +00:00
|
|
|
from PyQt5 import QtCore, QtWidgets
|
2021-02-21 16:45:24 +00:00
|
|
|
from PyQt5.QtCore import QPointF, QRectF
|
2020-12-29 13:43:25 +00:00
|
|
|
|
2021-07-12 13:18:11 +00:00
|
|
|
from ._style import (
|
2020-12-29 13:43:25 +00:00
|
|
|
_xaxis_at,
|
|
|
|
hcolor,
|
2021-06-04 15:30:15 +00:00
|
|
|
_font_small,
|
2021-09-04 18:24:33 +00:00
|
|
|
_font,
|
2020-12-29 13:43:25 +00:00
|
|
|
)
|
2021-07-12 13:18:11 +00:00
|
|
|
from ._axes import YAxisLabel, XAxisLabel
|
|
|
|
from ..log import get_logger
|
2021-01-01 17:59:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
log = get_logger(__name__)
|
2020-12-29 13:43:25 +00:00
|
|
|
|
|
|
|
# XXX: these settings seem to result in really decent mouse scroll
|
|
|
|
# latency (in terms of perceived lag in cross hair) so really be sure
|
|
|
|
# there's an improvement if you want to change it!
|
2021-09-04 18:24:33 +00:00
|
|
|
|
2022-04-24 21:09:58 +00:00
|
|
|
_mouse_rate_limit = 60 # TODO; should we calc current screen refresh rate?
|
|
|
|
_debounce_delay = 0
|
2020-12-29 13:43:25 +00:00
|
|
|
_ch_label_opac = 1
|
|
|
|
|
|
|
|
|
|
|
|
# TODO: we need to handle the case where index is outside
|
|
|
|
# the underlying datums range
|
|
|
|
class LineDot(pg.CurvePoint):
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
2021-07-22 00:02:09 +00:00
|
|
|
|
2020-12-29 13:43:25 +00:00
|
|
|
curve: pg.PlotCurveItem,
|
|
|
|
index: int,
|
2021-07-22 00:02:09 +00:00
|
|
|
|
2020-12-29 13:43:25 +00:00
|
|
|
plot: 'ChartPlotWidget', # type: ingore # noqa
|
|
|
|
pos=None,
|
|
|
|
color: str = 'default_light',
|
2021-07-22 00:02:09 +00:00
|
|
|
|
2020-12-29 13:43:25 +00:00
|
|
|
) -> None:
|
2021-09-04 18:24:33 +00:00
|
|
|
# scale from dpi aware font size
|
2022-01-11 21:06:51 +00:00
|
|
|
size = int(_font.px_size * 0.375)
|
2021-09-04 18:24:33 +00:00
|
|
|
|
2020-12-29 13:43:25 +00:00
|
|
|
pg.CurvePoint.__init__(
|
|
|
|
self,
|
|
|
|
curve,
|
|
|
|
index=index,
|
|
|
|
pos=pos,
|
|
|
|
rotate=False,
|
|
|
|
)
|
|
|
|
self._plot = plot
|
|
|
|
|
|
|
|
# TODO: get pen from curve if not defined?
|
|
|
|
cdefault = hcolor(color)
|
|
|
|
pen = pg.mkPen(cdefault)
|
|
|
|
brush = pg.mkBrush(cdefault)
|
|
|
|
|
|
|
|
# presuming this is fast since it's built in?
|
2021-07-21 19:50:09 +00:00
|
|
|
dot = self.dot = QtWidgets.QGraphicsEllipseItem(
|
2020-12-29 13:43:25 +00:00
|
|
|
QtCore.QRectF(-size / 2, -size / 2, size, size)
|
|
|
|
)
|
|
|
|
# if we needed transformable dot?
|
|
|
|
# dot.translate(-size*0.5, -size*0.5)
|
|
|
|
dot.setPen(pen)
|
|
|
|
dot.setBrush(brush)
|
|
|
|
dot.setParentItem(self)
|
|
|
|
|
|
|
|
# keep a static size
|
|
|
|
self.setFlag(self.ItemIgnoresTransformations)
|
|
|
|
|
|
|
|
def event(
|
|
|
|
self,
|
|
|
|
ev: QtCore.QEvent,
|
2021-07-22 00:02:09 +00:00
|
|
|
|
2022-04-01 17:49:42 +00:00
|
|
|
) -> bool:
|
2020-12-29 13:43:25 +00:00
|
|
|
if not isinstance(
|
|
|
|
ev, QtCore.QDynamicPropertyChangeEvent
|
|
|
|
) or self.curve() is None:
|
|
|
|
return False
|
|
|
|
|
2022-04-01 17:49:42 +00:00
|
|
|
# TODO: get rid of this ``.getData()`` and
|
|
|
|
# make a more pythonic api to retreive backing
|
|
|
|
# numpy arrays...
|
2020-12-29 13:43:25 +00:00
|
|
|
(x, y) = self.curve().getData()
|
|
|
|
index = self.property('index')
|
2021-06-21 20:45:27 +00:00
|
|
|
# first = self._plot._arrays['ohlc'][0]['index']
|
2020-12-29 13:43:25 +00:00
|
|
|
# first = x[0]
|
|
|
|
# i = index - first
|
2022-02-07 13:47:03 +00:00
|
|
|
if index:
|
2022-04-01 17:49:42 +00:00
|
|
|
i = round(index - x[0])
|
2022-02-07 13:47:03 +00:00
|
|
|
if i > 0 and i < len(y):
|
|
|
|
newPos = (index, y[i])
|
|
|
|
QtWidgets.QGraphicsItem.setPos(self, *newPos)
|
|
|
|
return True
|
2020-12-29 13:43:25 +00:00
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2021-06-04 15:30:15 +00:00
|
|
|
# TODO: change this into our own ``_label.Label``
|
2020-12-29 13:43:25 +00:00
|
|
|
class ContentsLabel(pg.LabelItem):
|
|
|
|
"""Label anchored to a ``ViewBox`` typically for displaying
|
|
|
|
datum-wise points from the "viewed" contents.
|
|
|
|
|
|
|
|
"""
|
2021-03-16 19:58:17 +00:00
|
|
|
_corner_anchors = {
|
|
|
|
'top': 0,
|
|
|
|
'left': 0,
|
|
|
|
'bottom': 1,
|
|
|
|
'right': 1,
|
|
|
|
}
|
|
|
|
|
|
|
|
# XXX: fyi naming here is confusing / opposite to coords
|
|
|
|
_corner_margins = {
|
2021-03-31 18:25:17 +00:00
|
|
|
('top', 'left'): (-2, lambda font_size: -font_size*0.25),
|
|
|
|
('top', 'right'): (2, lambda font_size: -font_size*0.25),
|
2021-03-16 19:58:17 +00:00
|
|
|
|
2021-03-31 18:25:17 +00:00
|
|
|
('bottom', 'left'): (-2, lambda font_size: font_size),
|
|
|
|
('bottom', 'right'): (2, lambda font_size: font_size),
|
2021-03-16 19:58:17 +00:00
|
|
|
}
|
|
|
|
|
2020-12-29 13:43:25 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
2021-07-22 00:02:09 +00:00
|
|
|
|
2021-06-21 20:45:27 +00:00
|
|
|
# chart: 'ChartPlotWidget', # noqa
|
|
|
|
view: pg.ViewBox,
|
|
|
|
|
2020-12-29 13:43:25 +00:00
|
|
|
anchor_at: str = ('top', 'right'),
|
|
|
|
justify_text: str = 'left',
|
|
|
|
font_size: Optional[int] = None,
|
2021-06-21 20:45:27 +00:00
|
|
|
|
2020-12-29 13:43:25 +00:00
|
|
|
) -> None:
|
2021-06-04 15:30:15 +00:00
|
|
|
|
|
|
|
font_size = font_size or _font_small.px_size
|
2021-03-31 18:25:17 +00:00
|
|
|
|
2020-12-29 13:43:25 +00:00
|
|
|
super().__init__(
|
|
|
|
justify=justify_text,
|
|
|
|
size=f'{str(font_size)}px'
|
|
|
|
)
|
|
|
|
|
|
|
|
# anchor to viewbox
|
2021-06-21 20:45:27 +00:00
|
|
|
self.setParentItem(view)
|
|
|
|
|
|
|
|
self.vb = view
|
|
|
|
view.scene().addItem(self)
|
2020-12-29 13:43:25 +00:00
|
|
|
|
|
|
|
v, h = anchor_at
|
2021-03-16 19:58:17 +00:00
|
|
|
index = (self._corner_anchors[h], self._corner_anchors[v])
|
|
|
|
margins = self._corner_margins[(v, h)]
|
2020-12-29 13:43:25 +00:00
|
|
|
|
|
|
|
ydim = margins[1]
|
|
|
|
if inspect.isfunction(margins[1]):
|
2021-06-04 15:30:15 +00:00
|
|
|
margins = margins[0], ydim(font_size)
|
2021-03-31 18:25:17 +00:00
|
|
|
|
2020-12-29 13:43:25 +00:00
|
|
|
self.anchor(itemPos=index, parentPos=index, offset=margins)
|
|
|
|
|
|
|
|
def update_from_ohlc(
|
|
|
|
self,
|
2021-07-22 00:02:09 +00:00
|
|
|
|
2020-12-29 13:43:25 +00:00
|
|
|
name: str,
|
|
|
|
index: int,
|
|
|
|
array: np.ndarray,
|
2021-06-21 20:45:27 +00:00
|
|
|
|
2020-12-29 13:43:25 +00:00
|
|
|
) -> None:
|
|
|
|
# this being "html" is the dumbest shit :eyeroll:
|
|
|
|
first = array[0]['index']
|
|
|
|
|
|
|
|
self.setText(
|
|
|
|
"<b>i</b>:{index}<br/>"
|
|
|
|
"<b>O</b>:{}<br/>"
|
|
|
|
"<b>H</b>:{}<br/>"
|
|
|
|
"<b>L</b>:{}<br/>"
|
|
|
|
"<b>C</b>:{}<br/>"
|
|
|
|
"<b>V</b>:{}<br/>"
|
|
|
|
"<b>wap</b>:{}".format(
|
|
|
|
*array[index - first][
|
|
|
|
['open', 'high', 'low', 'close', 'volume', 'bar_wap']
|
|
|
|
],
|
|
|
|
name=name,
|
|
|
|
index=index,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
def update_from_value(
|
|
|
|
self,
|
2021-07-22 00:02:09 +00:00
|
|
|
|
2020-12-29 13:43:25 +00:00
|
|
|
name: str,
|
|
|
|
index: int,
|
|
|
|
array: np.ndarray,
|
2021-06-21 20:45:27 +00:00
|
|
|
|
2020-12-29 13:43:25 +00:00
|
|
|
) -> None:
|
2021-06-21 20:45:27 +00:00
|
|
|
|
2020-12-29 13:43:25 +00:00
|
|
|
first = array[0]['index']
|
|
|
|
if index < array[-1]['index'] and index > first:
|
|
|
|
data = array[index - first][name]
|
|
|
|
self.setText(f"{name}: {data:.2f}")
|
|
|
|
|
|
|
|
|
2021-06-21 20:45:27 +00:00
|
|
|
class ContentsLabels:
|
|
|
|
'''Collection of labels that span a ``LinkedSplits`` set of chart plots
|
|
|
|
and can be updated from the underlying data from an x-index value sent
|
|
|
|
as input from a cursor or other query mechanism.
|
|
|
|
|
|
|
|
'''
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
linkedsplits: 'LinkedSplits', # type: ignore # noqa
|
|
|
|
|
|
|
|
) -> None:
|
|
|
|
|
|
|
|
self.linkedsplits = linkedsplits
|
|
|
|
self._labels: list[(
|
|
|
|
'CharPlotWidget', # type: ignore # noqa
|
|
|
|
str,
|
|
|
|
ContentsLabel,
|
|
|
|
Callable
|
|
|
|
)] = []
|
|
|
|
|
|
|
|
def update_labels(
|
|
|
|
self,
|
|
|
|
index: int,
|
|
|
|
|
|
|
|
) -> None:
|
|
|
|
for chart, name, label, update in self._labels:
|
|
|
|
|
2022-04-14 14:04:18 +00:00
|
|
|
flow = chart._flows[name]
|
|
|
|
array = flow.shm.array
|
|
|
|
|
2022-01-11 21:06:51 +00:00
|
|
|
if not (
|
|
|
|
index >= 0
|
|
|
|
and index < array[-1]['index']
|
|
|
|
):
|
2021-06-21 20:45:27 +00:00
|
|
|
# out of range
|
2022-02-06 17:19:05 +00:00
|
|
|
print('WTF out of range?')
|
2021-06-21 20:45:27 +00:00
|
|
|
continue
|
|
|
|
|
|
|
|
# call provided update func with data point
|
|
|
|
try:
|
|
|
|
label.show()
|
|
|
|
update(index, array)
|
|
|
|
|
|
|
|
except IndexError:
|
|
|
|
log.exception(f"Failed to update label: {name}")
|
|
|
|
|
|
|
|
def hide(self) -> None:
|
|
|
|
for chart, name, label, update in self._labels:
|
|
|
|
label.hide()
|
|
|
|
|
|
|
|
def add_label(
|
|
|
|
|
|
|
|
self,
|
|
|
|
chart: 'ChartPlotWidget', # type: ignore # noqa
|
|
|
|
name: str,
|
|
|
|
anchor_at: tuple[str, str] = ('top', 'left'),
|
|
|
|
update_func: Callable = ContentsLabel.update_from_value,
|
|
|
|
|
|
|
|
) -> ContentsLabel:
|
|
|
|
|
|
|
|
label = ContentsLabel(
|
2022-01-09 16:31:15 +00:00
|
|
|
view=chart.view,
|
2021-06-21 20:45:27 +00:00
|
|
|
anchor_at=anchor_at,
|
|
|
|
)
|
|
|
|
self._labels.append(
|
|
|
|
(chart, name, label, partial(update_func, label, name))
|
|
|
|
)
|
2021-07-26 19:35:06 +00:00
|
|
|
label.hide()
|
2021-06-21 20:45:27 +00:00
|
|
|
|
|
|
|
return label
|
|
|
|
|
|
|
|
|
2021-01-03 15:46:28 +00:00
|
|
|
class Cursor(pg.GraphicsObject):
|
2022-03-11 20:16:31 +00:00
|
|
|
'''
|
|
|
|
Multi-plot cursor for use on a ``LinkedSplits`` chart (set).
|
2020-12-29 13:43:25 +00:00
|
|
|
|
2021-07-22 00:02:09 +00:00
|
|
|
'''
|
2020-12-29 13:43:25 +00:00
|
|
|
def __init__(
|
2021-06-21 20:45:27 +00:00
|
|
|
|
2020-12-29 13:43:25 +00:00
|
|
|
self,
|
2021-06-15 22:25:03 +00:00
|
|
|
linkedsplits: 'LinkedSplits', # noqa
|
2020-12-29 13:43:25 +00:00
|
|
|
digits: int = 0
|
2021-06-21 20:45:27 +00:00
|
|
|
|
2020-12-29 13:43:25 +00:00
|
|
|
) -> None:
|
2021-06-21 20:45:27 +00:00
|
|
|
|
2020-12-29 13:43:25 +00:00
|
|
|
super().__init__()
|
2021-06-21 20:45:27 +00:00
|
|
|
|
|
|
|
self.linked = linkedsplits
|
|
|
|
self.graphics: dict[str, pg.GraphicsObject] = {}
|
2022-03-11 20:16:31 +00:00
|
|
|
self.plots: list['PlotChartWidget'] = [] # type: ignore # noqa
|
2021-06-21 20:45:27 +00:00
|
|
|
self.active_plot = None
|
|
|
|
self.digits: int = digits
|
|
|
|
self._datum_xy: tuple[int, float] = (0, 0)
|
|
|
|
|
|
|
|
self._hovered: set[pg.GraphicsObject] = set()
|
|
|
|
self._trackers: set[pg.GraphicsObject] = set()
|
|
|
|
|
2020-12-29 13:43:25 +00:00
|
|
|
# XXX: not sure why these are instance variables?
|
|
|
|
# It's not like we can change them on the fly..?
|
|
|
|
self.pen = pg.mkPen(
|
|
|
|
color=hcolor('default'),
|
|
|
|
style=QtCore.Qt.DashLine,
|
|
|
|
)
|
|
|
|
self.lines_pen = pg.mkPen(
|
2021-03-12 02:44:35 +00:00
|
|
|
color=hcolor('davies'),
|
2020-12-29 13:43:25 +00:00
|
|
|
style=QtCore.Qt.DashLine,
|
|
|
|
)
|
2021-01-03 15:46:28 +00:00
|
|
|
|
2021-01-23 03:59:00 +00:00
|
|
|
# value used for rounding y-axis discreet tick steps
|
|
|
|
# computing once, up front, here cuz why not
|
2021-06-21 20:45:27 +00:00
|
|
|
self._y_incr_mult = 1 / self.linked._symbol.tick_size
|
2021-01-23 03:59:00 +00:00
|
|
|
|
2021-02-08 12:03:28 +00:00
|
|
|
# line width in view coordinates
|
|
|
|
self._lw = self.pixelWidth() * self.lines_pen.width()
|
|
|
|
|
2021-03-13 22:29:35 +00:00
|
|
|
# xhair label's color name
|
|
|
|
self.label_color: str = 'default'
|
|
|
|
|
|
|
|
self._y_label_update: bool = True
|
|
|
|
|
2021-06-21 20:45:27 +00:00
|
|
|
self.contents_labels = ContentsLabels(self.linked)
|
|
|
|
self._in_query_mode: bool = False
|
|
|
|
|
|
|
|
@property
|
|
|
|
def in_query_mode(self) -> bool:
|
|
|
|
return self._in_query_mode
|
|
|
|
|
|
|
|
@in_query_mode.setter
|
|
|
|
def in_query_mode(self, value: bool) -> None:
|
|
|
|
if self._in_query_mode and not value:
|
|
|
|
|
2021-06-22 12:18:18 +00:00
|
|
|
# edge trigger "off" hide all labels
|
2021-06-21 20:45:27 +00:00
|
|
|
self.contents_labels.hide()
|
|
|
|
|
2021-06-22 12:18:18 +00:00
|
|
|
elif not self._in_query_mode and value:
|
|
|
|
# edge trigger "on" hide all labels
|
|
|
|
self.contents_labels.update_labels(self._datum_xy[0])
|
|
|
|
|
2021-06-21 20:45:27 +00:00
|
|
|
self._in_query_mode = value
|
|
|
|
|
2021-01-03 15:46:28 +00:00
|
|
|
def add_hovered(
|
|
|
|
self,
|
|
|
|
item: pg.GraphicsObject,
|
|
|
|
) -> None:
|
|
|
|
assert getattr(item, 'delete'), f"{item} must define a ``.delete()``"
|
|
|
|
self._hovered.add(item)
|
2020-12-29 13:43:25 +00:00
|
|
|
|
|
|
|
def add_plot(
|
|
|
|
self,
|
|
|
|
plot: 'ChartPlotWidget', # noqa
|
|
|
|
digits: int = 0,
|
2022-01-21 13:39:03 +00:00
|
|
|
|
2020-12-29 13:43:25 +00:00
|
|
|
) -> None:
|
2022-01-21 13:39:03 +00:00
|
|
|
'''
|
|
|
|
Add chart to tracked set such that a cross-hair and possibly
|
|
|
|
curve tracking cursor can be drawn on the plot.
|
|
|
|
|
|
|
|
'''
|
2020-12-29 13:43:25 +00:00
|
|
|
# add ``pg.graphicsItems.InfiniteLine``s
|
|
|
|
# vertical and horizonal lines and a y-axis label
|
2021-02-08 12:03:28 +00:00
|
|
|
|
2020-12-29 13:43:25 +00:00
|
|
|
vl = plot.addLine(x=0, pen=self.lines_pen, movable=False)
|
2021-07-21 19:50:09 +00:00
|
|
|
vl.setCacheMode(QtWidgets.QGraphicsItem.DeviceCoordinateCache)
|
2020-12-29 13:43:25 +00:00
|
|
|
|
|
|
|
hl = plot.addLine(y=0, pen=self.lines_pen, movable=False)
|
2021-07-21 19:50:09 +00:00
|
|
|
hl.setCacheMode(QtWidgets.QGraphicsItem.DeviceCoordinateCache)
|
2020-12-29 13:43:25 +00:00
|
|
|
hl.hide()
|
|
|
|
|
|
|
|
yl = YAxisLabel(
|
2021-02-08 12:03:28 +00:00
|
|
|
chart=plot,
|
2022-01-21 13:39:03 +00:00
|
|
|
# parent=plot.getAxis('right'),
|
|
|
|
parent=plot.pi_overlay.get_axis(plot.plotItem, 'right'),
|
2020-12-29 13:43:25 +00:00
|
|
|
digits=digits or self.digits,
|
|
|
|
opacity=_ch_label_opac,
|
2021-03-13 22:29:35 +00:00
|
|
|
bg_color=self.label_color,
|
2020-12-29 13:43:25 +00:00
|
|
|
)
|
|
|
|
yl.hide() # on startup if mouse is off screen
|
|
|
|
|
|
|
|
# TODO: checkout what ``.sigDelayed`` can be used for
|
|
|
|
# (emitted once a sufficient delay occurs in mouse movement)
|
|
|
|
px_moved = pg.SignalProxy(
|
|
|
|
plot.scene().sigMouseMoved,
|
|
|
|
rateLimit=_mouse_rate_limit,
|
|
|
|
slot=self.mouseMoved,
|
|
|
|
delay=_debounce_delay,
|
|
|
|
)
|
2022-04-01 17:49:42 +00:00
|
|
|
|
2020-12-29 13:43:25 +00:00
|
|
|
px_enter = pg.SignalProxy(
|
|
|
|
plot.sig_mouse_enter,
|
|
|
|
rateLimit=_mouse_rate_limit,
|
|
|
|
slot=lambda: self.mouseAction('Enter', plot),
|
|
|
|
delay=_debounce_delay,
|
|
|
|
)
|
|
|
|
px_leave = pg.SignalProxy(
|
|
|
|
plot.sig_mouse_leave,
|
|
|
|
rateLimit=_mouse_rate_limit,
|
|
|
|
slot=lambda: self.mouseAction('Leave', plot),
|
|
|
|
delay=_debounce_delay,
|
|
|
|
)
|
|
|
|
self.graphics[plot] = {
|
|
|
|
'vl': vl,
|
|
|
|
'hl': hl,
|
|
|
|
'yl': yl,
|
|
|
|
'px': (px_moved, px_enter, px_leave),
|
|
|
|
}
|
|
|
|
self.plots.append(plot)
|
|
|
|
|
|
|
|
# Determine where to place x-axis label.
|
|
|
|
# Place below the last plot by default, ow
|
|
|
|
# keep x-axis right below main chart
|
|
|
|
plot_index = -1 if _xaxis_at == 'bottom' else 0
|
|
|
|
|
2022-01-24 20:14:06 +00:00
|
|
|
# ONLY create an x-axis label for the cursor
|
|
|
|
# if this plot owns the 'bottom' axis.
|
2022-01-21 13:39:03 +00:00
|
|
|
# if 'bottom' in plot.plotItem.axes:
|
|
|
|
if plot.linked.xaxis_chart is plot:
|
|
|
|
xlabel = self.xaxis_label = XAxisLabel(
|
2022-01-24 20:14:06 +00:00
|
|
|
parent=self.plots[plot_index].getAxis('bottom'),
|
2022-03-11 20:16:31 +00:00
|
|
|
# parent=self.plots[plot_index].pi_overlay.get_axis(
|
|
|
|
# plot.plotItem, 'bottom'
|
|
|
|
# ),
|
|
|
|
|
2022-01-24 20:14:06 +00:00
|
|
|
opacity=_ch_label_opac,
|
|
|
|
bg_color=self.label_color,
|
|
|
|
)
|
|
|
|
# place label off-screen during startup
|
2022-01-21 13:39:03 +00:00
|
|
|
xlabel.setPos(
|
|
|
|
self.plots[0].mapFromView(QPointF(0, 0))
|
|
|
|
)
|
|
|
|
xlabel.show()
|
2020-12-29 13:43:25 +00:00
|
|
|
|
|
|
|
def add_curve_cursor(
|
|
|
|
self,
|
|
|
|
plot: 'ChartPlotWidget', # noqa
|
|
|
|
curve: 'PlotCurveItem', # noqa
|
2022-01-21 13:39:03 +00:00
|
|
|
|
2020-12-29 13:43:25 +00:00
|
|
|
) -> LineDot:
|
|
|
|
# if this plot contains curves add line dot "cursors" to denote
|
|
|
|
# the current sample under the mouse
|
2022-04-14 14:04:18 +00:00
|
|
|
main_flow = plot._flows[plot.name]
|
|
|
|
# read out last index
|
|
|
|
i = main_flow.shm.array[-1]['index']
|
2021-01-03 15:46:28 +00:00
|
|
|
cursor = LineDot(
|
|
|
|
curve,
|
2022-04-14 14:04:18 +00:00
|
|
|
index=i,
|
2021-01-03 15:46:28 +00:00
|
|
|
plot=plot
|
|
|
|
)
|
2020-12-29 13:43:25 +00:00
|
|
|
plot.addItem(cursor)
|
|
|
|
self.graphics[plot].setdefault('cursors', []).append(cursor)
|
|
|
|
return cursor
|
|
|
|
|
|
|
|
def mouseAction(self, action, plot): # noqa
|
2021-01-01 17:59:31 +00:00
|
|
|
log.debug(f"{(action, plot.name)}")
|
2020-12-29 13:43:25 +00:00
|
|
|
if action == 'Enter':
|
|
|
|
self.active_plot = plot
|
|
|
|
|
|
|
|
# show horiz line and y-label
|
|
|
|
self.graphics[plot]['hl'].show()
|
|
|
|
self.graphics[plot]['yl'].show()
|
|
|
|
|
|
|
|
else: # Leave
|
|
|
|
|
|
|
|
# hide horiz line and y-label
|
|
|
|
self.graphics[plot]['hl'].hide()
|
|
|
|
self.graphics[plot]['yl'].hide()
|
|
|
|
|
|
|
|
def mouseMoved(
|
|
|
|
self,
|
2022-01-11 21:06:51 +00:00
|
|
|
coords: tuple[QPointF], # noqa
|
|
|
|
|
|
|
|
) -> None:
|
|
|
|
'''
|
|
|
|
Update horizonal and vertical lines when mouse moves inside
|
2020-12-29 13:43:25 +00:00
|
|
|
either the main chart or any indicator subplot.
|
2022-01-11 21:06:51 +00:00
|
|
|
|
|
|
|
'''
|
|
|
|
pos = coords[0]
|
2020-12-29 13:43:25 +00:00
|
|
|
|
|
|
|
# find position inside active plot
|
|
|
|
try:
|
|
|
|
# map to view coordinate system
|
|
|
|
mouse_point = self.active_plot.mapToView(pos)
|
|
|
|
except AttributeError:
|
|
|
|
# mouse was not on active plot
|
|
|
|
return
|
|
|
|
|
|
|
|
x, y = mouse_point.x(), mouse_point.y()
|
|
|
|
plot = self.active_plot
|
|
|
|
|
|
|
|
# Update x if cursor changed after discretization calc
|
|
|
|
# (this saves draw cycles on small mouse moves)
|
2021-01-23 03:59:00 +00:00
|
|
|
last_ix, last_iy = self._datum_xy
|
|
|
|
|
2020-12-29 13:43:25 +00:00
|
|
|
ix = round(x) # since bars are centered around index
|
|
|
|
|
2022-01-21 13:39:03 +00:00
|
|
|
# px perfect...
|
|
|
|
line_offset = self._lw / 2
|
|
|
|
|
2021-01-23 03:59:00 +00:00
|
|
|
# round y value to nearest tick step
|
|
|
|
m = self._y_incr_mult
|
|
|
|
iy = round(y * m) / m
|
2022-01-21 13:39:03 +00:00
|
|
|
vl_y = iy - line_offset
|
2021-02-08 12:03:28 +00:00
|
|
|
|
2021-03-18 17:27:49 +00:00
|
|
|
# update y-range items
|
2021-03-14 16:28:11 +00:00
|
|
|
if iy != last_iy:
|
2021-01-23 03:59:00 +00:00
|
|
|
|
2021-03-14 16:28:11 +00:00
|
|
|
if self._y_label_update:
|
|
|
|
self.graphics[self.active_plot]['yl'].update_label(
|
2022-01-21 13:39:03 +00:00
|
|
|
# abs_pos=plot.mapFromView(QPointF(ix, iy)),
|
|
|
|
abs_pos=plot.mapFromView(QPointF(ix, vl_y)),
|
2021-03-14 16:28:11 +00:00
|
|
|
value=iy
|
|
|
|
)
|
2021-01-23 03:59:00 +00:00
|
|
|
|
2021-03-18 17:27:49 +00:00
|
|
|
# only update horizontal xhair line if label is enabled
|
2022-01-21 13:39:03 +00:00
|
|
|
# self.graphics[plot]['hl'].setY(iy)
|
|
|
|
self.graphics[plot]['hl'].setY(vl_y)
|
2021-03-18 17:27:49 +00:00
|
|
|
|
2021-01-23 03:59:00 +00:00
|
|
|
# update all trackers
|
|
|
|
for item in self._trackers:
|
|
|
|
item.on_tracked_source(ix, iy)
|
2021-01-03 15:46:28 +00:00
|
|
|
|
2021-01-23 03:59:00 +00:00
|
|
|
if ix != last_ix:
|
2021-06-21 20:45:27 +00:00
|
|
|
|
|
|
|
if self.in_query_mode:
|
|
|
|
# show contents labels on all linked charts and update
|
|
|
|
# with cursor movement
|
|
|
|
self.contents_labels.update_labels(ix)
|
|
|
|
|
2022-01-11 21:06:51 +00:00
|
|
|
vl_x = ix + line_offset
|
2020-12-29 13:43:25 +00:00
|
|
|
for plot, opts in self.graphics.items():
|
|
|
|
|
2021-02-08 12:03:28 +00:00
|
|
|
# move the vertical line to the current "center of bar"
|
2022-01-11 21:06:51 +00:00
|
|
|
opts['vl'].setX(vl_x)
|
2021-02-08 12:03:28 +00:00
|
|
|
|
2020-12-29 13:43:25 +00:00
|
|
|
# update all subscribed curve dots
|
|
|
|
for cursor in opts.get('cursors', ()):
|
|
|
|
cursor.setIndex(ix)
|
|
|
|
|
2022-02-06 17:19:05 +00:00
|
|
|
# Update the label on the bottom of the crosshair.
|
2022-01-11 21:06:51 +00:00
|
|
|
# TODO: make this an up-front calc that we update
|
2022-02-06 17:19:05 +00:00
|
|
|
# on axis-widget resize events instead of on every mouse
|
|
|
|
# update cylce.
|
|
|
|
|
2022-01-11 21:06:51 +00:00
|
|
|
# left axis offset width for calcuating
|
|
|
|
# absolute x-axis label placement.
|
|
|
|
left_axis_width = 0
|
2022-02-06 17:19:05 +00:00
|
|
|
if len(plot.pi_overlay.overlays):
|
|
|
|
# breakpoint()
|
|
|
|
lefts = plot.pi_overlay.get_axes('left')
|
|
|
|
if lefts:
|
|
|
|
for left in lefts:
|
|
|
|
left_axis_width += left.width()
|
2022-01-21 13:39:03 +00:00
|
|
|
|
|
|
|
# map back to abs (label-local) coordinates
|
|
|
|
self.xaxis_label.update_label(
|
|
|
|
abs_pos=(
|
|
|
|
plot.mapFromView(QPointF(vl_x, iy)) -
|
|
|
|
QPointF(left_axis_width, 0)
|
|
|
|
),
|
|
|
|
value=ix,
|
|
|
|
)
|
2020-12-29 13:43:25 +00:00
|
|
|
|
2021-01-23 03:59:00 +00:00
|
|
|
self._datum_xy = ix, iy
|
2020-12-29 13:43:25 +00:00
|
|
|
|
2021-02-21 16:45:24 +00:00
|
|
|
def boundingRect(self) -> QRectF:
|
2020-12-29 13:43:25 +00:00
|
|
|
try:
|
|
|
|
return self.active_plot.boundingRect()
|
|
|
|
except AttributeError:
|
|
|
|
return self.plots[0].boundingRect()
|
2021-02-21 16:45:24 +00:00
|
|
|
|
2021-03-14 16:28:11 +00:00
|
|
|
def show_xhair(
|
|
|
|
self,
|
|
|
|
y_label_level: float = None,
|
|
|
|
) -> None:
|
2021-05-20 20:10:42 +00:00
|
|
|
|
|
|
|
plot = self.active_plot
|
|
|
|
if not plot:
|
|
|
|
return
|
|
|
|
|
|
|
|
g = self.graphics[plot]
|
2021-02-21 16:45:24 +00:00
|
|
|
# show horiz line and y-label
|
|
|
|
g['hl'].show()
|
|
|
|
g['vl'].show()
|
|
|
|
|
2021-03-14 16:28:11 +00:00
|
|
|
self._y_label_update = True
|
2021-03-13 22:29:35 +00:00
|
|
|
yl = g['yl']
|
|
|
|
# yl.fg_color = pg.mkColor(hcolor('black'))
|
|
|
|
# yl.bg_color = pg.mkColor(hcolor(self.label_color))
|
2021-03-14 16:28:11 +00:00
|
|
|
if y_label_level:
|
|
|
|
yl.update_from_data(0, y_label_level, _save_last=False)
|
|
|
|
|
2021-03-13 22:29:35 +00:00
|
|
|
yl.show()
|
|
|
|
|
|
|
|
def hide_xhair(
|
|
|
|
self,
|
|
|
|
hide_label: bool = False,
|
|
|
|
y_label_level: float = None,
|
2021-03-18 17:27:49 +00:00
|
|
|
just_vertical: bool = False,
|
2021-03-13 22:29:35 +00:00
|
|
|
fg_color: str = None,
|
|
|
|
# bg_color: str = 'papas_special',
|
|
|
|
) -> None:
|
2021-02-21 16:45:24 +00:00
|
|
|
g = self.graphics[self.active_plot]
|
2021-03-18 17:27:49 +00:00
|
|
|
|
|
|
|
hl = g['hl']
|
|
|
|
if not just_vertical:
|
|
|
|
hl.hide()
|
|
|
|
|
2021-02-21 16:45:24 +00:00
|
|
|
g['vl'].hide()
|
2021-03-13 22:29:35 +00:00
|
|
|
|
2021-03-14 16:28:11 +00:00
|
|
|
# only disable cursor y-label updates
|
|
|
|
# if we're highlighting a line
|
2021-03-13 22:29:35 +00:00
|
|
|
yl = g['yl']
|
|
|
|
|
|
|
|
if hide_label:
|
|
|
|
yl.hide()
|
2021-03-14 16:28:11 +00:00
|
|
|
|
2021-03-13 22:29:35 +00:00
|
|
|
elif y_label_level:
|
2021-03-14 16:28:11 +00:00
|
|
|
yl.update_from_data(0, y_label_level, _save_last=False)
|
2021-03-18 17:27:49 +00:00
|
|
|
hl.setY(y_label_level)
|
2021-03-13 22:29:35 +00:00
|
|
|
|
|
|
|
if fg_color is not None:
|
|
|
|
yl.fg_color = pg.mkColor(hcolor(fg_color))
|
|
|
|
yl.bg_color = pg.mkColor(hcolor('papas_special'))
|