39
39
40
40
41
41
class Sparkline (displayio .Group ):
42
- # pylint: disable=invalid-name, too-many-arguments
42
+ # pylint: disable=too-many-arguments
43
43
""" A sparkline graph.
44
44
45
45
: param width: Width of the sparkline graph in pixels
46
46
: param height: Height of the sparkline graph in pixels
47
47
: param max_items: Maximum number of values housed in the sparkline
48
- : param yMin : Lower range for the y-axis. Set to None for autorange.
49
- : param yMax : Upper range for the y-axis. Set to None for autorange.
48
+ : param y_min : Lower range for the y-axis. Set to None for autorange.
49
+ : param y_max : Upper range for the y-axis. Set to None for autorange.
50
50
: param x: X-position on the screen, in pixels
51
51
: param y: Y-position on the screen, in pixels
52
52
: param color: Line color, the default value is 0xFFFFFF (WHITE)
@@ -57,8 +57,8 @@ def __init__(
57
57
width ,
58
58
height ,
59
59
max_items ,
60
- yMin = None , # None = autoscaling
61
- yMax = None , # None = autoscaling
60
+ y_min = None , # None = autoscaling
61
+ y_max = None , # None = autoscaling
62
62
x = 0 ,
63
63
y = 0 ,
64
64
color = 0xFFFFFF , # line color, default is WHITE
@@ -70,13 +70,13 @@ def __init__(
70
70
self .color = color #
71
71
self ._max_items = max_items # maximum number of items in the list
72
72
self ._spark_list = [] # list containing the values
73
- self .yMin = yMin # minimum of y-axis (None: autoscale)
74
- self .yMax = yMax # maximum of y-axis (None: autoscale)
75
- self .yBottom = yMin
76
- # yBottom : The actual minimum value of the vertical scale, will be
73
+ self .y_min = y_min # minimum of y-axis (None: autoscale)
74
+ self .y_max = y_max # maximum of y-axis (None: autoscale)
75
+ self .y_bottom = y_min
76
+ # y_bottom : The actual minimum value of the vertical scale, will be
77
77
# updated if autorange
78
- self .yTop = yMax
79
- # yTop : The actual minimum value of the vertical scale, will be
78
+ self .y_top = y_max
79
+ # y_top : The actual minimum value of the vertical scale, will be
80
80
# updated if autorange
81
81
self ._x = x
82
82
self ._y = y
@@ -101,23 +101,23 @@ def add_value(self, value):
101
101
# pylint: disable=no-else-return
102
102
@staticmethod
103
103
def _xintercept (
104
- x1 , y1 , x2 , y2 , horizontalY
104
+ x1 , y1 , x2 , y2 , horizontal_y
105
105
): # finds intercept of the line and a horizontal line at horizontalY
106
106
slope = (y2 - y1 ) / (x2 - x1 )
107
107
b = y1 - slope * x1
108
108
109
- if slope == 0 and y1 != horizontalY : # does not intercept horizontalY
109
+ if slope == 0 and y1 != horizontal_y : # does not intercept horizontalY
110
110
return None
111
111
else :
112
112
xint = (
113
- horizontalY - b
113
+ horizontal_y - b
114
114
) / slope # calculate the x-intercept at position y=horizontalY
115
115
return int (xint )
116
116
117
- def _plotLine (self , x1 , last_value , x2 , value , yBottom , yTop ):
117
+ def _plotLine (self , x1 , last_value , x2 , value , y_bottom , y_top ):
118
118
119
- y2 = int (self .height * (yTop - value ) / (yTop - yBottom ))
120
- y1 = int (self .height * (yTop - last_value ) / (yTop - yBottom ))
119
+ y2 = int (self .height * (y_top - value ) / (y_top - y_bottom ))
120
+ y1 = int (self .height * (y_top - last_value ) / (y_top - y_bottom ))
121
121
self .append (Line (x1 , y1 , x2 , y2 , self .color )) # plot the line
122
122
123
123
# pylint: disable=invalid-name, too-many-branches, too-many-nested-blocks
@@ -128,15 +128,15 @@ def update(self):
128
128
"""
129
129
130
130
# get the y range
131
- if self .yMin is None :
132
- self .yBottom = min (self ._spark_list )
131
+ if self .y_min is None :
132
+ self .y_bottom = min (self ._spark_list )
133
133
else :
134
- self .yBottom = self .yMin
134
+ self .y_bottom = self .y_min
135
135
136
- if self .yMax is None :
137
- self .yTop = max (self ._spark_list )
136
+ if self .y_max is None :
137
+ self .y_top = max (self ._spark_list )
138
138
else :
139
- self .yTop = self .yMax
139
+ self .y_top = self .y_max
140
140
141
141
if len (self ._spark_list ) > 2 :
142
142
xpitch = self .width / (
@@ -155,29 +155,29 @@ def update(self):
155
155
156
156
# print("x1: {}, x2: {}".format(x1,x2))
157
157
158
- if (self .yBottom <= last_value <= self .yTop ) and (
159
- self .yBottom <= value <= self .yTop
158
+ if (self .y_bottom <= last_value <= self .y_top ) and (
159
+ self .y_bottom <= value <= self .y_top
160
160
): # both points are in range, plot the line
161
161
self ._plotLine (
162
- x1 , last_value , x2 , value , self .yBottom , self .yTop
162
+ x1 , last_value , x2 , value , self .y_bottom , self .y_top
163
163
)
164
164
165
165
else : # at least one point is out of range, clip one or both ends the line
166
- if ((last_value > self .yTop ) and (value > self .yTop )) or (
167
- (last_value < self .yBottom ) and (value < self .yBottom )
166
+ if ((last_value > self .y_top ) and (value > self .y_top )) or (
167
+ (last_value < self .y_bottom ) and (value < self .y_bottom )
168
168
):
169
169
# both points are on the same side out of range: don't draw anything
170
170
pass
171
171
else :
172
- xintBottom = self ._xintercept (
173
- x1 , last_value , x2 , value , self .yBottom
172
+ xint_bottom = self ._xintercept (
173
+ x1 , last_value , x2 , value , self .y_bottom
174
174
) # get possible new x intercept points
175
- xintTop = self ._xintercept (
176
- x1 , last_value , x2 , value , self .yTop
175
+ xint_top = self ._xintercept (
176
+ x1 , last_value , x2 , value , self .y_top
177
177
) # on the top and bottom of range
178
178
179
- if (xintBottom is None ) or (
180
- xintTop is None
179
+ if (xint_bottom is None ) or (
180
+ xint_top is None
181
181
): # out of range doublecheck
182
182
pass
183
183
else :
@@ -188,27 +188,27 @@ def update(self):
188
188
adj_value = value
189
189
190
190
if value > last_value : # slope is positive
191
- if xintBottom >= x1 : # bottom is clipped
192
- adj_x1 = xintBottom
193
- adj_last_value = self .yBottom # y1
194
- if xintTop <= x2 : # top is clipped
195
- adj_x2 = xintTop
196
- adj_value = self .yTop # y2
191
+ if xint_bottom >= x1 : # bottom is clipped
192
+ adj_x1 = xint_bottom
193
+ adj_last_value = self .y_bottom # y1
194
+ if xint_top <= x2 : # top is clipped
195
+ adj_x2 = xint_top
196
+ adj_value = self .y_top # y2
197
197
else : # slope is negative
198
- if xintTop >= x1 : # top is clipped
199
- adj_x1 = xintTop
200
- adj_last_value = self .yTop # y1
201
- if xintBottom <= x2 : # bottom is clipped
202
- adj_x2 = xintBottom
203
- adj_value = self .yBottom # y2
198
+ if xint_top >= x1 : # top is clipped
199
+ adj_x1 = xint_top
200
+ adj_last_value = self .y_top # y1
201
+ if xint_bottom <= x2 : # bottom is clipped
202
+ adj_x2 = xint_bottom
203
+ adj_value = self .y_bottom # y2
204
204
205
205
self ._plotLine (
206
206
adj_x1 ,
207
207
adj_last_value ,
208
208
adj_x2 ,
209
209
adj_value ,
210
- self .yBottom ,
211
- self .yTop ,
210
+ self .y_bottom ,
211
+ self .y_top ,
212
212
)
213
213
214
214
last_value = value # store value for the next iteration
0 commit comments