From 5b7283eb2a75b54a7d69cb04f83c011ea736df42 Mon Sep 17 00:00:00 2001 From: Karel Kroeze Date: Sat, 13 Jan 2024 23:05:57 +0000 Subject: [PATCH] selectively update lines in add_values `add_values` would repeatedly call `update` for each line where a value was added, and `update` calls `_draw` which re-draws _all_ lines each time it is called. By keeping track of lines that need to be updated and only calling `update` once at the end of `add_values`, we avoid $(n-1)^2$ spurious line re-draws. --- adafruit_display_shapes/multisparkline.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/adafruit_display_shapes/multisparkline.py b/adafruit_display_shapes/multisparkline.py index 0d01b8b..acc1bee 100644 --- a/adafruit_display_shapes/multisparkline.py +++ b/adafruit_display_shapes/multisparkline.py @@ -172,8 +172,10 @@ def add_values(self, values: List[float], update: bool = True) -> None: call the update()-method """ + lines_to_update = [] for i, value in enumerate(values): if value is not None: + lines_to_update.append(i) top = self.y_tops[i] bottom = self.y_bottoms[i] if ( @@ -195,8 +197,8 @@ def add_values(self, values: List[float], update: bool = True) -> None: self.y_tops[i] = top self.y_bottoms[i] = bottom - if update: - self.update_line(i) + if update and lines_to_update: + self.update_line(lines_to_update) def _add_point( self,