Skip to content

Commit 4eece83

Browse files
committed
adding bar_color_change
1 parent 1343b7c commit 4eece83

File tree

5 files changed

+121
-4
lines changed

5 files changed

+121
-4
lines changed

circuitpython_uplot/ubar.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ def __init__(
8888
self._new_max = int(plot.transform(0, y_max, y_max, 0, y_max))
8989

9090
if color_palette is not None:
91+
self._palette_defined = True
9192
if projection:
9293
color_count = 2
9394
else:
@@ -97,6 +98,7 @@ def __init__(
9798
self._color_palette[i] = selected_color
9899
self._color_index = 0
99100
else:
101+
self._palette_defined = False
100102
self._color_palette = plot._plot_palette
101103
self._color_index = plot._index_colorused
102104
self._color_palette[self._color_index] = color
@@ -261,6 +263,40 @@ def update_values(self, values: list = None):
261263
element.height = height
262264
element.y = y
263265

266+
def update_colors(self, colors: list = None):
267+
"""
268+
Update Colors of the bars
269+
270+
:param list colors: list of colors to be asigned. list lenght must coincide with
271+
the numbers of bars in your plot
272+
273+
"""
274+
if colors is None:
275+
raise ValueError("You must provide a list of colors")
276+
if len(colors) != len(self._bars):
277+
raise ValueError("You must provide the same number of colors as bars")
278+
if self._palette_defined:
279+
for i in range(len(self._bars)):
280+
self._color_palette[i] = colors[i]
281+
else:
282+
for i in range(len(self._bars)):
283+
self._color_palette[i + 4] = colors[i]
284+
285+
def update_bar_color(self, bar_number: int, color: int = 0xFFFFFF):
286+
"""
287+
Update the color of a single bar
288+
289+
:param int bar_number: bar index. Bar number starts with :const`0`
290+
:param int color: new bar color. Defaults to :const:`0xFFFFFF`. White
291+
292+
"""
293+
if bar_number > len(self._bars):
294+
raise ValueError("Bar number must be a valid bar index")
295+
if self._palette_defined:
296+
self._color_palette[bar_number] = color
297+
else:
298+
self._color_palette[bar_number + 4] = color
299+
264300

265301
def color_fader(source_color=None, brightness=1.0, gamma=1.0):
266302
"""

docs/examples.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,15 @@ Bar Plot example showing how to update values for a filled bars bar plot
102102
:caption: examples/uplot_ubar_updating_values.py
103103
:lines: 5-
104104

105+
Bar plot updating bar colors Example
106+
-------------------------------------
107+
108+
Bar Plot example showing how to update colors for a filled bars bar plot
109+
110+
.. literalinclude:: ../examples/uplot_ubar_color_changing.py
111+
:caption: examples/uplot_ubar_color_changing.py
112+
:lines: 5-
113+
105114

106115
Ubar 3D Example
107116
----------------

docs/quick_start.rst

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -344,8 +344,8 @@ appearance
344344
ubar(plot, a, b, color=0xFF1000, fill=True, bar_space=30, xstart=70, projection=True)
345345
346346
347-
For filled unprojected bar you can update their values. This is useful for data logging.
348-
the max_value argument will allow you to set the maximum value of the graph. The plot will scale
347+
For filled unprojected bars you can update their values. This is useful for data logging.
348+
The max_value argument will allow you to set the maximum value of the graph. The plot will scale
349349
according to this max value, and bar plot will update their values accordingly
350350

351351
.. code-block:: python
@@ -362,13 +362,30 @@ according to this max value, and bar plot will update their values accordingly
362362
b = [3, 5, 1, 7]
363363
my_ubar = ubar(plot, a, b, color=0xFF1000, fill=True, color_palette=[0xFF1000, 0x00FF00, 0xFFFF00, 0x123456], max_value=10)
364364
365-
366365
Then you can update the values of the bar plot:
367366

368367
.. code-block:: python
369368
370369
my_ubar.update_values([1, 2, 3, 4])
371370
371+
372+
Also for Filled unprojected bars you can change all bars color at once. The following
373+
code will change all the bar's color to red
374+
375+
.. code-block:: python
376+
377+
my_ubar.update_colors(0xFF0000, 0xFF0000, 0xFF0000, 0xFF0000)
378+
379+
If you prefer, you can change the color of a single bar using the following code:
380+
381+
.. code-block:: python
382+
383+
my_ubar.update_bar_color(0, 0x0000FF)
384+
385+
This will change the first bar to Blue.
386+
387+
388+
372389
===============
373390
Fillbetween
374391
===============

examples/uplot_ubar_color_changing.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2023 Jose D. Montoya
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+
import time
9+
10+
# Setting up the display
11+
display = board.DISPLAY
12+
13+
# Configuring display dimensions
14+
DISPLAY_WIDTH = 480
15+
DISPLAY_HEIGHT = 320
16+
17+
# Defining the plot
18+
plot = Uplot(
19+
0,
20+
0,
21+
DISPLAY_WIDTH,
22+
DISPLAY_HEIGHT,
23+
background_color=color.BLACK,
24+
padding=10,
25+
box_color=color.WHITE,
26+
)
27+
28+
display.show(plot)
29+
30+
# Dummy data to plot
31+
some_values = [55, 20, 25, 30, 35, 10]
32+
a = ["a", "b", "c", "d", "e", "f"]
33+
34+
# Showing the plot
35+
display.show(plot)
36+
37+
# Creating the bar
38+
my_ubar = ubar(
39+
plot,
40+
a,
41+
some_values,
42+
0xFF1000,
43+
True,
44+
projection=False,
45+
max_value=50,
46+
)
47+
time.sleep(2)
48+
# Changing all the bars to Yellow
49+
my_ubar.update_colors(
50+
[color.YELLOW, color.YELLOW, color.YELLOW, color.YELLOW, color.YELLOW, color.YELLOW]
51+
)
52+
53+
time.sleep(2)
54+
55+
# Changing the 3 bar to Purple
56+
my_ubar.update_bar_color(2, color.PURPLE)

requirements.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries
21
# SPDX-FileCopyrightText: Copyright (c) 2023 Jose D. Montoya
32
#
43
# SPDX-License-Identifier: MIT

0 commit comments

Comments
 (0)