blob: f98552604ca0b70e725bd92148fca55a64228cb5 [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 Portnova407ca952015-04-10 10:38:15 +030022def render_vertical_bar(title, legend, dataset, width=700, height=400,
23 scale_x=None, scale_y=None, label_x=None,
24 label_y=None, lines=()):
Yulia Portnova6d72d7f2015-02-04 16:48:50 +020025 """
26 Renders vertical bar group chart
27
28 :param legend - list of legend values.
29 Example: ['bar1', 'bar2', 'bar3']
30 :param dataset - list of values for each type (value, deviation)
31 Example:
32 [
Yulia Portnova407ca952015-04-10 10:38:15 +030033 [(10,1), (11, 2), (10,1)], # bar1 values
Yulia Portnova6d72d7f2015-02-04 16:48:50 +020034 [(30,(29,33)),(35,(33,36)), (30,(29,33))], # bar2 values
35 [(20,(19,21)),(20,(13, 24)), (20,(19,21))] # bar 3 values
36 ]
37 :param width - width of chart
38 :param height - height of chart
39 :param scale_x - x ace scale
40 :param scale_y - y ace scale
41
42 :returns url to chart
43
44 dataset example:
45 {
46 'relese_1': {
47 'randr': (1, 0.1),
48 'randwr': (2, 0.2)
49 }
50 'release_2': {
51 'randr': (3, 0.3),
52 'randwr': (4, 0.4)
53 }
54 }
55 """
koder aka kdanilov66839a92015-04-11 13:22:31 +030056
Yulia Portnova6d72d7f2015-02-04 16:48:50 +020057 bar = VerticalBarGroup([], encoding='text')
58 bar.title(title)
59
60 values = []
61 deviations = []
62
63 for d in dataset:
64 val, dev = zip(*d)
65
66 display_dev = []
67 for i in range(len(val)):
68 display_dev.append((val[i]-dev[i], val[i]+dev[i]))
69 values.append(val)
70 # deviations.extend(zip(*dev))
71 deviations.extend(zip(*display_dev))
72
Yulia Portnova407ca952015-04-10 10:38:15 +030073 # bar.dataset(values + deviations, series=len(values))
koder aka kdanilov66839a92015-04-11 13:22:31 +030074 bar.dataset(values + deviations + [l[0] for l in lines],
75 series=len(values))
Yulia Portnova0d2bd0a2015-02-11 17:42:44 +020076 bar.axes.type('xyy')
Yulia Portnova407ca952015-04-10 10:38:15 +030077 bar.axes.label(2, None, label_x)
Yulia Portnova6d72d7f2015-02-04 16:48:50 +020078 if scale_x:
79 bar.axes.label(0, *scale_x)
Yulia Portnova6d72d7f2015-02-04 16:48:50 +020080
Yulia Portnova407ca952015-04-10 10:38:15 +030081 max_value = (max([max(l) for l in values + deviations + [lines[1][0]]]))
Yulia Portnova919f3be2015-02-06 12:49:22 +020082 bar.axes.range(1, 0, max_value)
Yulia Portnova0d2bd0a2015-02-11 17:42:44 +020083 bar.axes.style(1, 'N*s*')
Yulia Portnovaffec7602015-02-12 11:16:11 +020084 bar.axes.style(2, '000000', '13')
Yulia Portnova0d2bd0a2015-02-11 17:42:44 +020085
Yulia Portnova407ca952015-04-10 10:38:15 +030086 bar.scale(*[0, max_value] * len(values + deviations))
Yulia Portnova0d2bd0a2015-02-11 17:42:44 +020087
Yulia Portnova6d72d7f2015-02-04 16:48:50 +020088 bar.bar('r', '.1', '1')
Yulia Portnova407ca952015-04-10 10:38:15 +030089 for i in range(1):
Yulia Portnova6d72d7f2015-02-04 16:48:50 +020090 bar.marker('E', '000000', '%s:%s' % ((len(values) + i*2), i),
91 '', '1:10')
Yulia Portnova407ca952015-04-10 10:38:15 +030092 bar.color(*COLORS)
Yulia Portnova6d72d7f2015-02-04 16:48:50 +020093 bar.size(width, height)
94
Yulia Portnova407ca952015-04-10 10:38:15 +030095 axes_type = "xyy"
96
97 scale = [0, max_value] * len(values + deviations)
98 if lines:
99 line_n = 0
100 for data, label, axe, leg in lines:
101 bar.marker('D', COLORS[len(values) + line_n],
102 (len(values + deviations)) + line_n, 0, 3)
103 max_val_l = max(data)
104 if axe:
105 bar.axes.type(axes_type + axe)
106 bar.axes.range(len(axes_type), 0, max_val_l)
107 bar.axes.style(len(axes_type), 'N*s*')
108 bar.axes.label(len(axes_type) + 1, None, label)
109 bar.axes.style(len(axes_type) + 1, '000000', '13')
110 axes_type += axe
111 line_n += 1
112 legend.append(leg)
113 scale += [0, max_val_l]
114
115 bar.legend(*legend)
116 bar.scale(*scale)
Yulia Portnovae3a49982015-02-16 16:06:09 +0200117 img_name = hashlib.md5(str(bar)).hexdigest() + ".png"
koder aka kdanilov66839a92015-04-11 13:22:31 +0300118 img_path = os.path.join(cfg_dict['charts_img_path'], img_name)
Yulia Portnovae3a49982015-02-16 16:06:09 +0200119 if not os.path.exists(img_path):
120 save_image(bar, img_path)
121 return str(bar)
122 return img_path
Yulia Portnova6d72d7f2015-02-04 16:48:50 +0200123
124
Yulia Portnova919f3be2015-02-06 12:49:22 +0200125def render_lines(title, legend, dataset, scale_x, width=700, height=400):
126 line = Line([], encoding="text")
127 line.title(title)
128 line.dataset(dataset)
129
130 line.axes('xy')
131 max_value = (max([max(l) for l in dataset]))
132 line.axes.range(1, 0, max_value)
133 line.scale(0, max_value)
134 line.axes.label(0, *scale_x)
135 line.legend(*legend)
136 line.color(*COLORS[:len(legend)])
137 line.size(width, height)
Yulia Portnovae3a49982015-02-16 16:06:09 +0200138
Yulia Portnovae3a49982015-02-16 16:06:09 +0200139 img_name = hashlib.md5(str(line)).hexdigest() + ".png"
koder aka kdanilov66839a92015-04-11 13:22:31 +0300140 img_path = os.path.join(cfg_dict['charts_img_path'], img_name)
Yulia Portnovae3a49982015-02-16 16:06:09 +0200141 if not os.path.exists(img_path):
142 save_image(line, img_path)
143 return str(line)
144 return img_path