Skip to content

Commit d2da1d1

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

32 files changed

+132
-122
lines changed

.pre-commit-config.yaml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@ repos:
77
rev: 22.3.0
88
hooks:
99
- id: black
10-
- repo: https://github.com/fsfe/reuse-tool
11-
rev: v0.14.0
12-
hooks:
13-
- id: reuse
1410
- repo: https://github.com/pre-commit/pre-commit-hooks
1511
rev: v4.2.0
1612
hooks:

README.rst.license

Lines changed: 0 additions & 3 deletions
This file was deleted.

circuitpython_uplot/ubar.py

Lines changed: 72 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ def __init__(
4141
fill: bool = False,
4242
bar_space=16,
4343
xstart=50,
44+
projection=False,
4445
) -> None:
4546
"""
4647
:param Uplot plot: Plot object for the scatter to be drawn
@@ -50,6 +51,7 @@ def __init__(
5051
:param bool fill: boxes fill attribute. Defaults to `False`
5152
:param int bar_space: space in pixels between the bars
5253
:param int xstart: start point in the x axis for the bar to start. Default to :const:`50`
54+
:param bool projection: creates projection of the bars given them depth.
5355
5456
"""
5557
self._bar_space = bar_space
@@ -72,41 +74,43 @@ def __init__(
7274
color_index=plot._index_colorused,
7375
)
7476
)
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,
77+
if projection:
78+
delta = 20
79+
rx = int(delta * math.cos(-0.5))
80+
ry = int(delta * math.sin(-0.5))
81+
points = [
82+
(0, 0),
83+
(self._graphx, 0),
84+
(self._graphx - rx, 0 + ry),
85+
(0 - rx, 0 + ry),
86+
]
87+
plot._plot_palette[plot._index_colorused + 6] = color_fader(
88+
plot._plot_palette[plot._index_colorused], 0.7, 1
9389
)
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,
90+
plot.append(
91+
Polygon(
92+
pixel_shader=plot._plot_palette,
93+
points=points,
94+
x=xstart + (i * self._graphx),
95+
y=plot._newymin - self._graphy * y[i],
96+
color_index=plot._index_colorused + 6,
97+
)
98+
)
99+
points = [
100+
(0, 0),
101+
(0 - rx, 0 + ry),
102+
(0 - rx, self._graphy * y[i]),
103+
(0, self._graphy * y[i]),
104+
]
105+
plot.append(
106+
Polygon(
107+
pixel_shader=plot._plot_palette,
108+
points=points,
109+
x=xstart + (i * self._graphx),
110+
y=plot._newymin - self._graphy * y[i],
111+
color_index=plot._index_colorused + 6,
112+
)
108113
)
109-
)
110114

