Support func ops on field data, extend anchor logics

basic_orders
Tyler Goodlet 2021-03-13 19:29:52 -05:00
parent c71a3e0fc5
commit c75dacb239
1 changed files with 46 additions and 14 deletions

View File

@ -18,13 +18,13 @@
Non-shitty labels that don't re-invent the wheel. Non-shitty labels that don't re-invent the wheel.
""" """
from inspect import isfunction
from typing import Callable from typing import Callable
import pyqtgraph as pg import pyqtgraph as pg
from PyQt5 import QtGui from PyQt5 import QtGui
from PyQt5.QtCore import QPointF, QRectF from PyQt5.QtCore import QPointF, QRectF
from ._style import ( from ._style import (
DpiAwareFont, DpiAwareFont,
hcolor, hcolor,
@ -37,18 +37,16 @@ def vbr_left(label) -> Callable[..., float]:
leftmost point of the containing view box. leftmost point of the containing view box.
""" """
return label.vbr().left
def viewbox_left():
return label.vbr().left()
return viewbox_left
def right_axis( def right_axis(
chart: 'ChartPlotWidget', # noqa chart: 'ChartPlotWidget', # noqa
label: 'Label', # noqa label: 'Label', # noqa
side: str = 'left',
offset: float = 10, offset: float = 10,
avoid_book: bool = True,
width: float = None, width: float = None,
) -> Callable[..., float]: ) -> Callable[..., float]:
@ -59,6 +57,9 @@ def right_axis(
""" """
ryaxis = chart.getAxis('right') ryaxis = chart.getAxis('right')
if side == 'left':
if avoid_book:
def right_axis_offset_by_w() -> float: def right_axis_offset_by_w() -> float:
# l1 spread graphics x-size # l1 spread graphics x-size
@ -69,8 +70,23 @@ def right_axis(
return ryaxis.pos().x() - right_offset return ryaxis.pos().x() - right_offset
else:
def right_axis_offset_by_w() -> float:
return ryaxis.pos().x() - (label.w + offset)
return right_axis_offset_by_w return right_axis_offset_by_w
elif 'right':
# axis_offset = ryaxis.style['tickTextOffset'][0]
def on_axis() -> float:
return ryaxis.pos().x() # + axis_offset - 2
return on_axis
class Label: class Label:
""" """
@ -200,7 +216,23 @@ class Label:
self._fmt_str = fmt_str self._fmt_str = fmt_str
def format(self, **fields: dict) -> str: def format(self, **fields: dict) -> str:
text = self._fmt_str.format(**fields)
out = {}
# this is hacky support for single depth
# calcs of field data from field data
# ex. to calculate a $value = price * size
for k, v in fields.items():
if isfunction(v):
out[k] = v(fields)
else:
out[k] = v
text = self._fmt_str.format(**out)
# for large numbers with a thousands place
text = text.replace(',', ' ')
self.txt.setPlainText(text) self.txt.setPlainText(text)
def render(self) -> None: def render(self) -> None: