-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHistLayout.py
185 lines (153 loc) · 6.58 KB
/
HistLayout.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
from kivy.graphics import Color, Line, Rectangle
from kivy.uix.screenmanager import Screen
from kivy.uix.label import Label
"""
https://pypi.org/project/kivy-plot/0.0.1/
https://github.com/rcnn-retall/kivy_plot
"""
class Labels(Label):
def __init__(self, **kwargs):
super(Labels, self).__init__()
for key, value in kwargs.items():
self.__setattr__(key, value)
class Polyline(Screen):
def __init__(self, **kwargs):
super().__init__()
self.size_hint = (.9, .9)
self.pos_hint = {'x': .05, 'y': .05}
self.axis = []
self.data_line = []
with self.canvas:
Color(0, 0, 0, 1)
self.axis_left = Line()
self.axis_right = Line()
self.axis_top = Line()
self.axis_bottom = Line()
self.axis.append(self.axis_left)
self.axis.append(self.axis_right)
self.axis.append(self.axis_top)
self.axis.append(self.axis_bottom)
class RctHight(Rectangle):
def __init__(self, **kwargs):
super().__init__()
for i in kwargs.items():
self.__setattr__(*i)
class HistLayout(Screen):
def __init__(self,title="text_1",back=(1,1,1,1),x_name="",y_name="",**kwargs):
super().__init__()
self.back = back
for i in kwargs.items():
self.__setattr__(*i)
self.lable_list = {}
self.titless = Label(text=title, size_hint=(.8, .05), pos_hint={"top":1, "x":.1}, color=(0,0,0,1), font_size="18sp")
self.add_widget(self.titless)
self.ploath = Polyline()
self.add_widget(self.ploath)
self.ploath.bind(size=self.update)
self.max_y = 10
self.bar_data = {}
self.y_axis = []
self.y_axis_label = []
self.bind(size=self.update_background)
# self.bind(pos=self.update)
self.y_name = Label(text=y_name, size_hint=(None, None), pos_hint={"x":0, "center_y":.5}, color=(0,0,0,1))
self.y_name.bind(texture_size=self.y_name.setter("size"))
self.add_widget(self.y_name)
self.x_name = Label(text=x_name, size_hint=(None, None), pos_hint={"center_x":.5, "y":0}, color=(0,0,0,1))
self.x_name.bind(texture_size=self.x_name.setter("size"))
self.add_widget(self.x_name)
with self.canvas.before:
Color(*self.back)
self.rects = Rectangle()
def del_axis(self, axis):
self.ploath.canvas.remove(getattr(self.ploath, axis))
def update_background(self, widget,size):
self.rects.size=widget.size
def bar(self, index, y_list, meun=False,color=(0,0,0,1)):
self.max_y = self.max_y if self.max_y > max(y_list) else max(y_list)
for i in zip(index, y_list):
rects = RctHight(name=i[0], y_list=i[1])
self.ploath.canvas.add(Color(*color))
self.ploath.canvas.add(rects)
if i[0] in self.bar_data:
self.bar_data[i[0]].append(rects)
else:
l1 = Label(text=str(i[0]), color=(0,0,0,1), size_hint=(None,None))
l1.bind(texture_size=l1.setter("size"))
self.ploath.add_widget(l1)
self.lable_list[i[0]]=l1
self.bar_data[i[0]] = []
self.bar_data[i[0]].append(rects)
[self.ploath.canvas.remove(i) for i in self.y_axis]
[self.ploath.remove_widget(i) for i in self.y_axis_label]
self.y_axis_label = []
self.y_axis = []
for i in range(0, self.max_y, int(self.max_y / 10)):
self.ploath.canvas.add(Color(0, 0, 0))
tape = RctHight(size=[10, 2])
# tape.__slots__.append("index")
tape.index = i
self.ploath.canvas.add(tape)
self.y_axis.append(tape)
lx = Labels(text=str(i), size_hint=(None, None),color=(0, 0, 0, 1))
# , pos=[-30, i * (widget.height * (1 / self.max_y))],
# color=(0, 0, 0, 1))
# lx.__slots__.append("index")
lx.index = i
lx.bind(texture_size=lx.setter("size"))
self.ploath.add_widget(lx)
self.y_axis_label.append(lx)
# print(self.bar_data)
def update(self,widget,size):
widget.axis_left.points=[0,0,0,size[1]]
widget.axis_right.points=[size[0],size[1],size[0],0]
widget.axis_top.points=[size[0],size[1],0,size[1]]
widget.axis_bottom.points=[0,0,size[0],0]
whif = widget.width /len(self.bar_data)
whifs = (widget.height /len(self.bar_data)) -(widget.height*0.05)
for pos,j in enumerate(self.bar_data):
pos = pos*whif+whif/2
# l1 = Label(text=str(j), pos=(pos,-30), color=(0,0,0,1), size_hint=(None,None))
# l1.bind(texture_size=l1.setter("size"))
# self.ploath.add_widget(l1)
self.lable_list[j].pos = pos,-30
# print(self.lable_list[j].width)
# print(self.lable_list[j].pos)
list_all = len(self.bar_data[j])
for index,i in enumerate(self.bar_data[j]):
# print(i)
# print(list_all)
# print(pos+(index*whif))
# i.pos=pos+(index*(whif+100/2)),0
# i.pos = i.size[0]
# i.pos = index*((whif/list_all) - (pos-(whif/2)))+whif/2,0
i.size = whifs/list_all, i.y_list*(widget.height*(.95/self.max_y))
i.pos =(pos-(whifs/2))+ (index*(whifs/list_all)),0
# for i in range(0, self.max_y, int(self.max_y / 10)):
# index = 0
for i,j in zip(self.y_axis, self.y_axis_label):
# self.ploath.canvas.add(Color(0, 0, 0))
i.pos=[0, i.index * (widget.height * (1 / self.max_y))]
j.pos=[-30, j.index * (widget.height * (1 / self.max_y))]
index = index + 1
#
# if __name__ == '__main__':
# body = HistLayout()
# body.bar(["iewv", "lls", "llos"], [20, 20, 420], color=(0, 1, 0, 1))
# body.bar(["iewv", "lls", "llos"], [50, 220, 420], color=(0, 0, .2, 1))
# body.bar(["iewv", "lls", "llos"], [20, 220, 210], color=(.2, 0, .2, 1))
# body.bar(["iewvs", "xx"], [280, 204], color=(1, 0, .5, 1))
# body.bar(["iewvs", "xx"], [280, 204], color=(1, .5, .5, 1))
# body.bar(["iewvsok", "235"], [150, 253])
#
# body.bar(["iewvsok", "235"], [150, 22])
#
# from kivy.app import App
# class New_App(App):
# def __init__(self):
# super().__init__()
# self.body = Screen()
# self.body.add_widget(body)
# def build(self):
# return self.body
# New_App().run()