Skip to content

Commit

Permalink
Ubar - Adding 3D bars
Browse files Browse the repository at this point in the history
  • Loading branch information
jposada202020 committed May 20, 2023
1 parent b118df4 commit d2da1d1
Show file tree
Hide file tree
Showing 32 changed files with 132 additions and 122 deletions.
4 changes: 0 additions & 4 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ repos:
rev: 22.3.0
hooks:
- id: black
- repo: https://github.com/fsfe/reuse-tool
rev: v0.14.0
hooks:
- id: reuse
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.2.0
hooks:
Expand Down
3 changes: 0 additions & 3 deletions README.rst.license

This file was deleted.

105 changes: 72 additions & 33 deletions circuitpython_uplot/ubar.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def __init__(
fill: bool = False,
bar_space=16,
xstart=50,
projection=False,
) -> None:
"""
:param Uplot plot: Plot object for the scatter to be drawn
Expand All @@ -50,6 +51,7 @@ def __init__(
:param bool fill: boxes fill attribute. Defaults to `False`
:param int bar_space: space in pixels between the bars
:param int xstart: start point in the x axis for the bar to start. Default to :const:`50`
:param bool projection: creates projection of the bars given them depth.
"""
self._bar_space = bar_space
Expand All @@ -72,41 +74,43 @@ def __init__(
color_index=plot._index_colorused,
)
)

delta = 20
rx = int(delta * math.cos(-0.5))
ry = int(delta * math.sin(-0.5))
points = [
(0, 0),
(self._graphx, 0),
(self._graphx - rx, 0 + ry),
(0 - rx, 0 + ry),
]

