@@ -100,7 +100,7 @@ def values(self) -> List[float]:
100
100
return self ._buffer [start :] + self ._buffer [:end ]
101
101
102
102
103
- class Sparkline (displayio .Group ):
103
+ class Sparkline (displayio .TileGrid ):
104
104
# pylint: disable=too-many-arguments
105
105
"""A sparkline graph.
106
106
@@ -120,6 +120,8 @@ class Sparkline(displayio.Group):
120
120
will scroll to the left.
121
121
"""
122
122
123
+ _LINE_COLOR = 1
124
+
123
125
def __init__ (
124
126
self ,
125
127
width : int ,
@@ -132,11 +134,7 @@ def __init__(
132
134
y : int = 0 ,
133
135
color : int = 0xFFFFFF , # line color, default is WHITE
134
136
) -> None :
135
-
136
137
# define class instance variables
137
- self .width = width # in pixels
138
- self .height = height # in pixels
139
- self .color = color #
140
138
self ._max_items = max_items # maximum number of items in the list
141
139
self ._buffer = _CyclicBuffer (self ._max_items )
142
140
self .dyn_xpitch = dyn_xpitch
@@ -151,15 +149,17 @@ def __init__(
151
149
# y_top: The actual minimum value of the vertical scale, will be
152
150
# updated if autorange
153
151
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 )
154
157
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 )
157
159
158
160
def clear_values (self ) -> None :
159
161
"""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 )
163
163
self ._buffer .clear ()
164
164
165
165
def add_value (self , value : float , update : bool = True ) -> None :
@@ -222,20 +222,18 @@ def _add_point(
222
222
x : int ,
223
223
value : float ,
224
224
) -> None :
225
-
226
225
# Guard for y_top and y_bottom being the same
227
226
if self .y_top == self .y_bottom :
228
227
y = int (0.5 * self .height )
229
228
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
+ )
231
232
self ._points .append ((x , y ))
232
233
233
234
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 )
239
237
240
238
# pylint: disable= too-many-branches, too-many-nested-blocks, too-many-locals, too-many-statements
241
239
@@ -308,3 +306,17 @@ def values(self) -> List[float]:
308
306
"""Returns the values displayed on the sparkline."""
309
307
310
308
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