From 8911c3c8ed5b10dad2fb78647787aafb061b13cf Mon Sep 17 00:00:00 2001 From: Tyler Goodlet Date: Fri, 14 Jan 2022 09:14:58 -0500 Subject: [PATCH] Add support for "humanized" axes tick values --- piker/ui/_axes.py | 40 +++++++++++++++++++++++++++++----------- 1 file changed, 29 insertions(+), 11 deletions(-) diff --git a/piker/ui/_axes.py b/piker/ui/_axes.py index 67df0138..bac17124 100644 --- a/piker/ui/_axes.py +++ b/piker/ui/_axes.py @@ -18,7 +18,7 @@ Chart axes graphics and behavior. """ -import functools +from functools import partial from typing import List, Tuple, Optional from math import floor @@ -29,6 +29,7 @@ from PyQt5.QtCore import QPointF from ._style import DpiAwareFont, hcolor, _font from ..data._source import float_digits +from ..calc import humanize _axis_pen = pg.mkPen(hcolor('bracket')) @@ -92,6 +93,18 @@ class Axis(pg.AxisItem): 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: self.setWidth(self.typical_br.width()) @@ -103,20 +116,25 @@ class PriceAxis(Axis): scale, spacing, ): - - # TODO: figure out how to enforce min tick spacing by passing - # it into the parent type - digits = max(float_digits(spacing * scale), self._min_tick) + # TODO: figure out how to enforce min tick spacing by passing it + # into the parent type + digits = max( + float_digits(spacing * scale), + self._min_tick, + ) # print(f'vals: {vals}\nscale: {scale}\nspacing: {spacing}') # print(f'digits: {digits}') - return [ - ('{value:,.{digits}f}').format( - digits=digits, - value=v, - ).replace(',', ' ') for v in vals - ] + if not self.humanize: + return [ + ('{value:,.{digits}f}').format( + digits=digits, + value=v, + ).replace(',', ' ') for v in vals + ] + else: + return list(map(partial(humanize, digits=self.digits), vals)) class DynamicDateAxis(Axis):