Skip to content

Commit b4f9f4d

Browse files
authored
Merge pull request #56 from bablokb/sparkline_fix
Sparkline fix
2 parents 780fe1b + 37b57f2 commit b4f9f4d

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

adafruit_display_shapes/sparkline.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,6 @@ def __init__(
9797
self.y_top = y_max
9898
# y_top: The actual minimum value of the vertical scale, will be
9999
# updated if autorange
100-
self._x = x
101-
self._y = y
102100
self._redraw = True # _redraw: redraw primitives
103101
self._last = [] # _last: last point of sparkline
104102

@@ -127,7 +125,12 @@ def add_value(self, value: float, update: bool = True) -> None:
127125
if (
128126
len(self._spark_list) >= self._max_items
129127
): # if list is full, remove the first item
130-
self._spark_list.pop(0)
128+
first = self._spark_list.pop(0)
129+
# check if boundaries have to be updated
130+
if self.y_min is None and first == self.y_bottom:
131+
self.y_bottom = min(self._spark_list)
132+
if self.y_max is None and first == self.y_top:
133+
self.y_top = max(self._spark_list)
131134
self._redraw = True
132135
self._spark_list.append(value)
133136

@@ -140,10 +143,6 @@ def add_value(self, value: float, update: bool = True) -> None:
140143
self._redraw = self._redraw or value > self.y_top
141144
self.y_top = value if not self.y_top else max(value, self.y_top)
142145

143-
# Guard for y_top and y_bottom being the same
144-
if self.y_top == self.y_bottom:
145-
self.y_bottom *= 0.99
146-
147146
if update:
148147
self.update()
149148

@@ -177,10 +176,15 @@ def _plotline(
177176
value: float,
178177
) -> None:
179178

180-
y_2 = int(self.height * (self.y_top - value) / (self.y_top - self.y_bottom))
181-
y_1 = int(
182-
self.height * (self.y_top - last_value) / (self.y_top - self.y_bottom)
183-
)
179+
# Guard for y_top and y_bottom being the same
180+
if self.y_top == self.y_bottom:
181+
y_2 = int(0.5 * self.height)
182+
y_1 = int(0.5 * self.height)
183+
else:
184+
y_2 = int(self.height * (self.y_top - value) / (self.y_top - self.y_bottom))
185+
y_1 = int(
186+
self.height * (self.y_top - last_value) / (self.y_top - self.y_bottom)
187+
)
184188
self.append(Line(x_1, y_1, x_2, y_2, self.color)) # plot the line
185189
self._last = [x_2, value]
186190

0 commit comments

Comments
 (0)