21
21
22
22
"""
23
23
24
+ try :
25
+ from typing import Union
26
+ from typing_extensions import Literal
27
+ except ImportError :
28
+ pass
29
+
24
30
import displayio
25
31
from bitmaptools import draw_line
26
32
from vectorio import Circle
36
42
37
43
class Uplot (displayio .Group ):
38
44
"""
39
- Main class to display different graphics
45
+ Canvas Class to add different elements to the screen.
46
+ The origin point set by ``x`` and ``y`` properties
47
+
48
+ :param int x: origin x coordinate
49
+ :param int y: origin y coordinate
50
+ :param int width: plot box width in pixels
51
+ :param int height: plot box height in pixels
52
+ :param int padding: padding for the plot box in all directions
53
+
40
54
"""
41
55
42
- def __init__ (self , x = 0 , y = 0 , width = None , height = None , padding = 15 ):
56
+ def __init__ (
57
+ self ,
58
+ x : int = 0 ,
59
+ y : int = 0 ,
60
+ width : int = 100 ,
61
+ height : int = 100 ,
62
+ padding : int = 15 ,
63
+ ) -> None :
43
64
super ().__init__ (x = x , y = y , scale = 1 )
44
65
np .set_printoptions (threshold = 200 )
45
66
@@ -73,17 +94,24 @@ def __init__(self, x=0, y=0, width=None, height=None, padding=15):
73
94
)
74
95
)
75
96
76
- def axs_params (self , axstype = "box" ):
97
+ def axs_params (self , axstype : Literal [ "box" , "cartesian" , "line" ] = "box" ) -> None :
77
98
"""
78
99
Setting up axs visibility
79
100
80
- :param axs : argument with the kind of axs you selected
81
- :return: none
101
+ :param axstype : argument with the kind of axs you selected
102
+ :return: None
82
103
83
104
"""
84
105
self ._axesparams = axstype
85
106
86
107
def _drawbox (self ):
108
+ """
109
+ Draw the plot box
110
+
111
+ :return: None
112
+
113
+ """
114
+
87
115
if self ._axesparams == "cartesian" :
88
116
draw_box = [True , True , False , False ]
89
117
elif self ._axesparams == "line" :
@@ -129,40 +157,52 @@ def _drawbox(self):
129
157
1 ,
130
158
)
131
159
132
- def draw_circle (self , radius = 5 , x = 100 , y = 100 ):
160
+ def draw_circle (self , radius : int = 5 , x : int = 100 , y : int = 100 ) -> None :
133
161
"""
134
162
Draw a circle in the plot area
135
- :param radius: circle radius
136
- :param x: circles center x coordinate position in pixels, Defaults to 100.
137
- :param y: circles center y coordinate position in pixels. Defaults to 100.
163
+
164
+ :param int radius: circle radius
165
+ :param int x: circles center x coordinate position in pixels, Defaults to 100.
166
+ :param int y: circles center y coordinate position in pixels. Defaults to 100.
167
+
138
168
:return: None
169
+
139
170
"""
140
171
palette = displayio .Palette (1 )
141
172
palette [0 ] = 0xFFFFFF
142
173
self .append (Circle (pixel_shader = palette , radius = radius , x = x , y = y ))
143
174
144
175
@staticmethod
145
- def normalize (oldrangemin , oldrangemax , newrangemin , newrangemax , value ):
176
+ def normalize (
177
+ oldrangemin : Union [float , int ],
178
+ oldrangemax : Union [float , int ],
179
+ newrangemin : Union [float , int ],
180
+ newrangemax : Union [float , int ],
181
+ value : Union [float , int ],
182
+ ) -> Union [float , int ]:
146
183
"""
147
184
This function converts the original value into a new defined value in the new range
148
- :param oldrangemin: minimum of the original range
149
- :param oldrangemax: maximum of the original range
150
- :param newrangemin: minimum of the new range
151
- :param newrangemax: maximum of the new range
152
- :param value: value to be converted
153
- :return: converted value
185
+
186
+ :param int|float oldrangemin: minimum of the original range
187
+ :param int|float oldrangemax: maximum of the original range
188
+ :param int|float newrangemin: minimum of the new range
189
+ :param int|float newrangemax: maximum of the new range
190
+ :param int|float value: value to be converted
191
+ :return int|float: converted value
192
+
154
193
"""
194
+
155
195
return (
156
196
((value - oldrangemin ) * (newrangemax - newrangemin ))
157
197
/ (oldrangemax - oldrangemin )
158
198
) + newrangemin
159
199
160
- def draw_plot (self , x , y ) :
200
+ def draw_plot (self , x : int , y : int ) -> None :
161
201
"""
162
202
Function to draw the plot
163
203
164
- :param x: data for x points
165
- :param y: data for y points
204
+ :param int x: data for x points
205
+ :param int y: data for y points
166
206
:return: None
167
207
168
208
"""
@@ -195,13 +235,16 @@ def draw_plot(self, x, y):
195
235
if self ._showticks :
196
236
self ._draw_ticks (x , y )
197
237
198
- def _draw_ticks (self , x , y ) :
238
+ def _draw_ticks (self , x : int , y : int ) -> None :
199
239
"""
200
240
Draw ticks in the plot area
201
241
202
- :return:
242
+ :param int x: x coord
243
+ :param int y: y coord
244
+ :return:None
203
245
204
246
"""
247
+
205
248
ticks = np .array ([10 , 30 , 50 , 70 , 90 ])
206
249
subticks = np .array ([20 , 40 , 60 , 80 ])
207
250
ticksxnorm = np .array (self .normalize (0 , 100 , np .min (x ), np .max (x ), ticks ))
@@ -276,13 +319,15 @@ def _draw_ticks(self, x, y):
276
319
self ._draw_gridx (ticksxrenorm )
277
320
self ._draw_gridy (ticksyrenorm )
278
321
279
- def tick_params (self , tickheight = 8 , tickcolor = 0xFFFFFF , tickgrid = False ):
322
+ def tick_params (
323
+ self , tickheight : int = 8 , tickcolor : int = 0xFFFFFF , tickgrid : bool = False
324
+ ) -> None :
280
325
"""
281
326
Function to set ticks parameters
282
327
283
- :param tickheight:
284
- :param tickcolor:
285
-
328
+ :param int tickheight: tick height in pixels
329
+ :param int tickcolor: tick color in hex
330
+ :param bool tickgrid: defines if the grid is to be shown
286
331
:return: None
287
332
288
333
"""
@@ -292,10 +337,10 @@ def tick_params(self, tickheight=8, tickcolor=0xFFFFFF, tickgrid=False):
292
337
self ._plot_palette [2 ] = tickcolor
293
338
self ._tickgrid = tickgrid
294
339
295
- def _draw_gridx (self , ticks_data ) :
340
+ def _draw_gridx (self , ticks_data : list [ int ]) -> None :
296
341
"""
297
- draw plot grid
298
-
342
+ Draws the plot grid
343
+ :param list[int] ticks_data: ticks data information
299
344
:return: None
300
345
301
346
"""
@@ -314,10 +359,10 @@ def _draw_gridx(self, ticks_data):
314
359
)
315
360
start = start - grid_espace - line_lenght
316
361
317
- def _draw_gridy (self , ticks_data ) :
362
+ def _draw_gridy (self , ticks_data : list [ int ]) -> None :
318
363
"""
319
- draw plot grid
320
-
364
+ Draws plot grid in the y axs
365
+ :param list[int] ticks_data: ticks data information
321
366
:return: None
322
367
323
368
"""
@@ -335,3 +380,13 @@ def _draw_gridy(self, ticks_data):
335
380
2 ,
336
381
)
337
382
start = start + grid_espace + line_lenght
383
+
384
+ def update_plot (self ) -> None :
385
+ """
386
+ Function to update graph
387
+
388
+ :return: None
389
+
390
+ """
391
+
392
+ self ._drawbox ()
0 commit comments