33
33
import bitmaptools
34
34
except NameError :
35
35
pass # utilize the blit_rotate_scale function defined herein
36
+ try :
37
+ from typing import Tuple
38
+ except ImportError :
39
+ pass
36
40
37
41
38
42
class Cartesian (Widget ):
@@ -45,6 +49,9 @@ class Cartesian(Widget):
45
49
:param int width: requested width, in pixels defaults to 100 pixels
46
50
:param int height: requested height, in pixels defaults to 100 pixels
47
51
52
+ :param (int, int) axesx_range: X axes range
53
+ :param (int, int) axesy_range: Y axes range
54
+
48
55
:param int axes_color: axes lines color defaults to white (0xFFFFFF)
49
56
:param int axes_stroke: axes lines thickness in pixels defaults to 2
50
57
@@ -57,6 +64,43 @@ class Cartesian(Widget):
57
64
:param int pointer_radius: pointer radius in pixels defaults to 1
58
65
:param int pointer_color: pointer color defaults to white (0xFFFFFF)
59
66
67
+ **Quickstart: Importing and using Cartesian**
68
+
69
+ Here is one way of importing the `Cartesian` class so you can use it as
70
+ the name ``Plane``:
71
+
72
+ .. code-block:: python
73
+
74
+ from adafruit_displayio_layout.widgets.cartesian import Cartesian as Plane
75
+
76
+ Now you can create a plane at pixel position x=20, y=30 using:
77
+
78
+ .. code-block:: python
79
+
80
+ my_plane=Plane(x=20, y=30) # instance the switch at x=20, y=30
81
+
82
+ Once you setup your display, you can now add ``my_plane`` to your display using:
83
+
84
+ .. code-block:: python
85
+
86
+ display.show(my_plane) # add the group to the display
87
+
88
+ 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
90
+ group to the display as in this example:
91
+
92
+ .. code-block:: python
93
+
94
+ my_plane= Plane(20, 30) # instance the plane at x=20, y=30
95
+ my_group = displayio.Group(max_size=10) # make a group that can hold 10 items
96
+ my_group.append(my_plane) # Add my_plane to the group
97
+
98
+ #
99
+ # Append other display elements to the group
100
+ #
101
+
102
+ display.show(my_group) # add the group to the display
103
+
60
104
"""
61
105
62
106
def __init__ (
@@ -66,6 +110,8 @@ def __init__(
66
110
display_color = 0x000000 ,
67
111
width : int = 100 ,
68
112
height : int = 100 ,
113
+ xrange : Tuple [int , int ] = (0 , 100 ),
114
+ yrange : Tuple [int , int ] = (0 , 100 ),
69
115
axes_color : int = 0xFFFFFF ,
70
116
axes_stroke : int = 2 ,
71
117
tick_color : int = 0xFFFFFF ,
@@ -80,12 +126,12 @@ def __init__(
80
126
# TODO Make axes, separate from data [√]
81
127
# TODO Replace with drawline/vectorio [√]
82
128
# TODO Make a rectangle function [√]
83
- # TODO Include functions to equal space ticks [ ]
129
+ # TODO Include functions to equal space ticks [√ ]
84
130
# TODO Make labels and text [ ]
85
131
# TODO Make Styles applicable [ ]
86
132
# TODO Animate when overflow [ ]
87
133
# TODO Add Subticks functionality [ ]
88
- # TODO ticks evenly distributed [ ]
134
+ # TODO ticks evenly distributed [√ ]
89
135
90
136
super ().__init__ (** kwargs , max_size = 3 )
91
137
self ._origin_x = x
@@ -113,9 +159,11 @@ def __init__(
113
159
self ._font_width = self ._get_font_height (self ._font , 1 )[0 ]
114
160
self ._font_height = self ._get_font_height (self ._font , 1 )[1 ]
115
161
116
- self ._usable_width = self ._widget_width - 2 * self ._margin
117
- self ._usable_height = self ._widget_height - 2 * self ._margin
118
- self ._tickx_separation = 2 * self ._font_width + 2
162
+ self ._usable_width = self ._widget_width
163
+ self ._usable_height = self ._widget_height
164
+
165
+ self ._tickx_separation = int (xrange [1 ] / self ._usable_width * 10 )
166
+ self ._ticky_separation = int (yrange [1 ] / self ._usable_height * 10 )
119
167
120
168
self ._tick_bitmap = displayio .Bitmap (
121
169
self ._tick_line_thickness , self ._tick_line_height , 3
@@ -134,7 +182,7 @@ def __init__(
134
182
self ._axesy_width = (
135
183
2
136
184
+ self ._axes_line_thickness
137
- + self ._font_height
185
+ + self ._font_width
138
186
+ self ._tick_line_height // 2
139
187
)
140
188
self ._axesy_bitmap = displayio .Bitmap (self ._axesy_width , self ._usable_height , 4 )
@@ -143,7 +191,7 @@ def __init__(
143
191
self ._screen_bitmap = displayio .Bitmap (
144
192
self ._usable_width , self ._usable_height , 3
145
193
)
146
-
194
+ self . _screen_bitmap . fill ( 0 )
147
195
self ._screen_palette = displayio .Palette (6 )
148
196
self ._screen_palette .make_transparent (0 )
149
197
self ._screen_palette [1 ] = self ._tick_color
@@ -192,27 +240,34 @@ def _get_font_height(font, scale):
192
240
return font_width , font_height
193
241
194
242
def _draw_axes (self ):
195
- y = self ._tick_line_height // 2
196
- bitmaptools .draw_line (self ._axesx_bitmap , 0 , y , self ._usable_width - 1 , y , 3 )
243
+ # Draw x axes line
244
+ bitmaptools .draw_line (self ._axesx_bitmap , 0 , 0 , self ._usable_width - 1 , 0 , 2 )
245
+ # Draw y axes line
197
246
bitmaptools .draw_line (
198
247
self ._axesy_bitmap ,
199
248
self ._axesy_width - 1 ,
200
249
0 ,
201
250
self ._axesy_width - 1 ,
202
251
self ._usable_height - 1 ,
203
- 3 ,
252
+ 2 ,
204
253
)
205
254
206
255
def _draw_ticks (self ):
207
- for i in range (self ._margin , self ._usable_width , self ._tickx_separation ):
256
+ # X axes ticks
257
+
258
+ for i in range (
259
+ self ._tickx_separation , self ._usable_width , self ._tickx_separation
260
+ ):
208
261
bitmaptools .draw_line (
209
- self ._axesx_bitmap , i , self ._tick_line_height // 2 , i , 0 , 2
262
+ self ._axesx_bitmap , i , self ._tick_line_height , i , 0 , 2
210
263
)
211
-
212
- for i in range (self ._margin , self ._usable_height , self ._tickx_separation ):
264
+ # Y axes ticks
265
+ for i in range (
266
+ self ._usable_height - 1 - self ._ticky_separation , 0 , - self ._ticky_separation
267
+ ):
213
268
bitmaptools .draw_line (
214
269
self ._axesy_bitmap ,
215
- (self ._axesy_width - self ._tick_line_height // 2 ) - 1 ,
270
+ (self ._axesy_width - self ._tick_line_height ) - 1 ,
216
271
i ,
217
272
self ._axesy_width - 1 ,
218
273
i ,
0 commit comments