19
19
except ImportError :
20
20
pass
21
21
import math
22
+ from displayio import Palette
22
23
from bitmaptools import draw_line
23
24
from vectorio import Rectangle , Polygon
24
25
@@ -43,6 +44,7 @@ def __init__(
43
44
bar_space = 16 ,
44
45
xstart = 50 ,
45
46
projection = False ,
47
+ color_palette = None ,
46
48
) -> None :
47
49
"""
48
50
:param Uplot plot: Plot object for the scatter to be drawn
@@ -51,8 +53,11 @@ def __init__(
51
53
:param int color: boxes color. Defaults to const:``0xFFFFFF``
52
54
:param bool fill: boxes fill attribute. Defaults to `False`
53
55
:param int bar_space: space in pixels between the bars
54
- :param int xstart: start point in the x axis for the bar to start. Default to :const:`50`
56
+ :param int xstart: start point in the x axis for the bar to start. Defaults to :const:`50`
55
57
:param bool projection: creates projection of the bars given them depth.
58
+ :param list color_palette: list of colors to be used for the bars. Defaults to None.
59
+ Be aware that you need to include the same number if colors as your data.
60
+ This functionality will only work with filled bars.
56
61
57
62
"""
58
63
y = [i * plot .scale for i in y ]
@@ -66,7 +71,19 @@ def __init__(
66
71
self ._new_min = int (plot .transform (0 , max (y ), max (y ), 0 , 0 ))
67
72
self ._new_max = int (plot .transform (0 , max (y ), max (y ), 0 , max (y )))
68
73
69
- plot ._plot_palette [plot ._index_colorused ] = color
74
+ if color_palette is not None :
75
+ if projection :
76
+ color_count = 2
77
+ else :
78
+ color_count = 1
79
+ self ._color_palette = Palette (len (color_palette ) * color_count )
80
+ for i , selected_color in enumerate (color_palette ):
81
+ self ._color_palette [i ] = selected_color
82
+ self ._color_index = 0
83
+ else :
84
+ self ._color_palette = plot ._plot_palette
85
+ self ._color_index = plot ._index_colorused
86
+ self ._color_palette [self ._color_index ] = color
70
87
71
88
if plot ._index_colorused >= 14 :
72
89
plot ._index_colorused = 0
@@ -75,12 +92,12 @@ def __init__(
75
92
for i , _ in enumerate (x ):
76
93
plot .append (
77
94
Rectangle (
78
- pixel_shader = plot . _plot_palette ,
95
+ pixel_shader = self . _color_palette ,
79
96
width = self ._graphx ,
80
97
height = self ._graphy * y [i ],
81
98
x = xstart + (i * self ._graphx ),
82
99
y = int (plot ._newymin - self ._graphy * y [i ] / plot .scale ),
83
- color_index = plot . _index_colorused ,
100
+ color_index = self . _color_index ,
84
101
)
85
102
)
86
103
if projection :
@@ -93,16 +110,16 @@ def __init__(
93
110
(self ._graphx - rx , 0 + ry ),
94
111
(0 - rx , 0 + ry ),
95
112
]
96
- plot . _plot_palette [ plot . _index_colorused + 6 ] = color_fader (
97
- plot . _plot_palette [ plot . _index_colorused ], 0.7 , 1
98
- )
113
+ self . _color_palette [
114
+ self . _color_index + len ( color_palette )
115
+ ] = color_fader ( self . _color_palette [ self . _color_index ], 0.7 , 1 )
99
116
plot .append (
100
117
Polygon (
101
- pixel_shader = plot . _plot_palette ,
118
+ pixel_shader = self . _color_palette ,
102
119
points = points ,
103
120
x = xstart + (i * self ._graphx ),
104
121
y = plot ._newymin - self ._graphy * y [i ],
105
- color_index = plot . _index_colorused + 6 ,
122
+ color_index = self . _color_index + len ( color_palette ) ,
106
123
)
107
124
)
108
125
points = [
@@ -113,11 +130,11 @@ def __init__(
113
130
]
114
131
plot .append (
115
132
Polygon (
116
- pixel_shader = plot . _plot_palette ,
133
+ pixel_shader = self . _color_palette ,
117
134
points = points ,
118
135
x = xstart + (i * self ._graphx ),
119
136
y = plot ._newymin - self ._graphy * y [i ],
120
- color_index = plot . _index_colorused + 6 ,
137
+ color_index = self . _color_index + len ( color_palette ) ,
121
138
)
122
139
)
123
140
@@ -128,6 +145,7 @@ def __init__(
128
145
)
129
146
xstart = xstart + self ._bar_space
130
147
plot ._index_colorused = plot ._index_colorused + 1
148
+ self ._color_index = self ._color_index + 1
131
149
else :
132
150
for i , _ in enumerate (x ):
133
151
self ._draw_rectangle (
0 commit comments