-
Notifications
You must be signed in to change notification settings - Fork 0
/
draw.py
197 lines (155 loc) · 6.46 KB
/
draw.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
185
186
187
188
189
190
191
192
193
194
195
196
197
# coding=utf-8
import typing
from PIL import Image, ImageDraw, ImageFont
import math
import transaction as journal
# Styles
import datetime
fontHead = ImageFont.truetype("assets/georgia-bold.ttf", 18)
font = ImageFont.truetype("assets/georgia.ttf", 14)
tag_margin = 5
tag_width = 30
pad = 5
background_color = '#EBFFE6'
class Tag:
name = 'default'
balance = 0
def __init__(self, name, balance):
self.name = name
self.balance = balance
def convert_tags(tags: typing.Dict[str, float], add_positive=True):
res = []
print(tags)
for text, value in tags.items():
if 'Доход' in text:
text = 'Доход'
if add_positive:
res.append(Tag(text, -value))
else:
if value > 0:
res.append(Tag(text, -value))
res.sort(key=lambda m: abs(m.balance), reverse=True)
return res
def drawLines(filename, time, tags):
tags = convert_tags(tags)
img_w = 310
img_h = 160 + 30 * len(tags)
image = Image.new("RGB", (img_w, img_h), '#F3FFF0')
green = '#BFFE9E'
red = '#FE9EB5'
draw = ImageDraw.Draw(image)
# Receiving tags
# time = "March"
#
# tags = [Tag(u'Еда', TagType.credit, -230), Tag(u'Транспорт', TagType.credit, -100), Tag(u'Зарплата', TagType.debit, 500) ]
#
# tags.sort(key=lambda t: abs(t.balance), reverse=True)
total_amount = sum(abs(t.balance) for t in tags)
total_balance = sum(t.balance for t in tags)
days = datetime.date.today().day
# Header
draw.text((pad, pad), time, fill='black', font=fontHead)
str_total = str(total_balance)
# if total_amount > 0:
# str_total = '+' + str_total
# else:
# str_total = '-' + str_total
total_balance_size = fontHead.getsize(str_total)
draw.text((img_w - pad - total_balance_size[0], pad), str_total, fill='black', font=fontHead)
# Drawing tags
y_pos = 10 + fontHead.getsize(time)[1] + tag_margin
draw.line([(pad, y_pos), (img_w - pad, y_pos)], fill='black', width=1)
# draw total balance
y_pos += pad
debit_amount = sum(abs(t.balance) for t in tags if t.balance > 0)
credit_amount = total_amount - debit_amount
debit_procent = float(debit_amount) / total_amount
debit_w = (img_w - 2 * pad) * debit_procent
draw.rectangle([(pad, y_pos), (debit_w + pad, y_pos + tag_width)], fill=green)
draw.rectangle([(debit_w + pad, y_pos), (img_w - pad, y_pos + tag_width)], fill=red)
debit_text = '+' + str(debit_amount)
credit_text = str(credit_amount)
debit_size = font.getsize(debit_text)
credit_size = font.getsize(credit_text)
draw.rectangle([(pad, y_pos + pad), (pad + debit_size[0], y_pos + tag_width - pad)], fill='black')
draw.rectangle([(img_w - pad - credit_size[0], y_pos + pad), (img_w - pad, y_pos + tag_width - pad)], fill='black')
draw.text((pad, y_pos + pad), debit_text, font=font)
draw.text((img_w - pad - credit_size[0], y_pos + pad), credit_text, font=font)
y_pos += pad + tag_width
draw.line([(pad, y_pos), (img_w - pad, y_pos)], fill='black', width=1)
y_pos += tag_margin
m = 1
for idx, tag in enumerate(tags):
if idx == 0:
m = (img_w / 100) / (float(abs(tag.balance)) / total_amount)
if tag.balance == 0:
continue
fill = green if tag.balance > 0 else red
draw.rectangle([(pad, y_pos), (float(abs(tag.balance)) / total_amount * 100 * m, y_pos + tag_width)], fill=fill)
draw.text((pad * 3, pad + y_pos), str(tag.balance), fill='black', font=font)
size_name = font.getsize(tag.name)
size_block_name = (size_name[0] + 2 * pad, size_name[1])
x_pos_name = (img_w - (size_block_name[0] + pad))
y_pos_name = y_pos + ((tag_width - size_block_name[1]) / 2)
draw.rectangle([(x_pos_name, y_pos_name), (img_w - pad, y_pos_name + size_block_name[1] + 3)], fill='black')
draw.text((x_pos_name + pad, y_pos_name), tag.name, fill='white', font=font)
y_pos += tag_margin + tag_width
draw.line((pad, y_pos, img_w - pad, y_pos), fill='black')
y_pos += pad
average_debit = debit_amount / days
average_credit = credit_amount / days
draw.text((pad, y_pos), u'Средний доход в день: ' + f"{average_debit:0.2f}", font=font, fill='black')
y_pos += pad * 3
draw.text((pad, y_pos), u'Средний расход в день: ' + f"{average_credit:0.2f}", font=font, fill='black')
del draw
image.save(filename, "PNG")
# Draw Circle Total
def drawCircle(filename, time, tags):
tags = convert_tags(tags, False)
img_w = 310
img_h = 300 + 25 * len(tags)
image = Image.new("RGB", (img_w, img_h), '#F3FFF0')
draw = ImageDraw.Draw(image)
draw.text(((img_w - fontHead.getsize(time)[0]) / 2, pad), time, fill='black', font=fontHead)
draw.line([(pad, pad + 20), (img_w - pad, pad + 20)], fill='black', width=1)
r = 100
colors = ['#BFFE9E', '#FEFE9E', '#FFDFA6', '#FEBDA3', '#FE9EB5', '#E49EFE', '#C3B0FF', '#9E9FFE', '#A1CCFF',
'#9FE5FE', '#9EFECE']
# random.shuffle(colors)
x_pos = img_w / 2
y_pos = x_pos
total_amount = sum(abs(t.balance) for t in tags)
tag_gen = (float(abs(t.balance)) / total_amount * 100 for t in tags)
colors = (clr for clr in colors)
value = next(tag_gen)
color = next(colors)
pog = 1.0 / 3.6 # procents on gradus
used_colors = []
def drange(start, stop, step):
q = start
while q < stop:
yield q
q += step
if tags:
used_colors.append(color)
for i in drange(0, 360, 0.1):
i *= 0.0174533 # grad to rad
if value <= 0:
value = next(tag_gen)
color = next(colors)
used_colors.append(color)
draw.line([(x_pos, y_pos), (x_pos + r * math.cos(i), y_pos + r * math.sin(i))], fill=color, width=2)
value -= pog / 10
# draw legend
y_pos += r + pad
legend = zip(used_colors, tags)
tag_gen = (float(abs(t.balance)) / total_amount * 100 for t in tags)
for l in legend:
draw.rectangle([(pad, y_pos), (pad + 45, y_pos + tag_width / 2)], fill=l[0])
text = l[1].name + ' - ' + str(abs(l[1].balance))
draw.text((2 * pad + 45, y_pos), text, fill='black', font=font)
text = '{0:.1f}'.format(next(tag_gen))
draw.text((pad + (45 - font.getsize(text)[0]) / 2, y_pos), text, fill='black', font=font)
y_pos += tag_width
del draw
image.save(filename, "PNG")