Add support for "humanized" axes tick values

overlayed_dvlm
Tyler Goodlet 2022-01-14 09:14:58 -05:00
parent a40e949940
commit 8911c3c8ed
1 changed files with 29 additions and 11 deletions

View File

@ -18,7 +18,7 @@
Chart axes graphics and behavior. Chart axes graphics and behavior.
""" """
import functools from functools import partial
from typing import List, Tuple, Optional from typing import List, Tuple, Optional
from math import floor from math import floor
@ -29,6 +29,7 @@ from PyQt5.QtCore import QPointF
from ._style import DpiAwareFont, hcolor, _font from ._style import DpiAwareFont, hcolor, _font
from ..data._source import float_digits from ..data._source import float_digits
from ..calc import humanize
_axis_pen = pg.mkPen(hcolor('bracket')) _axis_pen = pg.mkPen(hcolor('bracket'))
@ -92,6 +93,18 @@ class Axis(pg.AxisItem):
class PriceAxis(Axis): class PriceAxis(Axis):
def __init__(
self,
*args,
humanize: bool = True,
digits: int = 2,
**kwargs
) -> None:
super().__init__(*args, **kwargs)
self.humanize = humanize
self.digits = digits
def size_to_values(self) -> None: def size_to_values(self) -> None:
self.setWidth(self.typical_br.width()) self.setWidth(self.typical_br.width())
@ -103,20 +116,25 @@ class PriceAxis(Axis):
scale, scale,
spacing, spacing,
): ):
# TODO: figure out how to enforce min tick spacing by passing it
# TODO: figure out how to enforce min tick spacing by passing # into the parent type
# it into the parent type digits = max(
digits = max(float_digits(spacing * scale), self._min_tick) float_digits(spacing * scale),
self._min_tick,
)
# print(f'vals: {vals}\nscale: {scale}\nspacing: {spacing}') # print(f'vals: {vals}\nscale: {scale}\nspacing: {spacing}')
# print(f'digits: {digits}') # print(f'digits: {digits}')
return [ if not self.humanize:
('{value:,.{digits}f}').format( return [
digits=digits, ('{value:,.{digits}f}').format(
value=v, digits=digits,
).replace(',', ' ') for v in vals value=v,
] ).replace(',', ' ') for v in vals
]
else:
return list(map(partial(humanize, digits=self.digits), vals))
class DynamicDateAxis(Axis): class DynamicDateAxis(Axis):