Skip to content

Commit 57389cd

Browse files
committed
adding upie
1 parent 23216b1 commit 57389cd

File tree

7 files changed

+138
-5
lines changed

7 files changed

+138
-5
lines changed

circuitpython_uplot/ubar.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@
1717

1818
# pylint: disable=too-many-arguments, invalid-name, protected-access
1919
# pylint: disable=too-few-public-methods, no-self-use
20+
21+
__version__ = "0.0.0+auto.0"
22+
__repo__ = "https://github.com/adafruit/CircuitPython_uplot.git"
23+
24+
2025
class ubar:
2126
"""
2227
Main class to display different graphics

circuitpython_uplot/upie.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2023 Jose D. Montoya
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
"""
6+
7+
`upie`
8+
================================================================================
9+
10+
CircuitPython pie graph
11+
12+
* Author(s): Jose D. Montoya
13+
14+
15+
"""
16+
17+
import math
18+
import bitmaptools
19+
20+
__version__ = "0.0.0+auto.0"
21+
__repo__ = "https://github.com/adafruit/CircuitPython_uplot.git"
22+
23+
# pylint: disable=too-many-arguments, invalid-name, no-self-use
24+
class upie:
25+
"""
26+
Class to draw pie
27+
"""
28+
29+
def __init__(self, plot, data: list, x: int = 130, y: int = 130, radius: int = 50):
30+
"""
31+
32+
:param plot: Plot object for the upie to be drawn
33+
:param list data: data to make the pie
34+
:param x: pie center x coordinate
35+
:param y: pie center y coordinate
36+
:param int radius: pie radius
37+
38+
"""
39+
40+
plot._drawbox()
41+
42+
step = 2
43+
total = sum(data)
44+
per = [int(i / total * 360) for i in data]
45+
46+
start = 0
47+
end = 0
48+
index_color = 4
49+
50+
for pie in per:
51+
end = end + pie
52+
for i in range(start, end, step):
53+
self.draw_lines(plot._plotbitmap, x, y, radius, i, index_color)
54+
start = start + pie
55+
index_color = index_color + 1
56+
57+
def draw_lines(self, bitmap, x, y, radius, angle, color):
58+
"""
59+
60+
:param bitmap: bitmap to be drawn in
61+
:param x: center x coordinate
62+
:param y: center y coordinate
63+
:param radius: pie radius in pixels
64+
:param angle: line angle
65+
:param color: color index
66+
:return: None
67+
68+
"""
69+
return bitmaptools.draw_line(
70+
bitmap,
71+
x,
72+
y,
73+
x + int(radius * math.cos(angle * math.pi / 180)),
74+
y + int(radius * math.sin(angle * math.pi / 180)),
75+
color,
76+
)

circuitpython_uplot/uplot.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,26 +68,37 @@ def __init__(
6868

6969
self.padding = padding
7070
self._newxmin = padding
71-
self._newxmax = width - 1 * padding
72-
self._newymin = height - 1 * padding
71+
self._newxmax = width - padding
72+
self._newymin = height - padding
7373
self._newymax = padding
7474

7575
self._tickheight = 8
7676
self._tickcolor = 0xFFFFFF
7777
self._showticks = False
7878
self._tickgrid = False
7979

80+
self._barcolor = 0xFFFFFF
81+
82+
self._piecolor = 0xFFFFFF
83+
8084
self._width = width
8185
self._height = height
8286

8387
self._axeslinethikness = 1
8488

85-
self._plotbitmap = displayio.Bitmap(width, height, 4)
89+
self._plotbitmap = displayio.Bitmap(width, height, 10)
8690

87-
self._plot_palette = displayio.Palette(4)
91+
self._plot_palette = displayio.Palette(10)
8892
self._plot_palette[0] = 0x000000
8993
self._plot_palette[1] = 0xFFFFFF
9094
self._plot_palette[2] = self._tickcolor
95+
self._plot_palette[3] = self._barcolor
96+
self._plot_palette[4] = 0xFFFF00
97+
self._plot_palette[5] = 0xFF0000
98+
self._plot_palette[6] = 0x7428EF
99+
self._plot_palette[7] = 0x005E99
100+
self._plot_palette[8] = 0x00A76D
101+
self._plot_palette[9] = 0x2C4971
91102
self.append(
92103
displayio.TileGrid(
93104
self._plotbitmap, pixel_shader=self._plot_palette, x=x, y=y

circuitpython_uplot/uscatter.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
import displayio
1919
from vectorio import Circle
2020

21+
__version__ = "0.0.0+auto.0"
22+
__repo__ = "https://github.com/adafruit/CircuitPython_uplot.git"
23+
2124
# pylint: disable=too-few-public-methods, invalid-name
2225
class uscatter:
2326
"""

docs/api.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
.. automodule:: circuitpython_uplot.uscatter
1111
:members:
1212

13-
1413
.. automodule:: circuitpython_uplot.ubar
1514
:members:
15+
16+
.. automodule:: circuitpython_uplot.upie
17+
:members:

docs/examples.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,12 @@ ubar example
7070
:caption: examples/uplot_ubar_example.py
7171
:linenos:
7272
.. image:: ../docs/uplot_ex8.jpg
73+
74+
Upie Example
75+
----------------
76+
77+
upie example
78+
79+
.. literalinclude:: ../examples/uplot_upie_example.py
80+
:caption: examples/uplot_upie_example.py
81+
:linenos:

examples/uplot_upie_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.upie import upie
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 = [5, 2, 7]
19+
20+
upie(plot, a)
21+
22+
# Plotting and showing the plot
23+
display.show(plot)
24+
25+
# Adding some wait time
26+
while True:
27+
time.sleep(1)

0 commit comments

Comments
 (0)