Skip to content

Commit

Permalink
Simplify L1 labels for multicharts
Browse files Browse the repository at this point in the history
Instead of having the l1 lines be inside the view space, move them to be
inside their respective axis (with only a 16 unit portion inside the
view) such that the clear price label can overlay with them nicely
without obscuring; this is much better suited to multiple adjacent
y-axes and in general is simpler and less noisy.

Further `L1Labels` + `LevelLabel` style tweaks:
- adjust `.rect` positioning to be "right" (i.e. inside the parent
  y-axis) with a slight 16 unit shift toward the viewbox (using the new
  `._x_br_offset`) to allow seeing each level label's line even when the
  clearing price label is positioned at that same level.
- add a newline's worth of vertical space to each of the bid/ask labels
  so that L1 labels' text content isn't ever obscured by the clear price
  label.
- set a low (10) z-value to ensure l1 labels are always placed
  underneath the clear price label.
- always fill the label rect with the chosen background color.
- make labels fully opaque so as to always make them hide the parent
  axes' `.tickStrings()` contents.
- make default color the "default" from the global scheme.
- drop the "price" part from the l1 label text contents, just show the
  book-queue's amount (in dst asset's units, aka the potential clearing vlm).
  • Loading branch information
goodboy committed Dec 23, 2022
1 parent cb00669 commit 84f1930
Showing 1 changed file with 45 additions and 24 deletions.
69 changes: 45 additions & 24 deletions piker/ui/_l1.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,20 @@


class LevelLabel(YAxisLabel):
"""Y-axis (vertically) oriented, horizontal label that sticks to
'''
Y-axis (vertically) oriented, horizontal label that sticks to
where it's placed despite chart resizing and supports displaying
multiple fields.
TODO: replace the rectangle-text part with our new ``Label`` type.
"""
_x_margin = 0
_y_margin = 0
'''
_x_br_offset: float = -16
_x_txt_h_scaling: float = 2

# adjustment "further away from" anchor point
_x_offset = 9
_x_offset = 0
_y_offset = 0

# fields to be displayed in the label string
Expand All @@ -58,12 +59,12 @@ def __init__(
chart,
parent,

color: str = 'bracket',
color: str = 'default_light',

orient_v: str = 'bottom',
orient_h: str = 'left',
orient_h: str = 'right',

opacity: float = 0,
opacity: float = 1,

# makes order line labels offset from their parent axis
# such that they don't collide with the L1/L2 lines/prices
Expand Down Expand Up @@ -99,21 +100,26 @@ def __init__(

self._h_shift = {
'left': -1.,
'right': 0.
'right': 0.,
}[orient_h]

self.fields = self._fields.copy()
# ensure default format fields are in correct
self.set_fmt_str(self._fmt_str, self.fields)

self.setZValue(10)

@property
def color(self):
return self._hcolor

@color.setter
def color(self, color: str) -> None:
self._hcolor = color
self._pen = self.pen = pg.mkPen(hcolor(color))
self._pen = self.pen = pg.mkPen(
hcolor(color),
width=3,
)

def update_on_resize(self, vr, r):
"""Tiis is a ``.sigRangeChanged()`` handler.
Expand All @@ -125,10 +131,11 @@ def update_fields(
self,
fields: dict = None,
) -> None:
"""Update the label's text contents **and** position from
'''
Update the label's text contents **and** position from
a view box coordinate datum.
"""
'''
self.fields.update(fields)
level = self.fields['level']

Expand Down Expand Up @@ -175,7 +182,8 @@ def set_label_str(
fields: dict,
):
# use space as e3 delim
self.label_str = self._fmt_str.format(**fields).replace(',', ' ')
self.label_str = self._fmt_str.format(
**fields).replace(',', ' ')

br = self.boundingRect()
h, w = br.height(), br.width()
Expand All @@ -188,14 +196,14 @@ def draw(
self,
p: QtGui.QPainter,
rect: QtCore.QRectF

) -> None:
p.setPen(self._pen)

p.setPen(self._pen)
rect = self.rect

if self._orient_v == 'bottom':
lp, rp = rect.topLeft(), rect.topRight()
# p.drawLine(rect.topLeft(), rect.topRight())

elif self._orient_v == 'top':
lp, rp = rect.bottomLeft(), rect.bottomRight()
Expand All @@ -209,6 +217,11 @@ def draw(
])
)

p.fillRect(
self.rect,
self.bg_color,
)

def highlight(self, pen) -> None:
self._pen = pen
self.update()
Expand Down Expand Up @@ -247,9 +260,10 @@ def set_label_str(


class L1Labels:
"""Level 1 bid ask labels for dynamic update on price-axis.
'''
Level 1 bid ask labels for dynamic update on price-axis.
"""
'''
def __init__(
self,
plotitem: PlotItem,
Expand All @@ -265,15 +279,17 @@ def __init__(
'chart': plotitem,
'parent': raxis,

'opacity': 1,
'opacity': .9,
'font_size': font_size,
'fg_color': chart.pen_color,
'bg_color': chart.view_color,
'fg_color': 'default_light',
'bg_color': chart.view_color, # normally 'papas_special'
}

# TODO: add humanized source-asset
# info format.
fmt_str = (
' {size:.{size_digits}f} x '
'{level:,.{level_digits}f} '
' {size:.{size_digits}f} u'
# '{level:,.{level_digits}f} '
)
fields = {
'level': 0,
Expand All @@ -286,12 +302,17 @@ def __init__(
orient_v='bottom',
**kwargs,
)
bid.set_fmt_str(fmt_str=fmt_str, fields=fields)
bid.set_fmt_str(
fmt_str='\n' + fmt_str,
fields=fields,
)
bid.show()

ask = self.ask_label = L1Label(
orient_v='top',
**kwargs,
)
ask.set_fmt_str(fmt_str=fmt_str, fields=fields)
ask.set_fmt_str(
fmt_str=fmt_str,
fields=fields)
ask.show()

0 comments on commit 84f1930

Please sign in to comment.