Skip to content

Commit b118df4

Browse files
committed
Ubar - Adding 3D bars
1 parent 2ed762e commit b118df4

File tree

1 file changed

+84
-10
lines changed

1 file changed

+84
-10
lines changed

circuitpython_uplot/ubar.py

Lines changed: 84 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,43 +13,51 @@
1313
1414
1515
"""
16+
1617
try:
1718
from circuitpython_uplot.uplot import Uplot
1819
except ImportError:
1920
pass
21+
import math
2022
from bitmaptools import draw_line
21-
from vectorio import Rectangle
23+
from vectorio import Rectangle, Polygon
2224

2325
__version__ = "0.0.0+auto.0"
2426
__repo__ = "https://github.com/adafruit/CircuitPython_uplot.git"
2527

2628
# pylint: disable=too-many-arguments, invalid-name, protected-access
27-
# pylint: disable=too-few-public-methods, no-self-use
29+
# pylint: disable=too-few-public-methods, no-self-use, too-many-locals
2830
class ubar:
2931
"""
3032
Main class to display different graphics
3133
"""
3234

3335
def __init__(
34-
self, plot: Uplot, x: list, y: list, color: int = 0xFFFFFF, fill: bool = False
36+
self,
37+
plot: Uplot,
38+
x: list,
39+
y: list,
40+
color: int = 0xFFFFFF,
41+
fill: bool = False,
42+
bar_space=16,
43+
xstart=50,
3544
) -> None:
3645
"""
3746
:param Uplot plot: Plot object for the scatter to be drawn
3847
:param list x: x data
3948
:param list y: y data
4049
:param int color: boxes color. Defaults to const:``0xFFFFFF``
4150
:param bool fill: boxes fill attribute. Defaults to `False`
51+
:param int bar_space: space in pixels between the bars
52+
:param int xstart: start point in the x axis for the bar to start. Default to :const:`50`
4253
4354
"""
44-
45-
self._graphx = abs(plot._newxmax - plot._newxmin) // (len(x) + 2)
55+
self._bar_space = bar_space
56+
self._graphx = abs(plot._newxmax - plot._newxmin) // (len(x) + 4)
4657
self._graphy = abs(plot._newymax - plot._newymin) // (max(y) + 2)
4758
self._new_min = int(plot.transform(0, max(y), max(y), 0, 0))
4859
self._new_max = int(plot.transform(0, max(y), max(y), 0, max(y)))
4960

50-
bar_space = max(2, plot._width // 30)
51-
xstart = self._graphx + plot._newxmin
52-
5361
plot._plot_palette[plot._index_colorused] = color
5462

5563
if fill:
@@ -64,15 +72,50 @@ def __init__(
6472
color_index=plot._index_colorused,
6573
)
6674
)
75+
76+
delta = 20
77+
rx = int(delta * math.cos(-0.5))
78+
ry = int(delta * math.sin(-0.5))
79+
points = [
80+
(0, 0),
81+
(self._graphx, 0),
82+
(self._graphx - rx, 0 + ry),
83+
(0 - rx, 0 + ry),
84+
]
85+
86+
plot.append(
87+
Polygon(
88+
pixel_shader=plot._plot_palette,
89+
points=points,
90+
x=xstart + (i * self._graphx),
91+
y=plot._newymin - self._graphy * y[i],
92+
color_index=plot._index_colorused - 1,
93+
)
94+
)
95+
points = [
96+
(0, 0),
97+
(0 - rx, 0 + ry),
98+
(0 - rx, self._graphy * y[i]),
99+
(0, self._graphy * y[i]),
100+
]
101+
plot.append(
102+
Polygon(
103+
pixel_shader=plot._plot_palette,
104+
points=points,
105+
x=xstart + (i * self._graphx),
106+
y=plot._newymin - self._graphy * y[i],
107+
color_index=plot._index_colorused + 1,
108+
)
109+
)
110+
67111
plot.show_text(
68112
str(y[i]),
69113
xstart + (i * self._graphx) + self._graphx // 2,
70114
plot._newymin,
71115
)
72-
xstart = xstart + bar_space
116+
xstart = xstart + self._bar_space
73117
plot._index_colorused = plot._index_colorused + 1
74118
else:
75-
76119
for i, _ in enumerate(x):
77120
self._draw_rectangle(
78121
plot,
@@ -82,6 +125,37 @@ def __init__(
82125
self._graphy * y[i],
83126
plot._index_colorused,
84127
)
128+
delta = 20
129+
rx = int(delta * math.cos(-0.5))
130+
ry = int(delta * math.sin(-0.5))
131+
x0 = xstart + (i * self._graphx)
132+
y0 = plot._newymin - self._graphy * y[i]
133+
x1 = xstart + (i * self._graphx) + self._graphx
134+
y1 = plot._newymin - self._graphy * y[i]
135+
136+
draw_line(
137+
plot._plotbitmap, x0, y0, x0 - rx, y0 + ry, plot._index_colorused
138+
)
139+
draw_line(
140+
plot._plotbitmap, x1, y1, x1 - rx, y1 + ry, plot._index_colorused
141+
)
142+
draw_line(
143+
plot._plotbitmap,
144+
x0 - rx,
145+
y0 + ry,
146+
x1 - rx,
147+
y1 + ry,
148+
plot._index_colorused,
149+
)
150+
draw_line(
151+
plot._plotbitmap,
152+
x0 - rx,
153+
y0 + ry,
154+
x0 - rx,
155+
plot._newymin,
156+
plot._index_colorused,
157+
)
158+
85159
xstart = xstart + bar_space
86160
plot._index_colorused = plot._index_colorused + 1
87161
plot.show_text(

0 commit comments

Comments
 (0)