Skip to content

Commit 5af4bf2

Browse files
committed
# TODO Make labels and text [√]
# TODO Make Styles applicable [ ] # TODO Animate when overflow [ ] # TODO Add Subticks functionality [ ] # TODO Updater to use local coordinates [ ]
1 parent a36b27c commit 5af4bf2

File tree

2 files changed

+97
-23
lines changed

2 files changed

+97
-23
lines changed

adafruit_displayio_layout/widgets/cartesian.py

Lines changed: 93 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
# SPDX-FileCopyrightText: 2021 Jose David Montoya
1+
# SPDX-FileCopyrightText: 2021 Jose David M.
22
#
33
# SPDX-License-Identifier: MIT
44
"""
55
66
`cartesian`
77
================================================================================
8-
A cartasian plane widget for displaying graphical information.
8+
A cartesian plane widget for displaying graphical information.
99
1010
* Author(s): Jose David Montoya
1111
@@ -28,6 +28,8 @@
2828
import terminalio
2929
import vectorio
3030
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
3133

3234
try:
3335
import bitmaptools
@@ -56,7 +58,8 @@ class Cartesian(Widget):
5658
:param int axes_stroke: axes lines thickness in pixels defaults to 2
5759
5860
: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
6063
6164
:param int tick_label_font: tick label text font
6265
:param int tick_label_color: tick label text color
@@ -77,7 +80,7 @@ class Cartesian(Widget):
7780
7881
.. code-block:: python
7982
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
8184
8285
Once you setup your display, you can now add ``my_plane`` to your display using:
8386
@@ -86,7 +89,7 @@ class Cartesian(Widget):
8689
display.show(my_plane) # add the group to the display
8790
8891
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
9093
group to the display as in this example:
9194
9295
.. code-block:: python
@@ -113,7 +116,7 @@ def __init__(
113116
xrange: Tuple[int, int] = (0, 100),
114117
yrange: Tuple[int, int] = (0, 100),
115118
axes_color: int = 0xFFFFFF,
116-
axes_stroke: int = 2,
119+
axes_stroke: int = 1,
117120
tick_color: int = 0xFFFFFF,
118121
major_tick_stroke: int = 1,
119122
major_tick_length: int = 5,
@@ -127,11 +130,13 @@ def __init__(
127130
# TODO Replace with drawline/vectorio [√]
128131
# TODO Make a rectangle function [√]
129132
# TODO Include functions to equal space ticks [√]
130-
# TODO Make labels and text [ ]
133+
# TODO Make labels and text []
131134
# TODO Make Styles applicable [ ]
132135
# TODO Animate when overflow [ ]
133136
# TODO Add Subticks functionality [ ]
134137
# TODO ticks evenly distributed [√]
138+
# TODO Make Ticker lines [√]
139+
# TODO Updater to use local coordinates [ ]
135140

136141
super().__init__(**kwargs, max_size=3)
137142
self._origin_x = x
@@ -162,8 +167,8 @@ def __init__(
162167
self._usable_width = self._widget_width
163168
self._usable_height = self._widget_height
164169

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
167172

168173
self._tick_bitmap = displayio.Bitmap(
169174
self._tick_line_thickness, self._tick_line_height, 3
@@ -237,34 +242,100 @@ def _get_font_height(font, scale):
237242
elif hasattr(font, "ascent"):
238243
font_height = int(scale * font.ascent + font.ascent)
239244
font_width = 12
245+
else:
246+
font_height = 12
247+
font_width = 12
240248
return font_width, font_height
241249

242250
def _draw_axes(self):
243251
# 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+
)
254286

255287
def _draw_ticks(self):
256288
# X axes ticks
257-
289+
tickcounter = 1
258290
for i in range(
259291
self._tickx_separation, self._usable_width, self._tickx_separation
260292
):
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+
261309
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,
263316
)
317+
tickcounter = tickcounter + 1
318+
264319
# Y axes ticks
320+
tickcounter = 0
265321
for i in range(
266322
self._usable_height - 1 - self._ticky_separation, 0, -self._ticky_separation
267323
):
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+
268339
bitmaptools.draw_line(
269340
self._axesy_bitmap,
270341
(self._axesy_width - self._tick_line_height) - 1,
@@ -273,6 +344,7 @@ def _draw_ticks(self):
273344
i,
274345
2,
275346
)
347+
tickcounter = tickcounter + 1
276348

277349
def _draw_pointers(self):
278350
self._pointer = vectorio.Circle(3)

examples/displayio_layout_cartesian_simpletest.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# SPDX-FileCopyrightText: 2021 Jose David Montoya
1+
# SPDX-FileCopyrightText: 2021 Jose David M.
22
#
33
# SPDX-License-Identifier: MIT
44
#############################
@@ -40,9 +40,11 @@
4040
display.show(my_group) # add high level Group to the display
4141

4242
posx = 0
43-
posy = 0
43+
posy = 100
4444

4545
while True:
4646
my_plane.update_pointer(posx, posy)
4747
display.show(my_group)
4848
time.sleep(0.5)
49+
posx = posx + 2
50+
posy = posy - 2

0 commit comments

Comments
 (0)