3939
4040# pylint: disable=too-many-instance-attributes
4141
42+ try :
43+ from typing import Optional , List
44+ except ImportError :
45+ pass
4246import displayio
4347from adafruit_display_shapes .line import Line
4448
@@ -59,15 +63,15 @@ class Sparkline(displayio.Group):
5963
6064 def __init__ (
6165 self ,
62- width ,
63- height ,
64- max_items ,
65- y_min = None , # None = autoscaling
66- y_max = None , # None = autoscaling
67- x = 0 ,
68- y = 0 ,
69- color = 0xFFFFFF , # line color, default is WHITE
70- ):
66+ width : int ,
67+ height : int ,
68+ max_items : int ,
69+ y_min : Optional [ int ] = None , # None = autoscaling
70+ y_max : Optional [ int ] = None , # None = autoscaling
71+ x : int = 0 ,
72+ y : int = 0 ,
73+ color : int = 0xFFFFFF , # line color, default is WHITE
74+ ) -> None :
7175
7276 # define class instance variables
7377 self .width = width # in pixels
@@ -88,14 +92,14 @@ def __init__(
8892
8993 super ().__init__ (x = x , y = y ) # self is a group of lines
9094
91- def clear_values (self ):
95+ def clear_values (self ) -> None :
9296 """Removes all values from the _spark_list list and removes all lines in the group"""
9397
9498 for _ in range (len (self )): # remove all items from the current group
9599 self .pop ()
96100 self ._spark_list = [] # empty the list
97101
98- def add_value (self , value ) :
102+ def add_value (self , value : float ) -> None :
99103 """Add a value to the sparkline.
100104 :param value: The value to be added to the sparkline
101105 """
@@ -111,8 +115,14 @@ def add_value(self, value):
111115 # pylint: disable=no-else-return
112116 @staticmethod
113117 def _xintercept (
114- x_1 , y_1 , x_2 , y_2 , horizontal_y
115- ): # finds intercept of the line and a horizontal line at horizontalY
118+ x_1 : int ,
119+ y_1 : float ,
120+ x_2 : int ,
121+ y_2 : float ,
122+ horizontal_y : int ,
123+ ) -> Optional [
124+ int
125+ ]: # finds intercept of the line and a horizontal line at horizontalY
116126 slope = (y_2 - y_1 ) / (x_2 - x_1 )
117127 b = y_1 - slope * x_1
118128
@@ -124,15 +134,23 @@ def _xintercept(
124134 ) / slope # calculate the x-intercept at position y=horizontalY
125135 return int (xint )
126136
127- def _plotline (self , x_1 , last_value , x_2 , value , y_bottom , y_top ):
137+ def _plotline (
138+ self ,
139+ x_1 : int ,
140+ last_value : float ,
141+ x_2 : int ,
142+ value : float ,
143+ y_bottom : int ,
144+ y_top : int ,
145+ ) -> None :
128146
129147 y_2 = int (self .height * (y_top - value ) / (y_top - y_bottom ))
130148 y_1 = int (self .height * (y_top - last_value ) / (y_top - y_bottom ))
131149 self .append (Line (x_1 , y_1 , x_2 , y_2 , self .color )) # plot the line
132150
133151 # pylint: disable= too-many-branches, too-many-nested-blocks
134152
135- def update (self ):
153+ def update (self ) -> None :
136154 """Update the drawing of the sparkline."""
137155
138156 # get the y range
@@ -224,7 +242,7 @@ def update(self):
224242
225243 last_value = value # store value for the next iteration
226244
227- def values (self ):
245+ def values (self ) -> List [ float ] :
228246 """Returns the values displayed on the sparkline."""
229247
230248 return self ._spark_list
0 commit comments