Skip to content

Commit 803f933

Browse files
committed
fixing value_max scale
1 parent f1818c9 commit 803f933

File tree

1 file changed

+18
-18
lines changed

1 file changed

+18
-18
lines changed

circuitpython_uplot/ubar.py

+18-18
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from circuitpython_uplot.uplot import Uplot
1919
except ImportError:
2020
pass
21-
import math
21+
from math import sin, cos, ceil
2222
from displayio import Palette
2323
from bitmaptools import draw_line
2424
from vectorio import Rectangle, Polygon
@@ -76,13 +76,12 @@ def __init__(
7676
y_max = max_value
7777

7878
self._y = [i * plot.scale for i in y]
79+
7980
self._bar_space = int(bar_space / plot.scale)
80-
self._graphx = plot.scale * int(
81+
self._graphx = plot.scale * ceil(
8182
abs(plot._newxmax - plot._newxmin) / (len(x) + 4)
8283
)
83-
self._graphy = plot.scale * int(
84-
abs(plot._newymax - plot._newymin) / (y_max + 2)
85-
)
84+
self._graphy = plot.scale * (abs(plot._newymax - plot._newymin) / (y_max + 2))
8685

8786
self._new_min = int(plot.transform(0, y_max, y_max, 0, 0))
8887
self._new_max = int(plot.transform(0, y_max, y_max, 0, y_max))
@@ -131,16 +130,16 @@ def __init__(
131130
xstart + (i * self._graphx),
132131
plot._newymin,
133132
self._graphx,
134-
self._graphy * y[i],
133+
ceil(self._graphy * y[i]),
135134
plot._index_colorused,
136135
)
137136
delta = 20
138-
rx = int(delta * math.cos(-0.5))
139-
ry = int(delta * math.sin(-0.5))
137+
rx = int(delta * cos(-0.5))
138+
ry = int(delta * sin(-0.5))
140139
x0 = xstart + (i * self._graphx)
141-
y0 = plot._newymin - self._graphy * y[i]
140+
y0 = ceil(plot._newymin - self._graphy * y[i])
142141
x1 = xstart + (i * self._graphx) + self._graphx
143-
y1 = plot._newymin - self._graphy * y[i]
142+
y1 = ceil(plot._newymin - self._graphy * y[i])
144143

145144
draw_line(
146145
plot._plotbitmap, x0, y0, x0 - rx, y0 + ry, plot._index_colorused
@@ -182,7 +181,7 @@ def _create_bars(self, xstart: int, indice: int):
182181
Rectangle(
183182
pixel_shader=self._color_palette,
184183
width=self._graphx,
185-
height=self._graphy * self._y[indice],
184+
height=ceil(self._graphy * self._y[indice]),
186185
x=xstart + (indice * self._graphx),
187186
y=int(
188187
self._plot_obj._newymin
@@ -195,8 +194,8 @@ def _create_bars(self, xstart: int, indice: int):
195194

196195
def _create_projections(self, xstart: int, indice: int, color_lenght: int):
197196
delta = 20
198-
rx = int(delta * math.cos(-0.5))
199-
ry = int(delta * math.sin(-0.5))
197+
rx = int(delta * cos(-0.5))
198+
ry = int(delta * sin(-0.5))
200199
points = [
201200
(0, 0),
202201
(self._graphx, 0),
@@ -213,22 +212,22 @@ def _create_projections(self, xstart: int, indice: int, color_lenght: int):
213212
pixel_shader=self._color_palette,
214213
points=points,
215214
x=xstart + (indice * self._graphx),
216-
y=self._plot_obj._newymin - self._graphy * self._y[indice],
215+
y=ceil(self._plot_obj._newymin - self._graphy * self._y[indice]),
217216
color_index=self._color_index + color_lenght,
218217
)
219218
)
220219
points = [
221220
(0, 0),
222221
(0 - rx, 0 + ry),
223-
(0 - rx, self._graphy * self._y[indice]),
224-
(0, self._graphy * self._y[indice]),
222+
(0 - rx, ceil(self._graphy * self._y[indice])),
223+
(0, ceil(self._graphy * self._y[indice])),
225224
]
226225
self._plot_obj.append(
227226
Polygon(
228227
pixel_shader=self._color_palette,
229228
points=points,
230229
x=xstart + (indice * self._graphx),
231-
y=self._plot_obj._newymin - self._graphy * self._y[indice],
230+
y=ceil(self._plot_obj._newymin - self._graphy * self._y[indice]),
232231
color_index=self._color_index + color_lenght,
233232
)
234233
)
@@ -239,6 +238,7 @@ def _draw_rectangle(
239238
"""
240239
Helper function to draw bins rectangles
241240
"""
241+
242242
draw_line(plot._plotbitmap, x, y, x + width, y, color)
243243
draw_line(plot._plotbitmap, x, y, x, y - height, color)
244244
draw_line(plot._plotbitmap, x + width, y, x + width, y - height, color)
@@ -255,7 +255,7 @@ def update_values(self, values: list = None):
255255
if values is None:
256256
raise ValueError("You must provide a list of values")
257257
for i, element in enumerate(self._bars):
258-
height = self._graphy * values[i]
258+
height = ceil(self._graphy * values[i])
259259
y = int(
260260
self._plot_obj._newymin
261261
- self._graphy * values[i] / self._plot_obj.scale

0 commit comments

Comments
 (0)