From b5d59fd6f16f416438aeb4f574c772fd36e38fd0 Mon Sep 17 00:00:00 2001 From: Michael Thies Date: Mon, 25 Dec 2023 12:11:26 +0100 Subject: [PATCH] data log widgets: Add scale_factor and unit symbol for ChartWidget --- example/ui_logging_showcase.py | 8 ++++---- shc/web/log_widgets.py | 11 +++++++++-- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/example/ui_logging_showcase.py b/example/ui_logging_showcase.py index 47af22ef..3293c06d 100644 --- a/example/ui_logging_showcase.py +++ b/example/ui_logging_showcase.py @@ -71,13 +71,13 @@ async def new_bool(_v, _o): index_page.new_segment() index_page.add_item(shc.web.log_widgets.ChartWidget(datetime.timedelta(minutes=5), [ - ChartDataSpec(random_float_log, "random float") + ChartDataSpec(random_float_log, "random float", scale_factor=10) ])) index_page.add_item(shc.web.log_widgets.ChartWidget(datetime.timedelta(minutes=5), [ - ChartDataSpec(random_float_log, "avg", aggregation=AggregationMethod.AVERAGE), - ChartDataSpec(random_float_log, "min", aggregation=AggregationMethod.MINIMUM), - ChartDataSpec(random_float_log, "max", aggregation=AggregationMethod.MAXIMUM), + ChartDataSpec(random_float_log, "avg", aggregation=AggregationMethod.AVERAGE, unit_symbol="°C"), + ChartDataSpec(random_float_log, "min", aggregation=AggregationMethod.MINIMUM, unit_symbol="°C"), + ChartDataSpec(random_float_log, "max", aggregation=AggregationMethod.MAXIMUM, unit_symbol="°C"), ])) diff --git a/shc/web/log_widgets.py b/shc/web/log_widgets.py index 4937de61..17361e3e 100644 --- a/shc/web/log_widgets.py +++ b/shc/web/log_widgets.py @@ -130,6 +130,10 @@ class ChartDataSpec: aggregation: Optional[AggregationMethod] = None #: If `aggregation` is not None: The time span/period of the single aggregation intervals and aggregated datapoints aggregation_interval: Optional[datetime.timedelta] = None + #: Multiply all logged values by this factor before showing in the chart (e.g. for unit conversion purposes) + scale_factor: float = 1.0 + #: Unit symbol to be shown after the value in the Chart tooltip + unit_symbol: str = "" class ChartWidget(WebPageItem): @@ -206,12 +210,15 @@ def __init__(self, interval: datetime.timedelta, data_spec: List[ChartDataSpec] aggregation_interval = interval / 10 is_aggregated = spec.aggregation is not None connector = LoggingWebUIView(spec.variable, interval, spec.aggregation, - aggregation_interval, align_to=self.align_ticks_to, include_previous=True) + aggregation_interval, align_to=self.align_ticks_to, + converter=None if spec.scale_factor == 1.0 else lambda x: x*spec.scale_factor, + include_previous=True) self.connectors.append(connector) self.row_specs.append({'id': id(connector), 'is_aggregated': is_aggregated, 'color': spec.color if spec.color is not None else self.COLORS[i % len(self.COLORS)], - 'label': spec.label}) + 'label': spec.label, + 'unit_symbol': spec.unit_symbol}) async def render(self) -> str: return await jinja_env.get_template('log/chart.htm').render_async(