13
13
14
14
15
15
"""
16
+
16
17
try :
17
18
from circuitpython_uplot .uplot import Uplot
18
19
except ImportError :
19
20
pass
21
+ import math
20
22
from bitmaptools import draw_line
21
- from vectorio import Rectangle
23
+ from vectorio import Rectangle , Polygon
22
24
23
25
__version__ = "0.0.0+auto.0"
24
26
__repo__ = "https://github.com/adafruit/CircuitPython_uplot.git"
25
27
26
28
# pylint: disable=too-many-arguments, invalid-name, protected-access
27
- # pylint: disable=too-few-public-methods, no-self-use
29
+ # pylint: disable=too-few-public-methods, no-self-use, too-many-locals
28
30
class ubar :
29
31
"""
30
32
Main class to display different graphics
31
33
"""
32
34
33
35
def __init__ (
34
- self , plot : Uplot , x : list , y : list , color : int = 0xFFFFFF , fill : bool = False
36
+ self ,
37
+ plot : Uplot ,
38
+ x : list ,
39
+ y : list ,
40
+ color : int = 0xFFFFFF ,
41
+ fill : bool = False ,
42
+ bar_space = 16 ,
43
+ xstart = 50 ,
35
44
) -> None :
36
45
"""
37
46
:param Uplot plot: Plot object for the scatter to be drawn
38
47
:param list x: x data
39
48
:param list y: y data
40
49
:param int color: boxes color. Defaults to const:``0xFFFFFF``
41
50
:param bool fill: boxes fill attribute. Defaults to `False`
51
+ :param int bar_space: space in pixels between the bars
52
+ :param int xstart: start point in the x axis for the bar to start. Default to :const:`50`
42
53
43
54
"""
44
-
45
- self ._graphx = abs (plot ._newxmax - plot ._newxmin ) // (len (x ) + 2 )
55
+ self . _bar_space = bar_space
56
+ self ._graphx = abs (plot ._newxmax - plot ._newxmin ) // (len (x ) + 4 )
46
57
self ._graphy = abs (plot ._newymax - plot ._newymin ) // (max (y ) + 2 )
47
58
self ._new_min = int (plot .transform (0 , max (y ), max (y ), 0 , 0 ))
48
59
self ._new_max = int (plot .transform (0 , max (y ), max (y ), 0 , max (y )))
49
60
50
- bar_space = max (2 , plot ._width // 30 )
51
- xstart = self ._graphx + plot ._newxmin
52
-
53
61
plot ._plot_palette [plot ._index_colorused ] = color
54
62
55
63
if fill :
@@ -64,15 +72,50 @@ def __init__(
64
72
color_index = plot ._index_colorused ,
65
73
)
66
74
)
75
+
76
+ delta = 20
77
+ rx = int (delta * math .cos (- 0.5 ))
78
+ ry = int (delta * math .sin (- 0.5 ))
79
+ points = [
80
+ (0 , 0 ),
81
+ (self ._graphx , 0 ),
82
+ (self ._graphx - rx , 0 + ry ),
83
+ (0 - rx , 0 + ry ),
84
+ ]
85
+
86
+ plot .append (
87
+ Polygon (
88
+ pixel_shader = plot ._plot_palette ,
89
+ points = points ,
90
+ x = xstart + (i * self ._graphx ),
91
+ y = plot ._newymin - self ._graphy * y [i ],
92
+ color_index = plot ._index_colorused - 1 ,
93
+ )
94
+ )
95
+ points = [
96
+ (0 , 0 ),
97
+ (0 - rx , 0 + ry ),
98
+ (0 - rx , self ._graphy * y [i ]),
99
+ (0 , self ._graphy * y [i ]),
100
+ ]
101
+ plot .append (
102
+ Polygon (
103
+ pixel_shader = plot ._plot_palette ,
104
+ points = points ,
105
+ x = xstart + (i * self ._graphx ),
106
+ y = plot ._newymin - self ._graphy * y [i ],
107
+ color_index = plot ._index_colorused + 1 ,
108
+ )
109
+ )
110
+
67
111
plot .show_text (
68
112
str (y [i ]),
69
113
xstart + (i * self ._graphx ) + self ._graphx // 2 ,
70
114
plot ._newymin ,
71
115
)
72
- xstart = xstart + bar_space
116
+ xstart = xstart + self . _bar_space
73
117
plot ._index_colorused = plot ._index_colorused + 1
74
118
else :
75
-
76
119
for i , _ in enumerate (x ):
77
120
self ._draw_rectangle (
78
121
plot ,
@@ -82,6 +125,37 @@ def __init__(
82
125
self ._graphy * y [i ],
83
126
plot ._index_colorused ,
84
127
)
128
+ delta = 20
129
+ rx = int (delta * math .cos (- 0.5 ))
130
+ ry = int (delta * math .sin (- 0.5 ))
131
+ x0 = xstart + (i * self ._graphx )
132
+ y0 = plot ._newymin - self ._graphy * y [i ]
133
+ x1 = xstart + (i * self ._graphx ) + self ._graphx
134
+ y1 = plot ._newymin - self ._graphy * y [i ]
135
+
136
+ draw_line (
137
+ plot ._plotbitmap , x0 , y0 , x0 - rx , y0 + ry , plot ._index_colorused
138
+ )
139
+ draw_line (
140
+ plot ._plotbitmap , x1 , y1 , x1 - rx , y1 + ry , plot ._index_colorused
141
+ )
142
+ draw_line (
143
+ plot ._plotbitmap ,
144
+ x0 - rx ,
145
+ y0 + ry ,
146
+ x1 - rx ,
147
+ y1 + ry ,
148
+ plot ._index_colorused ,
149
+ )
150
+ draw_line (
151
+ plot ._plotbitmap ,
152
+ x0 - rx ,
153
+ y0 + ry ,
154
+ x0 - rx ,
155
+ plot ._newymin ,
156
+ plot ._index_colorused ,
157
+ )
158
+
85
159
xstart = xstart + bar_space
86
160
plot ._index_colorused = plot ._index_colorused + 1
87
161
plot .show_text (
0 commit comments