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.
"""
from inspect import isfunction
from typing import Callable
import pyqtgraph as pg
from PyQt5 import QtGui
from PyQt5.QtCore import QPointF, QRectF
from ._style import (
DpiAwareFont,
hcolor,
@ -37,18 +37,16 @@ def vbr_left(label) -> Callable[..., float]:
leftmost point of the containing view box.
"""
def viewbox_left():
return label.vbr().left()
return viewbox_left
return label.vbr().left
def right_axis(
chart: 'ChartPlotWidget', # noqa
label: 'Label', # noqa
side: str = 'left',
offset: float = 10,
avoid_book: bool = True,
width: float = None,
) -> Callable[..., float]:
@ -59,6 +57,9 @@ def right_axis(
"""
ryaxis = chart.getAxis('right')
if side == 'left':
if avoid_book:
def right_axis_offset_by_w() -> float:
# l1 spread graphics x-size
@ -69,8 +70,23 @@ def right_axis(
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
elif 'right':
# axis_offset = ryaxis.style['tickTextOffset'][0]
def on_axis() -> float:
return ryaxis.pos().x() # + axis_offset - 2
return on_axis
class Label:
"""
@ -200,7 +216,23 @@ class Label:
self._fmt_str = fmt_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)
def render(self) -> None: