From 1857b97a9475a8837fffbe6be34ccaee6d71fa3a Mon Sep 17 00:00:00 2001 From: Bernhard Bablok Date: Fri, 19 Aug 2022 19:37:33 +0200 Subject: [PATCH 1/3] recalculate min/max after pop --- adafruit_display_shapes/sparkline.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/adafruit_display_shapes/sparkline.py b/adafruit_display_shapes/sparkline.py index 867ee0a..2ffe929 100644 --- a/adafruit_display_shapes/sparkline.py +++ b/adafruit_display_shapes/sparkline.py @@ -127,7 +127,12 @@ def add_value(self, value: float, update: bool = True) -> None: if ( len(self._spark_list) >= self._max_items ): # if list is full, remove the first item - self._spark_list.pop(0) + first = self._spark_list.pop(0) + # check if boundaries have to be updated + if self.y_min is None and first == self.y_bottom: + self.y_bottom = min(self._spark_list) + if self.y_max is None and first == self.y_top: + self.y_top = max(self._spark_list) self._redraw = True self._spark_list.append(value) From fff1de9220005c205371d002ff6943c46a0e3711 Mon Sep 17 00:00:00 2001 From: Bernhard Bablok Date: Fri, 19 Aug 2022 19:49:23 +0200 Subject: [PATCH 2/3] don't set y_top/y_bottom to arbitrary values --- adafruit_display_shapes/sparkline.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/adafruit_display_shapes/sparkline.py b/adafruit_display_shapes/sparkline.py index 2ffe929..818b651 100644 --- a/adafruit_display_shapes/sparkline.py +++ b/adafruit_display_shapes/sparkline.py @@ -145,10 +145,6 @@ def add_value(self, value: float, update: bool = True) -> None: self._redraw = self._redraw or value > self.y_top self.y_top = value if not self.y_top else max(value, self.y_top) - # Guard for y_top and y_bottom being the same - if self.y_top == self.y_bottom: - self.y_bottom *= 0.99 - if update: self.update() @@ -182,10 +178,15 @@ def _plotline( value: float, ) -> None: - y_2 = int(self.height * (self.y_top - value) / (self.y_top - self.y_bottom)) - y_1 = int( - self.height * (self.y_top - last_value) / (self.y_top - self.y_bottom) - ) + # Guard for y_top and y_bottom being the same + if self.y_top == self.y_bottom: + y_2 = int(0.5 * self.height) + y_1 = int(0.5 * self.height) + else: + y_2 = int(self.height * (self.y_top - value) / (self.y_top - self.y_bottom)) + y_1 = int( + self.height * (self.y_top - last_value) / (self.y_top - self.y_bottom) + ) self.append(Line(x_1, y_1, x_2, y_2, self.color)) # plot the line self._last = [x_2, value] From 37b57f2b8b85e6900321aaa31a5b2b0428c911fc Mon Sep 17 00:00:00 2001 From: Bernhard Bablok Date: Sat, 20 Aug 2022 12:59:28 +0200 Subject: [PATCH 3/3] removed unused self._x and self._y --- adafruit_display_shapes/sparkline.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/adafruit_display_shapes/sparkline.py b/adafruit_display_shapes/sparkline.py index 818b651..ccb9241 100644 --- a/adafruit_display_shapes/sparkline.py +++ b/adafruit_display_shapes/sparkline.py @@ -97,8 +97,6 @@ def __init__( self.y_top = y_max # y_top: The actual minimum value of the vertical scale, will be # updated if autorange - self._x = x - self._y = y self._redraw = True # _redraw: redraw primitives self._last = [] # _last: last point of sparkline