Skip to content

Sparkline fix #56

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 22, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 15 additions & 11 deletions adafruit_display_shapes/sparkline.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -127,7 +125,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)

Expand All @@ -140,10 +143,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()

Expand Down Expand Up @@ -177,10 +176,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]

Expand Down