1
- # SPDX-FileCopyrightText: 2021 Jose David Montoya
1
+ # SPDX-FileCopyrightText: 2021 Jose David M.
2
2
#
3
3
# SPDX-License-Identifier: MIT
4
4
"""
5
5
6
6
`cartesian`
7
7
================================================================================
8
- A cartasian plane widget for displaying graphical information.
8
+ A cartesian plane widget for displaying graphical information.
9
9
10
10
* Author(s): Jose David Montoya
11
11
28
28
import terminalio
29
29
import vectorio
30
30
from adafruit_displayio_layout .widgets .widget import Widget
31
+ from adafruit_displayio_layout .widgets import rectangle_helper
32
+ from adafruit_display_text import bitmap_label
31
33
32
34
try :
33
35
import bitmaptools
@@ -56,7 +58,8 @@ class Cartesian(Widget):
56
58
:param int axes_stroke: axes lines thickness in pixels defaults to 2
57
59
58
60
:param int major_tick_stroke: tick lines thickness in pixels dafaults to 1
59
- :param int major_tick_lenght: tick lines lenght in pixels defaults to 5
61
+ :param int major_tick_length: tick lines length in pixels defaults to 5
62
+ :param List[str] tick_labels: a list of strings for the tick text labels
60
63
61
64
:param int tick_label_font: tick label text font
62
65
:param int tick_label_color: tick label text color
@@ -77,7 +80,7 @@ class Cartesian(Widget):
77
80
78
81
.. code-block:: python
79
82
80
- my_plane=Plane(x=20, y=30) # instance the switch at x=20, y=30
83
+ my_plane=Plane(x=20, y=30) # instance the plane at x=20, y=30
81
84
82
85
Once you setup your display, you can now add ``my_plane`` to your display using:
83
86
@@ -86,7 +89,7 @@ class Cartesian(Widget):
86
89
display.show(my_plane) # add the group to the display
87
90
88
91
If you want to have multiple display elements, you can create a group and then
89
- append the switch and the other elements to the group. Then, you can add the full
92
+ append the plane and the other elements to the group. Then, you can add the full
90
93
group to the display as in this example:
91
94
92
95
.. code-block:: python
@@ -113,7 +116,7 @@ def __init__(
113
116
xrange : Tuple [int , int ] = (0 , 100 ),
114
117
yrange : Tuple [int , int ] = (0 , 100 ),
115
118
axes_color : int = 0xFFFFFF ,
116
- axes_stroke : int = 2 ,
119
+ axes_stroke : int = 1 ,
117
120
tick_color : int = 0xFFFFFF ,
118
121
major_tick_stroke : int = 1 ,
119
122
major_tick_length : int = 5 ,
@@ -127,11 +130,13 @@ def __init__(
127
130
# TODO Replace with drawline/vectorio [√]
128
131
# TODO Make a rectangle function [√]
129
132
# TODO Include functions to equal space ticks [√]
130
- # TODO Make labels and text [ ]
133
+ # TODO Make labels and text [√ ]
131
134
# TODO Make Styles applicable [ ]
132
135
# TODO Animate when overflow [ ]
133
136
# TODO Add Subticks functionality [ ]
134
137
# TODO ticks evenly distributed [√]
138
+ # TODO Make Ticker lines [√]
139
+ # TODO Updater to use local coordinates [ ]
135
140
136
141
super ().__init__ (** kwargs , max_size = 3 )
137
142
self ._origin_x = x
@@ -162,8 +167,8 @@ def __init__(
162
167
self ._usable_width = self ._widget_width
163
168
self ._usable_height = self ._widget_height
164
169
165
- self ._tickx_separation = int (xrange [1 ] / self ._usable_width * 10 )
166
- self ._ticky_separation = int (yrange [1 ] / self ._usable_height * 10 )
170
+ self ._tickx_separation = int (xrange [1 ] / self ._usable_width * 10 ) + 3
171
+ self ._ticky_separation = int (yrange [1 ] / self ._usable_height * 10 ) + 3
167
172
168
173
self ._tick_bitmap = displayio .Bitmap (
169
174
self ._tick_line_thickness , self ._tick_line_height , 3
@@ -237,34 +242,100 @@ def _get_font_height(font, scale):
237
242
elif hasattr (font , "ascent" ):
238
243
font_height = int (scale * font .ascent + font .ascent )
239
244
font_width = 12
245
+ else :
246
+ font_height = 12
247
+ font_width = 12
240
248
return font_width , font_height
241
249
242
250
def _draw_axes (self ):
243
251
# Draw x axes line
244
- bitmaptools .draw_line (self ._axesx_bitmap , 0 , 0 , self ._usable_width - 1 , 0 , 2 )
245
- # Draw y axes line
246
- bitmaptools .draw_line (
247
- self ._axesy_bitmap ,
248
- self ._axesy_width - 1 ,
249
- 0 ,
250
- self ._axesy_width - 1 ,
251
- self ._usable_height - 1 ,
252
- 2 ,
253
- )
252
+ if self ._axes_line_thickness == 1 :
253
+ bitmaptools .draw_line (
254
+ self ._axesx_bitmap , 0 , 0 , self ._usable_width - 1 , 0 , 2
255
+ )
256
+ # Draw y axes line
257
+ bitmaptools .draw_line (
258
+ self ._axesy_bitmap ,
259
+ self ._axesy_width - 1 ,
260
+ 0 ,
261
+ self ._axesy_width - 1 ,
262
+ self ._usable_height - 1 ,
263
+ 2 ,
264
+ )
265
+ else :
266
+ rectangle_helper (
267
+ 0 ,
268
+ 0 ,
269
+ self ._axes_line_thickness ,
270
+ self ._axesx_bitmap .width - 1 ,
271
+ self ._axesx_bitmap ,
272
+ 2 ,
273
+ self ._screen_palette ,
274
+ True ,
275
+ )
276
+ rectangle_helper (
277
+ self ._axesy_width - self ._axes_line_thickness - 1 ,
278
+ 0 ,
279
+ self ._axesy_bitmap .height ,
280
+ self ._axes_line_thickness ,
281
+ self ._axesy_bitmap ,
282
+ 2 ,
283
+ self ._screen_palette ,
284
+ True ,
285
+ )
254
286
255
287
def _draw_ticks (self ):
256
288
# X axes ticks
257
-
289
+ tickcounter = 1
258
290
for i in range (
259
291
self ._tickx_separation , self ._usable_width , self ._tickx_separation
260
292
):
293
+ if tickcounter == 3 :
294
+ tickcounter = 0
295
+ shift_label_x = len (str (i )) * self ._font_width
296
+ tick_text = bitmap_label .Label (
297
+ self ._font ,
298
+ text = str (i ),
299
+ x = self ._origin_x + (i - shift_label_x // 2 ),
300
+ y = self ._origin_y
301
+ + self ._usable_height
302
+ + self ._axes_line_thickness
303
+ + self ._tick_line_height
304
+ + self ._font_height // 2
305
+ + 1 ,
306
+ )
307
+ self .append (tick_text )
308
+
261
309
bitmaptools .draw_line (
262
- self ._axesx_bitmap , i , self ._tick_line_height , i , 0 , 2
310
+ self ._axesx_bitmap ,
311
+ i ,
312
+ self ._tick_line_height + self ._axes_line_thickness ,
313
+ i ,
314
+ 0 ,
315
+ 2 ,
263
316
)
317
+ tickcounter = tickcounter + 1
318
+
264
319
# Y axes ticks
320
+ tickcounter = 0
265
321
for i in range (
266
322
self ._usable_height - 1 - self ._ticky_separation , 0 , - self ._ticky_separation
267
323
):
324
+ if tickcounter == 2 :
325
+ tickcounter = 0
326
+ shift_label_x = len (str (self ._usable_height - i )) * self ._font_width
327
+ tick_text = bitmap_label .Label (
328
+ self ._font ,
329
+ text = str (self ._usable_height - i ),
330
+ x = self ._origin_x
331
+ - shift_label_x
332
+ - self ._axes_line_thickness
333
+ - self ._tick_line_height
334
+ - 2 ,
335
+ y = self ._origin_y + i + self ._font_height ,
336
+ )
337
+ self .append (tick_text )
338
+
268
339
bitmaptools .draw_line (
269
340
self ._axesy_bitmap ,
270
341
(self ._axesy_width - self ._tick_line_height ) - 1 ,
@@ -273,6 +344,7 @@ def _draw_ticks(self):
273
344
i ,
274
345
2 ,
275
346
)
347
+ tickcounter = tickcounter + 1
276
348
277
349
def _draw_pointers (self ):
278
350
self ._pointer = vectorio .Circle (3 )
0 commit comments