Skip to content

Commit b9339c6

Browse files
committed
added bar color palette argument
1 parent 55fd7b7 commit b9339c6

File tree

6 files changed

+103
-12
lines changed

6 files changed

+103
-12
lines changed

.pylintrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ max-public-methods=20
385385
max-returns=6
386386

387387
# Maximum number of statements in function / method body
388-
max-statements=50
388+
max-statements=60
389389

390390
# Minimum number of public methods for a class (see R0903).
391391
min-public-methods=1

circuitpython_uplot/ubar.py

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
except ImportError:
2020
pass
2121
import math
22+
from displayio import Palette
2223
from bitmaptools import draw_line
2324
from vectorio import Rectangle, Polygon
2425

@@ -43,6 +44,7 @@ def __init__(
4344
bar_space=16,
4445
xstart=50,
4546
projection=False,
47+
color_palette=None,
4648
) -> None:
4749
"""
4850
:param Uplot plot: Plot object for the scatter to be drawn
@@ -51,8 +53,11 @@ def __init__(
5153
:param int color: boxes color. Defaults to const:``0xFFFFFF``
5254
:param bool fill: boxes fill attribute. Defaults to `False`
5355
:param int bar_space: space in pixels between the bars
54-
:param int xstart: start point in the x axis for the bar to start. Default to :const:`50`
56+
:param int xstart: start point in the x axis for the bar to start. Defaults to :const:`50`
5557
:param bool projection: creates projection of the bars given them depth.
58+
:param list color_palette: list of colors to be used for the bars. Defaults to None.
59+
Be aware that you need to include the same number if colors as your data.
60+
This functionality will only work with filled bars.
5661
5762
"""
5863
y = [i * plot.scale for i in y]
@@ -66,7 +71,19 @@ def __init__(
6671
self._new_min = int(plot.transform(0, max(y), max(y), 0, 0))
6772
self._new_max = int(plot.transform(0, max(y), max(y), 0, max(y)))
6873

69-
plot._plot_palette[plot._index_colorused] = color
74+
if color_palette is not None:
75+
if projection:
76+
color_count = 2
77+
else:
78+
color_count = 1
79+
self._color_palette = Palette(len(color_palette) * color_count)
80+
for i, selected_color in enumerate(color_palette):
81+
self._color_palette[i] = selected_color
82+
self._color_index = 0
83+
else:
84+
self._color_palette = plot._plot_palette
85+
self._color_index = plot._index_colorused
86+
self._color_palette[self._color_index] = color
7087

7188
if plot._index_colorused >= 14:
7289
plot._index_colorused = 0
@@ -75,12 +92,12 @@ def __init__(
7592
for i, _ in enumerate(x):
7693
plot.append(
7794
Rectangle(
78-
pixel_shader=plot._plot_palette,
95+
pixel_shader=self._color_palette,
7996
width=self._graphx,
8097
height=self._graphy * y[i],
8198
x=xstart + (i * self._graphx),
8299
y=int(plot._newymin - self._graphy * y[i] / plot.scale),
83-
color_index=plot._index_colorused,
100+
color_index=self._color_index,
84101
)
85102
)
86103
if projection:
@@ -93,16 +110,16 @@ def __init__(
93110
(self._graphx - rx, 0 + ry),
94111
(0 - rx, 0 + ry),
95112
]
96-
plot._plot_palette[plot._index_colorused + 6] = color_fader(
97-
plot._plot_palette[plot._index_colorused], 0.7, 1
98-
)
113+
self._color_palette[
114+
self._color_index + len(color_palette)
115+
] = color_fader(self._color_palette[self._color_index], 0.7, 1)
99116
plot.append(
100117
Polygon(
101-
pixel_shader=plot._plot_palette,
118+
pixel_shader=self._color_palette,
102119
points=points,
103120
x=xstart + (i * self._graphx),
104121
y=plot._newymin - self._graphy * y[i],
105-
color_index=plot._index_colorused + 6,
122+
color_index=self._color_index + len(color_palette),
106123
)
107124
)
108125
points = [
@@ -113,11 +130,11 @@ def __init__(
113130
]
114131
plot.append(
115132
Polygon(
116-
pixel_shader=plot._plot_palette,
133+
pixel_shader=self._color_palette,
117134
points=points,
118135
x=xstart + (i * self._graphx),
119136
y=plot._newymin - self._graphy * y[i],
120-
color_index=plot._index_colorused + 6,
137+
color_index=self._color_index + len(color_palette),
121138
)
122139
)
123140

@@ -128,6 +145,7 @@ def __init__(
128145
)
129146
xstart = xstart + self._bar_space
130147
plot._index_colorused = plot._index_colorused + 1
148+
self._color_index = self._color_index + 1
131149
else:
132150
for i, _ in enumerate(x):
133151
self._draw_rectangle(

docs/bar_palette.jpg

13.2 KB
Loading

docs/examples.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,17 @@ Bar plot example showing how to use the scale
8282
.. image:: ../docs/bar_scale.jpg
8383

8484

85+
Ubar Color Palette Example
86+
----------------------------
87+
88+
Bar plot example showing how to pass a user color Palette
89+
90+
.. literalinclude:: ../examples/uplot_ubar_colorpalette.py
91+
:caption: examples/uplot_ubar_colorpalette.py
92+
:lines: 5-
93+
.. image:: ../docs/bar_palette.jpg
94+
95+
8596
Ubar 3D Example
8697
----------------
8798

docs/quick_start.rst

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,26 @@ You can select the color or and if the bars are filled
306306
ubar(plot, a, b, 0xFF1000, True)
307307
308308
309+
You can also select the bar spacing and the xstart position:
310+
311+
.. code-block:: python
312+
313+
ubar(plot, a, b, 0xFF1000, fill=True, bar_space=30, xstart=70)
314+
315+
For bar filled graphs you can pass a color_palette list. This will allow you to select the color of each bar
316+
This will not work for shell bars sadly.
317+
318+
.. code-block:: python
319+
320+
import board
321+
from circuitpython_uplot.uplot import Uplot
322+
from circuitpython_uplot.ubar import ubar
323+
324+
display = board.DISPLAY
325+
plot = Uplot(0, 0, display.width, display.height)
326+
ubar(plot, a, b, fill=True, bar_space=30, xstart=70, color_palette=[0xFF1000, 0x00FF00, 0x0000FF, 0x00FFFF])
327+
328+
309329
with the projection argument you can show the bars with projection. This will give them a 3D
310330
appearance
311331

examples/uplot_ubar_colorpalette.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2023 Jose D. Montoya, DJDevon
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
import board
6+
from circuitpython_uplot.uplot import Uplot, color
7+
from circuitpython_uplot.ubar import ubar
8+
9+
# Setting up the display
10+
display = board.DISPLAY
11+
12+
# Configuring display dimensions
13+
DISPLAY_WIDTH = 480
14+
DISPLAY_HEIGHT = 320
15+
16+
# Defining the plot
17+
plot = Uplot(
18+
0,
19+
0,
20+
DISPLAY_WIDTH,
21+
DISPLAY_HEIGHT,
22+
background_color=color.BLACK,
23+
padding=10,
24+
box_color=color.BLACK,
25+
)
26+
27+
# Dummy data to plot
28+
activities_latest_heart_value = [55, 20, 25, 30, 35, 10]
29+
a = ["a", "b", "c", "d", "e", "f"]
30+
31+
# Creating the Bar Plot
32+
ubar(
33+
plot,
34+
a,
35+
activities_latest_heart_value,
36+
0xFF1000,
37+
True,
38+
color_palette=[0xFF1000, 0x00FF00, 0x0000FF, 0xFFFF00, 0x00FFFF, 0x123456],
39+
)
40+
41+
# Showing the plot
42+
display.show(plot)

0 commit comments

Comments
 (0)