blob: 549b8209c8092697678d8a55011e2876ff9dbed5 [file] [log] [blame]
Yulia Portnovae3a49982015-02-16 16:06:09 +02001import hashlib
2import os
3import threading
4
Yulia Portnova6d72d7f2015-02-04 16:48:50 +02005from GChartWrapper import VerticalBarGroup
6from GChartWrapper import Line
7from GChartWrapper import constants
8
Yulia Portnova407ca952015-04-10 10:38:15 +03009from config import cfg_dict
Yulia Portnovae3a49982015-02-16 16:06:09 +020010
Yulia Portnova6d72d7f2015-02-04 16:48:50 +020011
Yulia Portnova407ca952015-04-10 10:38:15 +030012COLORS = ["1569C7", "81D8D0", "307D7E", "5CB3FF", "0040FF", "81DAF5"]
Yulia Portnova6d72d7f2015-02-04 16:48:50 +020013constants.MARKERS += 'E' # append E marker to available markers
14
15
Yulia Portnovae3a49982015-02-16 16:06:09 +020016def save_image(chart, img_path):
17 t = threading.Thread(target=chart.save, kwargs={'fname': img_path})
18 t.daemon = True
19 t.start()
20
21
Yulia Portnova8ca20572015-04-14 14:09:39 +030022def render_vertical_bar(title, legend, bars_data, bars_dev_top,
23 bars_dev_bottom,
24 width=700, height=400,
Yulia Portnova407ca952015-04-10 10:38:15 +030025 scale_x=None, scale_y=None, label_x=None,
26 label_y=None, lines=()):
Yulia Portnova6d72d7f2015-02-04 16:48:50 +020027 """
28 Renders vertical bar group chart
29
30 :param legend - list of legend values.
31 Example: ['bar1', 'bar2', 'bar3']
32 :param dataset - list of values for each type (value, deviation)
33 Example:
34 [
Yulia Portnova407ca952015-04-10 10:38:15 +030035 [(10,1), (11, 2), (10,1)], # bar1 values
Yulia Portnova6d72d7f2015-02-04 16:48:50 +020036 [(30,(29,33)),(35,(33,36)), (30,(29,33))], # bar2 values
37 [(20,(19,21)),(20,(13, 24)), (20,(19,21))] # bar 3 values
38 ]
39 :param width - width of chart
40 :param height - height of chart
41 :param scale_x - x ace scale
42 :param scale_y - y ace scale
43
44 :returns url to chart
45
46 dataset example:
47 {
48 'relese_1': {
49 'randr': (1, 0.1),
50 'randwr': (2, 0.2)
51 }
52 'release_2': {
53 'randr': (3, 0.3),
54 'randwr': (4, 0.4)
55 }
56 }
57 """
koder aka kdanilov66839a92015-04-11 13:22:31 +030058
Yulia Portnova6d72d7f2015-02-04 16:48:50 +020059 bar = VerticalBarGroup([], encoding='text')
60 bar.title(title)
61
Yulia Portnova8ca20572015-04-14 14:09:39 +030062 dataset = bars_data + bars_dev_top + bars_dev_bottom + [lines[0][0]]
Yulia Portnova6d72d7f2015-02-04 16:48:50 +020063
Yulia Portnova8ca20572015-04-14 14:09:39 +030064 bar.dataset(dataset, series=len(bars_data))
Yulia Portnova0d2bd0a2015-02-11 17:42:44 +020065 bar.axes.type('xyy')
Yulia Portnova407ca952015-04-10 10:38:15 +030066 bar.axes.label(2, None, label_x)
Yulia Portnova6d72d7f2015-02-04 16:48:50 +020067 if scale_x:
68 bar.axes.label(0, *scale_x)
Yulia Portnova6d72d7f2015-02-04 16:48:50 +020069
Yulia Portnova8ca20572015-04-14 14:09:39 +030070 max_value = (max([max(l) for l in dataset[:2]]))
Yulia Portnova919f3be2015-02-06 12:49:22 +020071 bar.axes.range(1, 0, max_value)
Yulia Portnova0d2bd0a2015-02-11 17:42:44 +020072 bar.axes.style(1, 'N*s*')
Yulia Portnovaffec7602015-02-12 11:16:11 +020073 bar.axes.style(2, '000000', '13')
Yulia Portnova0d2bd0a2015-02-11 17:42:44 +020074
Yulia Portnova8ca20572015-04-14 14:09:39 +030075 bar.scale(*[0, max_value] * 3)
Yulia Portnova0d2bd0a2015-02-11 17:42:44 +020076
Yulia Portnova6d72d7f2015-02-04 16:48:50 +020077 bar.bar('r', '.1', '1')
Yulia Portnova407ca952015-04-10 10:38:15 +030078 for i in range(1):
Yulia Portnova8ca20572015-04-14 14:09:39 +030079 bar.marker('E', '000000', '%s:%s' % ((len(bars_data) + i*2), i),
Yulia Portnova6d72d7f2015-02-04 16:48:50 +020080 '', '1:10')
Yulia Portnova407ca952015-04-10 10:38:15 +030081 bar.color(*COLORS)
Yulia Portnova6d72d7f2015-02-04 16:48:50 +020082 bar.size(width, height)
83
Yulia Portnova407ca952015-04-10 10:38:15 +030084 axes_type = "xyy"
85
Yulia Portnova8ca20572015-04-14 14:09:39 +030086 scale = [0, max_value] * len(bars_dev_top + bars_dev_bottom + bars_data)
Yulia Portnova407ca952015-04-10 10:38:15 +030087 if lines:
88 line_n = 0
89 for data, label, axe, leg in lines:
Yulia Portnova8ca20572015-04-14 14:09:39 +030090 bar.marker('D', COLORS[len(bars_data) + line_n],
91 (len(bars_data + bars_dev_top + bars_dev_bottom))
92 + line_n, 0, 3)
Yulia Portnova407ca952015-04-10 10:38:15 +030093 max_val_l = max(data)
94 if axe:
95 bar.axes.type(axes_type + axe)
96 bar.axes.range(len(axes_type), 0, max_val_l)
97 bar.axes.style(len(axes_type), 'N*s*')
98 bar.axes.label(len(axes_type) + 1, None, label)
99 bar.axes.style(len(axes_type) + 1, '000000', '13')
100 axes_type += axe
101 line_n += 1
102 legend.append(leg)
103 scale += [0, max_val_l]
104
105 bar.legend(*legend)
106 bar.scale(*scale)
Yulia Portnovae3a49982015-02-16 16:06:09 +0200107 img_name = hashlib.md5(str(bar)).hexdigest() + ".png"
koder aka kdanilov66839a92015-04-11 13:22:31 +0300108 img_path = os.path.join(cfg_dict['charts_img_path'], img_name)
Yulia Portnovae3a49982015-02-16 16:06:09 +0200109 if not os.path.exists(img_path):
110 save_image(bar, img_path)
111 return str(bar)
112 return img_path
Yulia Portnova6d72d7f2015-02-04 16:48:50 +0200113
114
Yulia Portnova919f3be2015-02-06 12:49:22 +0200115def render_lines(title, legend, dataset, scale_x, width=700, height=400):
116 line = Line([], encoding="text")
117 line.title(title)
118 line.dataset(dataset)
119
120 line.axes('xy')
121 max_value = (max([max(l) for l in dataset]))
122 line.axes.range(1, 0, max_value)
123 line.scale(0, max_value)
124 line.axes.label(0, *scale_x)
125 line.legend(*legend)
126 line.color(*COLORS[:len(legend)])
127 line.size(width, height)
Yulia Portnovae3a49982015-02-16 16:06:09 +0200128
Yulia Portnovae3a49982015-02-16 16:06:09 +0200129 img_name = hashlib.md5(str(line)).hexdigest() + ".png"
koder aka kdanilov66839a92015-04-11 13:22:31 +0300130 img_path = os.path.join(cfg_dict['charts_img_path'], img_name)
Yulia Portnovae3a49982015-02-16 16:06:09 +0200131 if not os.path.exists(img_path):
132 save_image(line, img_path)
133 return str(line)
134 return img_path