111115
plot.show_text(
112116
str(y[i]),
@@ -174,3 +178,38 @@ def _draw_rectangle(
174178
draw_line(plot._plotbitmap, x, y, x, y - height, color)
175179
draw_line(plot._plotbitmap, x + width, y, x + width, y - height, color)
176180
draw_line(plot._plotbitmap, x + width, y - height, x, y - height, color)
181+
182+
183+
def color_fader(source_color=None, brightness=1.0, gamma=1.0):
184+
"""
185+
Function taken from https://github.com/CedarGroveStudios
186+
Copyright (c) 2022 JG for Cedar Grove Maker Studios
187+
License: MIT
188+
189+
Scale a 24-bit RGB source color value in proportion to the brightness
190+
setting (0 to 1.0). Returns an adjusted 24-bit RGB color value or None if
191+
the source color is None (transparent). The adjusted color's gamma value is
192+
typically from 0.0 to 2.0 with a default of 1.0 for no gamma adjustment.
193+
194+
:param int source_color: The color value to be adjusted. Default is None.
195+
:param float brightness: The brightness value for color value adjustment.
196+
Value range is 0.0 to 1.0. Default is 1.0 (maximum brightness).
197+
:param float gamma: The gamma value for color value adjustment. Value range
198+
is 0.0 to 2.0. Default is 1.0 (no gamma adjustment).
199+
200+
:return int: The adjusted color value."""
201+
202+
if source_color is None:
203+
return source_color
204+
205+
# Extract primary colors and scale to brightness
206+
r = min(int(brightness * ((source_color & 0xFF0000) >> 16)), 0xFF)
207+
g = min(int(brightness * ((source_color & 0x00FF00) >> 8)), 0xFF)
208+
b = min(int(brightness * ((source_color & 0x0000FF) >> 0)), 0xFF)
209+
210+
# Adjust result for gamma
211+
r = min(int(round((r**gamma), 0)), 0xFF)
212+
g = min(int(round((g**gamma), 0)), 0xFF)
213+
b = min(int(round((b**gamma), 0)), 0xFF)
214+
215+
return (r << 16) + (g << 8) + b

circuitpython_uplot/uplot.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,12 +127,12 @@ def __init__(
127127

128128
self._index_colorused = 4
129129

130-
self._plotbitmap = displayio.Bitmap(width, height, 14)
130+
self._plotbitmap = displayio.Bitmap(width, height, 17)
131131

132132
if show_box:
133133
self._drawbox()
134134

135-
self._plot_palette = displayio.Palette(14)
135+
self._plot_palette = displayio.Palette(17)
136136
self._plot_palette[0] = background_color
137137
self._plot_palette[1] = box_color
138138
self._plot_palette[2] = self._tickcolor

docs/api.rst.license

Lines changed: 0 additions & 4 deletions
This file was deleted.

docs/examples.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,16 @@ ubar example
7171
:linenos:
7272
.. image:: ../docs/uplot_ex8.jpg
7373

74+
Ubar 3D Example
75+
----------------
76+
77+
ubar 3D example
78+
79+
.. literalinclude:: ../examples/uplot_ubar_3Dbars.py
80+
:caption: examples/uplot_ubar_3Dbars.py
81+
:linenos:
82+
.. image:: ../docs/uplot_3DBars.jpg
83+
7484
Upie Example
7585
----------------
7686

docs/examples.rst.license

Lines changed: 0 additions & 4 deletions
This file was deleted.

docs/index.rst.license

Lines changed: 0 additions & 4 deletions
This file was deleted.

docs/logging.png.license

Lines changed: 0 additions & 3 deletions
This file was deleted.

docs/quick_start.rst

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,6 @@ You can choose some colors directly from the library. This can be done by import
141141
from circuitpython_uplot.uplot import color
142142
143143
This will allow you to use the colors in the list as color variable definitions
144-
145144
* WHITE
146145
* BLACK
147146
* RED
@@ -189,7 +188,6 @@ After the initial setup we add our xy plane and show our plot
189188
190189
191190
There are some parameters that you can customize:
192-
193191
* rangex and rangey: you could specify the ranges of your graph. Allowing you to move your graph according to your needs. This parameters only accept lists
194192
* line color: you could specify the color in HEX
195193
* fill: if you selected this as `True` the area under your graph will be filled
@@ -264,7 +262,6 @@ Creates a scatter plot with x,y data. You can customize the circle diameter if y
264262
265263
266264
There are some parameters that you can customize:
267-
268265
* rangex and rangey: you can specify the ranges of your graph. This allows you to move your graph according to your needs. This parameters only accept lists
269266
* radius: circles radius/radii
270267
* circle_color: you can specify the color in HEX
@@ -284,7 +281,7 @@ Bar Plot
284281
===============
285282

286283
Allows you to graph bar plots. You just need to give the values of the bar in a python list.
287-
You can choose to create shell or filled bars
284+
You can choose to create shell or filled bars.
288285

289286
.. code-block:: python
290287
@@ -308,6 +305,24 @@ You can select the color or and if the bars are filled
308305
ubar(plot, a, b, 0xFF1000, True)
309306
310307
308+
with the projection argument you can show the bars with projection. This will give them a 3D
309+
appearance
310+
311+
.. code-block:: python
312+
313+
import board
314+
from circuitpython_uplot.uplot import Uplot
315+
from circuitpython_uplot.ubar import ubar
316+
317+
display = board.DISPLAY
318+
plot = Uplot(0, 0, display.width, display.height)
319+
320+
321+
a = ["a", "b", "c", "d"]
322+
b = [3, 5, 1, 7]
323+
ubar(plot, a, b, color=0xFF1000, fill=True, bar_space=30, xstart=70, projection=True)
324+
325+
311326
===============
312327
Fillbetween
313328
===============
@@ -380,7 +395,6 @@ This is a similar to Cartesian but designed to allow the user to use it as a dat
380395
The user needs to manually set up the range and tick values in order for this graph to work properly
381396

382397
There are some parameters that you can customize:
383-
384398
* rangex and rangey: you need specify the ranges of your graph. This allows you to move your graph according to your needs. This parameters only accept lists
385399
* ticksx and ticksy: Specific ticks for the X and Y axes
386400
* line_color: you can specify the color in HEX

docs/quick_start.rst.license

Lines changed: 0 additions & 3 deletions
This file was deleted.

docs/readme.png.license

Lines changed: 0 additions & 3 deletions
This file was deleted.

docs/readme2.png.license

Lines changed: 0 additions & 3 deletions
This file was deleted.

docs/uplot_3DBars.jpg

20 KB
Loading

docs/uplot_cartesian.gif.license

Lines changed: 0 additions & 3 deletions
This file was deleted.

docs/uplot_ex1.jpg.license

Lines changed: 0 additions & 3 deletions
This file was deleted.

docs/uplot_ex10.jpg.license

Lines changed: 0 additions & 3 deletions
This file was deleted.

docs/uplot_ex11.jpg.license

Lines changed: 0 additions & 3 deletions
This file was deleted.

docs/uplot_ex12.jpg.license

Lines changed: 0 additions & 3 deletions
This file was deleted.

docs/uplot_ex15.jpg.license

Lines changed: 0 additions & 3 deletions
This file was deleted.

docs/uplot_ex16.jpg.license

Lines changed: 0 additions & 3 deletions
This file was deleted.

docs/uplot_ex17.jpg.license

Lines changed: 0 additions & 3 deletions
This file was deleted.

docs/uplot_ex22.jpg.license

Lines changed: 0 additions & 3 deletions
This file was deleted.

docs/uplot_ex23.jpg.license

Lines changed: 0 additions & 3 deletions
This file was deleted.

docs/uplot_ex3.jpg.license

Lines changed: 0 additions & 3 deletions
This file was deleted.

docs/uplot_ex4.jpg.license

Lines changed: 0 additions & 3 deletions
This file was deleted.

docs/uplot_ex5.jpg.license

Lines changed: 0 additions & 3 deletions
This file was deleted.

docs/uplot_ex6.jpg.license

Lines changed: 0 additions & 3 deletions
This file was deleted.

docs/uplot_ex7.jpg.license

Lines changed: 0 additions & 3 deletions
This file was deleted.

docs/uplot_ex8.jpg.license

Lines changed: 0 additions & 3 deletions
This file was deleted.

docs/uplot_ex9.jpg.license

Lines changed: 0 additions & 3 deletions
This file was deleted.

examples/uplot_ubar_3Dbars.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2023 Jose D. Montoya
2+
#
3+
# SPDX-License-Identifier: MIT
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", "e"]
19+
b = [3, 5, 1, 9, 7]
20+
21+
# Creating a 3D bar
22+
ubar(plot, a, b, color=0xFF1000, fill=True, bar_space=30, xstart=70, projection=True)
23+
24+
# Plotting and showing the plot
25+
display.show(plot)
26+
27+
# Adding some wait time
28+
while True:
29+
time.sleep(1)

0 commit comments

Comments
 (0)