Skip to content

Commit 23216b1

Browse files
committed
adding ubar
1 parent d1cfec3 commit 23216b1

File tree

8 files changed

+108
-11
lines changed

8 files changed

+108
-11
lines changed

circuitpython_uplot/ubar.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2023 Jose D. Montoya
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
"""
6+
7+
`ubar`
8+
================================================================================
9+
10+
CircuitPython uscatter graph
11+
12+
* Author(s): Jose D. Montoya
13+
14+
15+
"""
16+
from bitmaptools import draw_line
17+
18+
# pylint: disable=too-many-arguments, invalid-name, protected-access
19+
# pylint: disable=too-few-public-methods, no-self-use
20+
class ubar:
21+
"""
22+
Main class to display different graphics
23+
"""
24+
25+
def __init__(self, plot, x: any, y: any, color=0xFFFFFF) -> None:
26+
27+
plot._drawbox()
28+
29+
self._graphx = abs(plot._newxmax - plot._newxmin) // (len(x) + 2)
30+
self._graphy = abs(plot._newymax - plot._newymin) // (max(y) + 2)
31+
self._new_min = int(plot.normalize(0, max(y), max(y), 0, 0))
32+
self._new_max = int(plot.normalize(0, max(y), max(y), 0, max(y)))
33+
xstart = self._graphx
34+
bar_space = 10
35+
plot._plot_palette[3] = color
36+
37+
for i, _ in enumerate(x):
38+
self._draw_rectangle(
39+
plot,
40+
xstart + (i * self._graphx),
41+
plot._newymin,
42+
self._graphx,
43+
self._graphy * y[i],
44+
3,
45+
)
46+
xstart = xstart + bar_space
47+
48+
def _draw_rectangle(self, plot, x, y, width, height, color):
49+
"""
50+
Helper function to draw bins rectangles
51+
"""
52+
draw_line(plot._plotbitmap, x, y, x + width, y, color)
53+
draw_line(plot._plotbitmap, x, y, x, y - height, color)
54+
draw_line(plot._plotbitmap, x + width, y, x + width, y - height, color)
55+
draw_line(plot._plotbitmap, x + width, y - height, x, y - height, color)

circuitpython_uplot/scatter.py renamed to circuitpython_uplot/uscatter.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55

66
"""
77
8-
`scatter`
8+
`uscatter`
99
================================================================================
1010
11-
CircuitPython scatter graph
11+
CircuitPython uscatter graph
1212
1313
* Author(s): Jose D. Montoya
1414
@@ -19,15 +19,15 @@
1919
from vectorio import Circle
2020

2121
# pylint: disable=too-few-public-methods, invalid-name
22-
class scatter:
22+
class uscatter:
2323
"""
2424
Main class to display different graphics
2525
"""
2626

2727
def __init__(self, plot, x: any, y: any, radius: int = 3) -> None:
2828
"""
2929
30-
:param plot: Plot object for the scatter to be drawn
30+
:param plot: Plot object for the uscatter to be drawn
3131
:param x: x points coordinates
3232
:param y: y points coordinates
3333
:param int radius: circle radius

docs/api.rst

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,9 @@
77
.. automodule:: circuitpython_uplot.uplot
88
:members:
99

10-
.. automodule:: circuitpython_uplot.scatter
10+
.. automodule:: circuitpython_uplot.uscatter
11+
:members:
12+
13+
14+
.. automodule:: circuitpython_uplot.ubar
1115
:members:

docs/examples.rst

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ Scatter Example
4646

4747
Scatter plot Example
4848

49-
.. literalinclude:: ../examples/uplot_scatter.py
50-
:caption: examples/uplot_scatter.py
49+
.. literalinclude:: ../examples/uplot_uscatter.py
50+
:caption: examples/uplot_uscatter.py
5151
:linenos:
5252
.. image:: ../docs/uplot_ex7.jpg
5353

@@ -60,3 +60,13 @@ Display Shapes integration example
6060
:caption: examples/uplot_display_shapes.py
6161
:linenos:
6262
.. image:: ../docs/uplot_ex6.jpg
63+
64+
Ubar Example
65+
----------------
66+
67+
ubar example
68+
69+
.. literalinclude:: ../examples/uplot_ubar_example.py
70+
:caption: examples/uplot_ubar_example.py
71+
:linenos:
72+
.. image:: ../docs/uplot_ex8.jpg

docs/uplot_ex8.jpg

10.2 KB
Loading

docs/uplot_ex8.jpg.license

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
SPDX-FileCopyrightText: 2023 Jose David M.
2+
3+
SPDX-License-Identifier: MIT

examples/uplot_ubar_example.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2023 Jose D. Montoya
2+
#
3+
# SPDX-License-Identifier: Unlicense
4+
5+
import time
6+
import board
7+
from circuitpython_uplot.uplot import Uplot
8+
from circuitpython_uplot.ubar import ubar
9+
10+
11+
# Setting up the display
12+
display = board.DISPLAY
13+
14+
plot = Uplot(0, 0, display.width, display.height)
15+
16+
# Setting up tick parameters
17+
plot.axs_params(axstype="box")
18+
a = ["a", "b", "c", "d"]
19+
b = [3, 5, 1, 7]
20+
ubar(plot, a, b, 0xFF1000)
21+
22+
# Plotting and showing the plot
23+
display.show(plot)
24+
25+
# Adding some wait time
26+
while True:
27+
time.sleep(1)

examples/uplot_scatter.py renamed to examples/uplot_uscatter.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import board
88
from ulab import numpy as np
99
from circuitpython_uplot.uplot import Uplot
10-
from circuitpython_uplot.scatter import scatter
10+
from circuitpython_uplot.uscatter import uscatter
1111

1212

1313
# Setting up the display
@@ -20,9 +20,7 @@
2020
plot.axs_params(axstype="cartesian")
2121
a = np.linspace(1, 100)
2222
b = [choice(a) for _ in a]
23-
scatter(plot, a, b)
24-
25-
# Seeting some date to plot
23+
uscatter(plot, a, b)
2624

2725
# Plotting and showing the plot
2826
display.show(plot)

0 commit comments

Comments
 (0)