Skip to content

Commit

Permalink
data log widgets: Add scale_factor and unit symbol for ChartWidget
Browse files Browse the repository at this point in the history
  • Loading branch information
mhthies committed Dec 25, 2023
1 parent 54175a7 commit b5d59fd
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
8 changes: 4 additions & 4 deletions example/ui_logging_showcase.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
]))


Expand Down
11 changes: 9 additions & 2 deletions shc/web/log_widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit b5d59fd

Please sign in to comment.