Skip to content

Commit a36b27c

Browse files
committed
# TODO Include functions to equal space ticks [√]
# TODO Make labels and text [ ] # TODO Make Styles applicable [ ] # TODO Animate when overflow [ ] # TODO Add Subticks functionality [ ] # TODO ticks evenly distributed [√]
1 parent b129077 commit a36b27c

File tree

2 files changed

+79
-15
lines changed

2 files changed

+79
-15
lines changed

adafruit_displayio_layout/widgets/cartesian.py

Lines changed: 70 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@
3333
import bitmaptools
3434
except NameError:
3535
pass # utilize the blit_rotate_scale function defined herein
36+
try:
37+
from typing import Tuple
38+
except ImportError:
39+
pass
3640

3741

3842
class Cartesian(Widget):
@@ -45,6 +49,9 @@ class Cartesian(Widget):
4549
:param int width: requested width, in pixels defaults to 100 pixels
4650
:param int height: requested height, in pixels defaults to 100 pixels
4751
52+
:param (int, int) axesx_range: X axes range
53+
:param (int, int) axesy_range: Y axes range
54+
4855
:param int axes_color: axes lines color defaults to white (0xFFFFFF)
4956
:param int axes_stroke: axes lines thickness in pixels defaults to 2
5057
@@ -57,6 +64,43 @@ class Cartesian(Widget):
5764
:param int pointer_radius: pointer radius in pixels defaults to 1
5865
:param int pointer_color: pointer color defaults to white (0xFFFFFF)
5966
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+
60104
"""
61105

62106
def __init__(
@@ -66,6 +110,8 @@ def __init__(
66110
display_color=0x000000,
67111
width: int = 100,
68112
height: int = 100,
113+
xrange: Tuple[int, int] = (0, 100),
114+
yrange: Tuple[int, int] = (0, 100),
69115
axes_color: int = 0xFFFFFF,
70116
axes_stroke: int = 2,
71117
tick_color: int = 0xFFFFFF,
@@ -80,12 +126,12 @@ def __init__(
80126
# TODO Make axes, separate from data [√]
81127
# TODO Replace with drawline/vectorio [√]
82128
# TODO Make a rectangle function [√]
83-
# TODO Include functions to equal space ticks [ ]
129+
# TODO Include functions to equal space ticks []
84130
# TODO Make labels and text [ ]
85131
# TODO Make Styles applicable [ ]
86132
# TODO Animate when overflow [ ]
87133
# TODO Add Subticks functionality [ ]
88-
# TODO ticks evenly distributed [ ]
134+
# TODO ticks evenly distributed []
89135

90136
super().__init__(**kwargs, max_size=3)
91137
self._origin_x = x
@@ -113,9 +159,11 @@ def __init__(
113159
self._font_width = self._get_font_height(self._font, 1)[0]
114160
self._font_height = self._get_font_height(self._font, 1)[1]
115161

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)
119167

120168
self._tick_bitmap = displayio.Bitmap(
121169
self._tick_line_thickness, self._tick_line_height, 3
@@ -134,7 +182,7 @@ def __init__(
134182
self._axesy_width = (
135183
2
136184
+ self._axes_line_thickness
137-
+ self._font_height
185+
+ self._font_width
138186
+ self._tick_line_height // 2
139187
)
140188
self._axesy_bitmap = displayio.Bitmap(self._axesy_width, self._usable_height, 4)
@@ -143,7 +191,7 @@ def __init__(
143191
self._screen_bitmap = displayio.Bitmap(
144192
self._usable_width, self._usable_height, 3
145193
)
146-
194+
self._screen_bitmap.fill(0)
147195
self._screen_palette = displayio.Palette(6)
148196
self._screen_palette.make_transparent(0)
149197
self._screen_palette[1] = self._tick_color
@@ -192,27 +240,34 @@ def _get_font_height(font, scale):
192240
return font_width, font_height
193241

194242
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
197246
bitmaptools.draw_line(
198247
self._axesy_bitmap,
199248
self._axesy_width - 1,
200249
0,
201250
self._axesy_width - 1,
202251
self._usable_height - 1,
203-
3,
252+
2,
204253
)
205254

206255
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+
):
208261
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
210263
)
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+
):
213268
bitmaptools.draw_line(
214269
self._axesy_bitmap,
215-
(self._axesy_width - self._tick_line_height // 2) - 1,
270+
(self._axesy_width - self._tick_line_height) - 1,
216271
i,
217272
self._axesy_width - 1,
218273
i,

docs/examples.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,12 @@ Create multiple sliding switch with various sizes and orientations.
2424
.. literalinclude:: ../examples/displayio_layout_switch_multiple.py
2525
:caption: examples/displayio_layout_switch_multiple.py
2626
:linenos:
27+
28+
Cartesian plane simple test
29+
---------------------------
30+
31+
Create a simple plot plane.
32+
33+
.. literalinclude:: ../examples/displayio_layout_cartesian_simpletest.py
34+
:caption: examples/displayio_layout_cartesian_simpletest.py
35+
:linenos:

0 commit comments

Comments
 (0)