plot.append(
Polygon(
pixel_shader=plot._plot_palette,
points=points,
x=xstart + (i * self._graphx),
y=plot._newymin - self._graphy * y[i],
color_index=plot._index_colorused - 1,
if projection:
delta = 20
rx = int(delta * math.cos(-0.5))
ry = int(delta * math.sin(-0.5))
points = [
(0, 0),
(self._graphx, 0),
(self._graphx - rx, 0 + ry),
(0 - rx, 0 + ry),
]
plot._plot_palette[plot._index_colorused + 6] = color_fader(
plot._plot_palette[plot._index_colorused], 0.7, 1
)
)
points = [
(0, 0),
(0 - rx, 0 + ry),
(0 - rx, self._graphy * y[i]),
(0, self._graphy * y[i]),
]
plot.append(
Polygon(
pixel_shader=plot._plot_palette,
points=points,
x=xstart + (i * self._graphx),
y=plot._newymin - self._graphy * y[i],
color_index=plot._index_colorused + 1,
plot.append(
Polygon(
pixel_shader=plot._plot_palette,
points=points,
x=xstart + (i * self._graphx),
y=plot._newymin - self._graphy * y[i],
color_index=plot._index_colorused + 6,
)
)
points = [
(0, 0),
(0 - rx, 0 + ry),
(0 - rx, self._graphy * y[i]),
(0, self._graphy * y[i]),
]
plot.append(
Polygon(
pixel_shader=plot._plot_palette,
points=points,
x=xstart + (i * self._graphx),
y=plot._newymin - self._graphy * y[i],
color_index=plot._index_colorused + 6,
)
)
)

plot.show_text(
str(y[i]),
Expand Down Expand Up @@ -174,3 +178,38 @@ def _draw_rectangle(
draw_line(plot._plotbitmap, x, y, x, y - height, color)
draw_line(plot._plotbitmap, x + width, y, x + width, y - height, color)
draw_line(plot._plotbitmap, x + width, y - height, x, y - height, color)


def color_fader(source_color=None, brightness=1.0, gamma=1.0):
"""
Function taken from https://github.com/CedarGroveStudios
Copyright (c) 2022 JG for Cedar Grove Maker Studios
License: MIT
Scale a 24-bit RGB source color value in proportion to the brightness
setting (0 to 1.0). Returns an adjusted 24-bit RGB color value or None if
the source color is None (transparent). The adjusted color's gamma value is
typically from 0.0 to 2.0 with a default of 1.0 for no gamma adjustment.
:param int source_color: The color value to be adjusted. Default is None.
:param float brightness: The brightness value for color value adjustment.
Value range is 0.0 to 1.0. Default is 1.0 (maximum brightness).
:param float gamma: The gamma value for color value adjustment. Value range
is 0.0 to 2.0. Default is 1.0 (no gamma adjustment).
:return int: The adjusted color value."""

if source_color is None:
return source_color

# Extract primary colors and scale to brightness
r = min(int(brightness * ((source_color & 0xFF0000) >> 16)), 0xFF)
g = min(int(brightness * ((source_color & 0x00FF00) >> 8)), 0xFF)
b = min(int(brightness * ((source_color & 0x0000FF) >> 0)), 0xFF)

# Adjust result for gamma
r = min(int(round((r**gamma), 0)), 0xFF)
g = min(int(round((g**gamma), 0)), 0xFF)
b = min(int(round((b**gamma), 0)), 0xFF)

return (r << 16) + (g << 8) + b
4 changes: 2 additions & 2 deletions circuitpython_uplot/uplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,12 @@ def __init__(

self._index_colorused = 4

self._plotbitmap = displayio.Bitmap(width, height, 14)
self._plotbitmap = displayio.Bitmap(width, height, 17)

if show_box:
self._drawbox()

self._plot_palette = displayio.Palette(14)
self._plot_palette = displayio.Palette(17)
self._plot_palette[0] = background_color
self._plot_palette[1] = box_color
self._plot_palette[2] = self._tickcolor
Expand Down
4 changes: 0 additions & 4 deletions docs/api.rst.license

This file was deleted.

10 changes: 10 additions & 0 deletions docs/examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,16 @@ ubar example
:linenos:
.. image:: ../docs/uplot_ex8.jpg

Ubar 3D Example
----------------

ubar 3D example

.. literalinclude:: ../examples/uplot_ubar_3Dbars.py
:caption: examples/uplot_ubar_3Dbars.py
:linenos:
.. image:: ../docs/uplot_3DBars.jpg

Upie Example
----------------

Expand Down
4 changes: 0 additions & 4 deletions docs/examples.rst.license

This file was deleted.

4 changes: 0 additions & 4 deletions docs/index.rst.license

This file was deleted.

3 changes: 0 additions & 3 deletions docs/logging.png.license

This file was deleted.

24 changes: 19 additions & 5 deletions docs/quick_start.rst
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,6 @@ You can choose some colors directly from the library. This can be done by import
from circuitpython_uplot.uplot import color
This will allow you to use the colors in the list as color variable definitions

* WHITE
* BLACK
* RED
Expand Down Expand Up @@ -189,7 +188,6 @@ After the initial setup we add our xy plane and show our plot
There are some parameters that you can customize:

* 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
* line color: you could specify the color in HEX
* fill: if you selected this as `True` the area under your graph will be filled
Expand Down Expand Up @@ -264,7 +262,6 @@ Creates a scatter plot with x,y data. You can customize the circle diameter if y
There are some parameters that you can customize:

* 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
* radius: circles radius/radii
* circle_color: you can specify the color in HEX
Expand All @@ -284,7 +281,7 @@ Bar Plot
===============

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

.. code-block:: python
Expand All @@ -308,6 +305,24 @@ You can select the color or and if the bars are filled
ubar(plot, a, b, 0xFF1000, True)
with the projection argument you can show the bars with projection. This will give them a 3D
appearance

.. code-block:: python
import board
from circuitpython_uplot.uplot import Uplot
from circuitpython_uplot.ubar import ubar
display = board.DISPLAY
plot = Uplot(0, 0, display.width, display.height)
a = ["a", "b", "c", "d"]
b = [3, 5, 1, 7]
ubar(plot, a, b, color=0xFF1000, fill=True, bar_space=30, xstart=70, projection=True)
===============
Fillbetween
===============
Expand Down Expand Up @@ -380,7 +395,6 @@ This is a similar to Cartesian but designed to allow the user to use it as a dat
The user needs to manually set up the range and tick values in order for this graph to work properly

There are some parameters that you can customize:

* 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
* ticksx and ticksy: Specific ticks for the X and Y axes
* line_color: you can specify the color in HEX
Expand Down
3 changes: 0 additions & 3 deletions docs/quick_start.rst.license

This file was deleted.

3 changes: 0 additions & 3 deletions docs/readme.png.license

This file was deleted.

3 changes: 0 additions & 3 deletions docs/readme2.png.license

This file was deleted.

Binary file added docs/uplot_3DBars.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 0 additions & 3 deletions docs/uplot_cartesian.gif.license

This file was deleted.

3 changes: 0 additions & 3 deletions docs/uplot_ex1.jpg.license

This file was deleted.

3 changes: 0 additions & 3 deletions docs/uplot_ex10.jpg.license

This file was deleted.

3 changes: 0 additions & 3 deletions docs/uplot_ex11.jpg.license

This file was deleted.

3 changes: 0 additions & 3 deletions docs/uplot_ex12.jpg.license

This file was deleted.

3 changes: 0 additions & 3 deletions docs/uplot_ex15.jpg.license

This file was deleted.

3 changes: 0 additions & 3 deletions docs/uplot_ex16.jpg.license

This file was deleted.

3 changes: 0 additions & 3 deletions docs/uplot_ex17.jpg.license

This file was deleted.

3 changes: 0 additions & 3 deletions docs/uplot_ex22.jpg.license

This file was deleted.

3 changes: 0 additions & 3 deletions docs/uplot_ex23.jpg.license

This file was deleted.

3 changes: 0 additions & 3 deletions docs/uplot_ex3.jpg.license

This file was deleted.

3 changes: 0 additions & 3 deletions docs/uplot_ex4.jpg.license

This file was deleted.

3 changes: 0 additions & 3 deletions docs/uplot_ex5.jpg.license

This file was deleted.

3 changes: 0 additions & 3 deletions docs/uplot_ex6.jpg.license

This file was deleted.

3 changes: 0 additions & 3 deletions docs/uplot_ex7.jpg.license

This file was deleted.

3 changes: 0 additions & 3 deletions docs/uplot_ex8.jpg.license

This file was deleted.

3 changes: 0 additions & 3 deletions docs/uplot_ex9.jpg.license

This file was deleted.

29 changes: 29 additions & 0 deletions examples/uplot_ubar_3Dbars.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# SPDX-FileCopyrightText: Copyright (c) 2023 Jose D. Montoya
#
# SPDX-License-Identifier: MIT

import time
import board
from circuitpython_uplot.uplot import Uplot
from circuitpython_uplot.ubar import ubar


# Setting up the display
display = board.DISPLAY

plot = Uplot(0, 0, display.width, display.height)

# Setting up tick parameters
plot.axs_params(axstype="box")
a = ["a", "b", "c", "d", "e"]
b = [3, 5, 1, 9, 7]

# Creating a 3D bar
ubar(plot, a, b, color=0xFF1000, fill=True, bar_space=30, xstart=70, projection=True)

# Plotting and showing the plot
display.show(plot)

# Adding some wait time
while True:
time.sleep(1)

0 comments on commit d2da1d1

Please sign in to comment.