Skip to content

Commit d1e96bf

Browse files
authored
Reuse the same bitmap in sparkline
1 parent 8fda975 commit d1e96bf

File tree

1 file changed

+29
-17
lines changed

1 file changed

+29
-17
lines changed

adafruit_display_shapes/sparkline.py

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ def values(self) -> List[float]:
100100
return self._buffer[start:] + self._buffer[:end]
101101

102102

103-
class Sparkline(displayio.Group):
103+
class Sparkline(displayio.TileGrid):
104104
# pylint: disable=too-many-arguments
105105
"""A sparkline graph.
106106
@@ -120,6 +120,8 @@ class Sparkline(displayio.Group):
120120
will scroll to the left.
121121
"""
122122

123+
_LINE_COLOR = 1
124+
123125
def __init__(
124126
self,
125127
width: int,
@@ -132,11 +134,7 @@ def __init__(
132134
y: int = 0,
133135
color: int = 0xFFFFFF, # line color, default is WHITE
134136
) -> None:
135-
136137
# define class instance variables
137-
self.width = width # in pixels
138-
self.height = height # in pixels
139-
self.color = color #
140138
self._max_items = max_items # maximum number of items in the list
141139
self._buffer = _CyclicBuffer(self._max_items)
142140
self.dyn_xpitch = dyn_xpitch
@@ -151,15 +149,17 @@ def __init__(
151149
# y_top: The actual minimum value of the vertical scale, will be
152150
# updated if autorange
153151
self._points = [] # _points: all points of sparkline
152+
colors = 2
153+
self._palette = displayio.Palette(colors + 1)
154+
self._palette.make_transparent(0)
155+
self._palette[self._LINE_COLOR] = color
156+
self._bitmap = displayio.Bitmap(width, height, colors + 1)
154157

155-
super().__init__(x=x, y=y) # self is a group of single Polygon
156-
# (TODO: it has one element, maybe group is no longer needed?)
158+
super().__init__(self._bitmap, pixel_shader=self._palette, x=x, y=y)
157159

158160
def clear_values(self) -> None:
159161
"""Clears _buffer and removes all lines in the group"""
160-
161-
for _ in range(len(self)): # remove all items from the current group
162-
self.pop()
162+
self._bitmap.fill(0)
163163
self._buffer.clear()
164164

165165
def add_value(self, value: float, update: bool = True) -> None:
@@ -222,20 +222,18 @@ def _add_point(
222222
x: int,
223223
value: float,
224224
) -> None:
225-
226225
# Guard for y_top and y_bottom being the same
227226
if self.y_top == self.y_bottom:
228227
y = int(0.5 * self.height)
229228
else:
230-
y = int(self.height * (self.y_top - value) / (self.y_top - self.y_bottom))
229+
y = int(
230+
(self.height - 1) * (self.y_top - value) / (self.y_top - self.y_bottom)
231+
)
231232
self._points.append((x, y))
232233

233234
def _draw(self) -> None:
234-
while len(self):
235-
self.pop()
236-
self.append(
237-
Polygon(self._points, outline=self.color, close=False)
238-
) # plot the polyline
235+
self._bitmap.fill(0)
236+
Polygon.draw(self._bitmap, self._points, self._LINE_COLOR, close=False)
239237

240238
# pylint: disable= too-many-branches, too-many-nested-blocks, too-many-locals, too-many-statements
241239

@@ -308,3 +306,17 @@ def values(self) -> List[float]:
308306
"""Returns the values displayed on the sparkline."""
309307

310308
return self._buffer.values()
309+
310+
@property
311+
def width(self) -> int:
312+
"""
313+
:return: the width of the graph in pixels
314+
"""
315+
return self._bitmap.width
316+
317+
@property
318+
def height(self) -> int:
319+
"""
320+
:return: the height of the graph in pixels
321+
"""
322+
return self._bitmap.height

0 commit comments

Comments
 (0)