Skip to content
This repository has been archived by the owner on Dec 19, 2024. It is now read-only.

Commit

Permalink
adding fillbetween
Browse files Browse the repository at this point in the history
  • Loading branch information
jposada202020 committed Aug 20, 2023
1 parent 22860d4 commit e5f81ef
Show file tree
Hide file tree
Showing 6 changed files with 173 additions and 1 deletion.
3 changes: 3 additions & 0 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,6 @@ MicroPython UPLOT Library

.. automodule:: micropython_uplot.map
:members:

.. automodule:: micropython_uplot.fillbetween
:members:
10 changes: 10 additions & 0 deletions docs/examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,13 @@ Map Simpletest
:caption: examples/map_simpletest.py
:lines: 5-
.. image:: ../docs/map_simpletest.jpg

Fillbetween Simpletest
----------------------------

Fillbetween Simpletest

.. literalinclude:: ../examples/fillbetween_simpletest.py
:caption: examples/fillbetween_simpletest.py
:lines: 5-
.. image:: ../docs/fillbetween.jpg
Binary file added docs/fillbetween.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
30 changes: 30 additions & 0 deletions examples/fillbetween_simpletest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# SPDX-FileCopyrightText: Copyright (c) 2023 Jose D. Montoya
#
# SPDX-License-Identifier: MIT

import gc
from machine import Pin, SPI
from ili9486 import ILI9486
from micropython_uplot.plot import PLOT
from micropython_uplot.utils import linspace
from micropython_uplot.fillbetween import Fillbetween

# Pin definition
pdc = Pin(8, Pin.OUT, value=0)
prst = Pin(15, Pin.OUT, value=1)
pcs = Pin(9, Pin.OUT, value=1)
spi = SPI(1, sck=Pin(10), mosi=Pin(11), miso=Pin(12), baudrate=30_000_000)
gc.collect()
display = ILI9486(spi, pcs, pdc, prst)

plot = PLOT(display, 5, 5, 300, 200, padding=1, box_color=(255, 255, 255))


x = list(linspace(0, 8, 25))

y1 = [value**2 / 2 for value in x]
y2 = [2 + value**2 + 3 * value for value in x]

Fillbetween(plot, x, y1, y2, fill_color=(255, 0, 0))

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

"""
`fillbetween`
================================================================================
MicroPython fillbetween graph
* Author: Jose D. Montoya
"""
try:
from typing import Optional, Union
from micropython_uplot.plot import PLOT
except ImportError:
pass

from array import array
from micropython_uplot.colors import set_color


__version__ = "0.0.0+auto.0"
__repo__ = "https://github.com/adafruit/MicroPython_UPLOT.git"


class Fillbetween:
"""
Class to draw a fillbetween graph
"""

def __init__(
self,
plot: PLOT,
x: list,
y1: list,
y2: list,
rangex: Optional[list] = None,
rangey: Optional[list] = None,
fill_color: int = (0, 255, 0),
pointer_index: Optional[int] = None,
) -> None:
"""
:param Plot plot: Plot object for the scatter to be drawn
:param list x: x points coordinates
:param list y1: y1 points coordinates
:param list y2: y2 points coordinates
:param list|None rangex: x range limits
:param list|None rangey: y range limits
:param int fill_color: filling color. Defaults to (0, 255, 0)
:param int|None pointer_index: Pointer index. Defaults to None
"""

if pointer_index is None:
self._pointer_index = plot._pointer_index
else:
self._pointer_index = pointer_index

self._line_color = set_color(
plot._display,
self._pointer_index,
fill_color[0],
fill_color[1],
fill_color[2],
)
plot._pointer_index += 1

points = []

max_x = max(x)
min_x = min(x)
max_y = max(max(y2), max(y1))
min_y = min(min(y1), min(y2))

if rangex is None:
self.xmin = min_x - (abs(max_x - min_x) / 10)
self.xmax = max_x + (abs(max_x - min_x) / 10)

else:
self.xmin = min(rangex)
self.xmax = max(rangex)

if rangey is None:
self.ymin = min_y - (abs(max_y - min_y) / 10)
self.ymax = max_y + (abs(max_y - min_y) / 10)
else:
self.ymin = min(rangey)
self.ymax = max(rangey)

xnorm = [
int(plot.transform(self.xmin, self.xmax, plot._newxmin, plot._newxmax, _))
for _ in x
]
xrev = [
int(plot.transform(self.xmin, self.xmax, plot._newxmin, plot._newxmax, _))
for _ in x
]

xrev.reverse()

y1norm = tuple(
[
int(
plot.transform(
self.ymin, self.ymax, plot._newymin, plot._newymax, _
)
)
for _ in y1
]
)
y2norm = [
int(plot.transform(self.ymin, self.ymax, plot._newymin, plot._newymax, _))
for _ in y2
]
y2norm.reverse()

flip2y = y2norm

for index, item in enumerate(xnorm):
points.extend([item, y1norm[index]])
for index, item in enumerate(xrev):
points.extend([item, flip2y[index]])

array_points = array("i", points)
plot._display.poly(0, 0, array_points, self._line_color, True)
2 changes: 1 addition & 1 deletion micropython_uplot/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ def _savingppm(
self._color0,
self._color2,
self._color1,
self._color0,
(255, 0, 0),
(237, 0, 86),
(218, 0, 105),
(199, 0, 124),
Expand Down

0 comments on commit e5f81ef

Please sign in to comment.