blob: b53ac5a23140513f8270e4ee2775b7371ab57e16 [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
10CHARTS_IMG_PATH = cfg_dict['charts_img_path']
Yulia Portnovae3a49982015-02-16 16:06:09 +020011
Yulia Portnova6d72d7f2015-02-04 16:48:50 +020012
Yulia Portnova407ca952015-04-10 10:38:15 +030013COLORS = ["1569C7", "81D8D0", "307D7E", "5CB3FF", "0040FF", "81DAF5"]
Yulia Portnova6d72d7f2015-02-04 16:48:50 +020014constants.MARKERS += 'E' # append E marker to available markers
15
16
Yulia Portnovae3a49982015-02-16 16:06:09 +020017def save_image(chart, img_path):
18 t = threading.Thread(target=chart.save, kwargs={'fname': img_path})
19 t.daemon = True
20 t.start()
21
22
Yulia Portnova407ca952015-04-10 10:38:15 +030023def render_vertical_bar(title, legend, dataset, width=700, height=400,
24 scale_x=None, scale_y=None, label_x=None,
25 label_y=None, lines=()):
Yulia Portnova6d72d7f2015-02-04 16:48:50 +020026 """
27 Renders vertical bar group chart
28
29 :param legend - list of legend values.
30 Example: ['bar1', 'bar2', 'bar3']
31 :param dataset - list of values for each type (value, deviation)
32 Example:
33 [
Yulia Portnova407ca952015-04-10 10:38:15 +030034 [(10,1), (11, 2), (10,1)], # bar1 values
Yulia Portnova6d72d7f2015-02-04 16:48:50 +020035 [(30,(29,33)),(35,(33,36)), (30,(29,33))], # bar2 values
36 [(20,(19,21)),(20,(13, 24)), (20,(19,21))] # bar 3 values
37 ]
38 :param width - width of chart
39 :param height - height of chart
40 :param scale_x - x ace scale
41 :param scale_y - y ace scale
42
43 :returns url to chart
44
45 dataset example:
46 {
47 'relese_1': {
48 'randr': (1, 0.1),
49 'randwr': (2, 0.2)
50 }
51 'release_2': {
52 'randr': (3, 0.3),
53 'randwr': (4, 0.4)
54 }
55 }
56 """
57 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))
74 bar.dataset(values + deviations + [l[0] for l in lines], series=len(values))
Yulia Portnova0d2bd0a2015-02-11 17:42:44 +020075 bar.axes.type('xyy')
Yulia Portnova407ca952015-04-10 10:38:15 +030076 bar.axes.label(2, None, label_x)
Yulia Portnova6d72d7f2015-02-04 16:48:50 +020077 if scale_x:
78 bar.axes.label(0, *scale_x)
Yulia Portnova6d72d7f2015-02-04 16:48:50 +020079
Yulia Portnova407ca952015-04-10 10:38:15 +030080 max_value = (max([max(l) for l in values + deviations + [lines[1][0]]]))
Yulia Portnova919f3be2015-02-06 12:49:22 +020081 bar.axes.range(1, 0, max_value)
Yulia Portnova0d2bd0a2015-02-11 17:42:44 +020082 bar.axes.style(1, 'N*s*')
Yulia Portnovaffec7602015-02-12 11:16:11 +020083 bar.axes.style(2, '000000', '13')
Yulia Portnova0d2bd0a2015-02-11 17:42:44 +020084
Yulia Portnova407ca952015-04-10 10:38:15 +030085 bar.scale(*[0, max_value] * len(values + deviations))
Yulia Portnova0d2bd0a2015-02-11 17:42:44 +020086
Yulia Portnova6d72d7f2015-02-04 16:48:50 +020087 bar.bar('r', '.1', '1')
Yulia Portnova407ca952015-04-10 10:38:15 +030088 for i in range(1):
Yulia Portnova6d72d7f2015-02-04 16:48:50 +020089 bar.marker('E', '000000', '%s:%s' % ((len(values) + i*2), i),
90 '', '1:10')
Yulia Portnova407ca952015-04-10 10:38:15 +030091 bar.color(*COLORS)
Yulia Portnova6d72d7f2015-02-04 16:48:50 +020092 bar.size(width, height)
93
Yulia Portnova407ca952015-04-10 10:38:15 +030094 axes_type = "xyy"
95
96 scale = [0, max_value] * len(values + deviations)
97 if lines:
98 line_n = 0
99 for data, label, axe, leg in lines:
100 bar.marker('D', COLORS[len(values) + line_n],
101 (len(values + deviations)) + line_n, 0, 3)
102 max_val_l = max(data)
103 if axe:
104 bar.axes.type(axes_type + axe)
105 bar.axes.range(len(axes_type), 0, max_val_l)
106 bar.axes.style(len(axes_type), 'N*s*')
107 bar.axes.label(len(axes_type) + 1, None, label)
108 bar.axes.style(len(axes_type) + 1, '000000', '13')
109 axes_type += axe
110 line_n += 1
111 legend.append(leg)
112 scale += [0, max_val_l]
113
114 bar.legend(*legend)
115 bar.scale(*scale)
Yulia Portnovae3a49982015-02-16 16:06:09 +0200116 img_name = hashlib.md5(str(bar)).hexdigest() + ".png"
117 img_path = os.path.join(CHARTS_IMG_PATH, img_name)
118 if not os.path.exists(img_path):
119 save_image(bar, img_path)
120 return str(bar)
121 return img_path
Yulia Portnova6d72d7f2015-02-04 16:48:50 +0200122
123
Yulia Portnova919f3be2015-02-06 12:49:22 +0200124def render_lines(title, legend, dataset, scale_x, width=700, height=400):
125 line = Line([], encoding="text")
126 line.title(title)
127 line.dataset(dataset)
128
129 line.axes('xy')
130 max_value = (max([max(l) for l in dataset]))
131 line.axes.range(1, 0, max_value)
132 line.scale(0, max_value)
133 line.axes.label(0, *scale_x)
134 line.legend(*legend)
135 line.color(*COLORS[:len(legend)])
136 line.size(width, height)
Yulia Portnovae3a49982015-02-16 16:06:09 +0200137
Yulia Portnovae3a49982015-02-16 16:06:09 +0200138 img_name = hashlib.md5(str(line)).hexdigest() + ".png"
139 img_path = os.path.join(CHARTS_IMG_PATH, img_name)
140 if not os.path.exists(img_path):
141 save_image(line, img_path)
142 return str(line)
143 return img_path