-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathimages.py
70 lines (50 loc) · 1.93 KB
/
images.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
from io import BytesIO
from matplotlib.figure import Figure
from matplotlib.ticker import FormatStrFormatter
from matplotlib.backends.backend_agg import FigureCanvasAgg
DEF_CHART_W = 600
DEF_CHART_H = 500
MIN_CHART_W = 400
MIN_CHART_H = 300
def gen_image_from_results(result_data, width, height):
canvas_width = width if width is not None else DEF_CHART_W
canvas_height = height if height is not None else DEF_CHART_H
canvas_width = max(canvas_width, MIN_CHART_W)
canvas_height = max(canvas_height, MIN_CHART_H)
values = [element.value for element in result_data['results']]
max_value = max(values)
min_value = min(values)
value_range = max_value - min_value
range_increment = 0.05 * abs(value_range)
fig = Figure(figsize=(canvas_width / 100, canvas_height / 100), dpi=100)
ax = fig.add_axes([.1, .15, .85, .75])
ax.set_ylim(min_value - range_increment, max_value + range_increment)
xax = range(0, len(values))
yax = values
ax.set_xticks(xax)
ax.set_xticklabels([element.date.strftime('%d %b') for element in
result_data['results']], rotation=75)
ax.set_title(result_data['benchmark'].name)
if result_data['relative']:
ax.yaxis.set_major_formatter(FormatStrFormatter('%.2f%%'))
font_sizes = [16, 16]
dimensions = [canvas_width, canvas_height]
for idx, value in enumerate(dimensions):
if value < 500:
font_sizes[idx] = 8
elif value < 1000:
font_sizes[idx] = 12
if result_data['relative']:
font_sizes[0] -= 2
for item in ax.get_yticklabels():
item.set_fontsize(font_sizes[0])
for item in ax.get_xticklabels():
item.set_fontsize(font_sizes[1])
ax.title.set_fontsize(font_sizes[1] + 4)
ax.scatter(xax, yax)
ax.plot(xax, yax)
canvas = FigureCanvasAgg(fig)
buf = BytesIO()
canvas.print_png(buf)
buf_data = buf.getvalue()
return buf_data