Skip to content

Commit 196e7e7

Browse files
committed
adding logging line_limits
1 parent e7bcf5d commit 196e7e7

File tree

4 files changed

+135
-3
lines changed

4 files changed

+135
-3
lines changed

circuitpython_uplot/logging.py

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
1515
"""
1616
try:
17-
from typing import Union
17+
from typing import Union, Optional
1818
from circuitpython_uplot.plot import Plot
1919
except ImportError:
2020
pass
@@ -42,6 +42,8 @@ def __init__(
4242
ticksy: Union[np.array, list] = np.array([0, 10, 30, 50, 70, 90]),
4343
tick_pos: bool = False,
4444
fill: bool = False,
45+
limits: Optional[list] = None,
46+
limits_color: int = 0xFF0000,
4547
) -> None:
4648
"""
4749
@@ -70,11 +72,22 @@ def __init__(
7072

7173
plot._plot_palette[plot._index_colorused] = line_color
7274

75+
self._line_index = plot._index_colorused
76+
7377
self.xmin = rangex[0]
7478
self.xmax = rangex[1]
7579
self.ymin = rangey[0]
7680
self.ymax = rangey[1]
7781

82+
if limits is not None:
83+
self._limits = np.array(
84+
plot.transform(
85+
self.ymin, self.ymax, plot._newymin, plot._newymax, np.array(limits)
86+
),
87+
dtype=np.int16,
88+
)
89+
plot._plot_palette[9] = limits_color
90+
7891
self.draw_points(plot, x, y, fill)
7992

8093
if plot._showticks:
@@ -186,7 +199,7 @@ def draw_new_lines(self, plot: Plot, x: list, y: list, fill: bool = False) -> No
186199
ynorm[index],
187200
xnorm[index + 1],
188201
ynorm[index + 1],
189-
plot._index_colorused,
202+
self._line_index,
190203
)
191204
if fill:
192205
for index, _ in enumerate(xnorm):
@@ -196,5 +209,23 @@ def draw_new_lines(self, plot: Plot, x: list, y: list, fill: bool = False) -> No
196209
ynorm[index],
197210
xnorm[index],
198211
plot._newymin,
199-
plot._index_colorused,
212+
self._line_index,
200213
)
214+
215+
def _draw_limit_lines(self, plot: Plot) -> None:
216+
draw_line(
217+
plot._plotbitmap,
218+
plot._newxmin,
219+
self._limits[0],
220+
plot._newxmax,
221+
self._limits[0],
222+
9,
223+
)
224+
draw_line(
225+
plot._plotbitmap,
226+
plot._newxmin,
227+
self._limits[1],
228+
plot._newxmax,
229+
self._limits[1],
230+
9,
231+
)

docs/examples.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,16 @@ This example shows how to animate a plot
309309
:caption: examples/logging_animation.py
310310
:lines: 5-
311311

312+
Logging Animation Example
313+
---------------------------------------
314+
315+
This example shows how to add limits to our plot
316+
317+
.. literalinclude:: ../examples/logging_limits.py
318+
:caption: examples/logging_limits.py
319+
:lines: 5-
320+
.. image:: ../docs/logging_limits.jpg
321+
312322
SVG Images examples
313323
---------------------------
314324

docs/logging_limits.jpg

22.2 KB
Loading

examples/logging_limits.py

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# SPDX-FileCopyrightText: Copyright (c) Jose D. Montoya
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
6+
import time
7+
import random
8+
import board
9+
from circuitpython_uplot.plot import Plot, color
10+
from circuitpython_uplot.logging import Logging
11+
12+
# Setting up the display
13+
display = board.DISPLAY
14+
display.auto_refresh = False
15+
16+
# Drawing the graph
17+
my_plot = Plot(
18+
140,
19+
60,
20+
200,
21+
200,
22+
padding=1,
23+
show_box=True,
24+
box_color=color.WHITE,
25+
)
26+
27+
# Setting the tick parameters
28+
my_plot.tick_params(
29+
tickx_height=4,
30+
ticky_height=4,
31+
show_ticks=True,
32+
tickcolor=color.TEAL,
33+
showtext=True,
34+
)
35+
36+
# Creating the x and y data
37+
x = [
38+
10,
39+
20,
40+
30,
41+
40,
42+
50,
43+
60,
44+
70,
45+
80,
46+
90,
47+
100,
48+
110,
49+
120,
50+
130,
51+
140,
52+
150,
53+
160,
54+
170,
55+
180,
56+
190,
57+
]
58+
y = [26, 32, 34, 30, 28, 35, 46, 65, 37, 23, 40, 27, 26, 36, 44, 53, 69, 27, 26]
59+
60+
# Creating the random numbers
61+
random_numbers = [32, 34, 45, 65, 24, 40, 18, 27]
62+
63+
64+
display.show(my_plot)
65+
display.refresh()
66+
67+
dist = 1
68+
69+
# Creating the loggraph
70+
my_loggraph = Logging(
71+
my_plot,
72+
x[0:dist],
73+
y[0:dist],
74+
rangex=[0, 210],
75+
rangey=[0, 110],
76+
line_color=color.BLUE,
77+
ticksx=[25, 50, 75, 100, 125, 150, 175, 200],
78+
ticksy=[25, 50, 75, 100],
79+
limits=[30, 60],
80+
)
81+
82+
# Showing the loggraph
83+
for i in range(45):
84+
if dist > len(x):
85+
y.pop(0)
86+
y.append(random.choice(random_numbers))
87+
88+
my_loggraph.draw_points(my_plot, x[0:dist], y[0:dist])
89+
display.refresh()
90+
dist += 1
91+
time.sleep(0.5)

0 commit comments

Comments
 (0)