blob: b4472a4af9ae57b57fa251009a4c97b39e417352 [file] [log] [blame]
Yulia Portnovae3a49982015-02-16 16:06:09 +02001import os
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +03002import sys
koder aka kdanilovcee43342015-04-14 22:52:53 +03003import hashlib
Yulia Portnovae3a49982015-02-16 16:06:09 +02004
Yulia Portnova6d72d7f2015-02-04 16:48:50 +02005from GChartWrapper import Line
6from GChartWrapper import constants
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +03007from GChartWrapper import VerticalBarGroup
Yulia Portnova6d72d7f2015-02-04 16:48:50 +02008
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
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +030012# Patch MARKER constant
13constants.MARKERS += 'E'
14sys.modules['GChartWrapper.GChart'].MARKERS += 'E'
15
16
koder aka kdanilovea7eac72015-04-21 21:37:27 +030017COLORS = ["1569C7", "81D8D0", "B0BD2E", "5CB3FF", "0040FF", "81DAF5"]
Yulia Portnova6d72d7f2015-02-04 16:48:50 +020018constants.MARKERS += 'E' # append E marker to available markers
19
20
koder aka kdanilovcee43342015-04-14 22:52:53 +030021def get_top_top_dir(path):
22 top_top_dir = os.path.dirname(os.path.dirname(path))
23 return path[len(top_top_dir) + 1:]
Yulia Portnovae3a49982015-02-16 16:06:09 +020024
25
Yulia Portnova8ca20572015-04-14 14:09:39 +030026def render_vertical_bar(title, legend, bars_data, bars_dev_top,
gstepanov69339ac2015-04-16 20:09:33 +030027 bars_dev_bottom, file_name,
Yulia Portnova8ca20572015-04-14 14:09:39 +030028 width=700, height=400,
Yulia Portnova407ca952015-04-10 10:38:15 +030029 scale_x=None, scale_y=None, label_x=None,
30 label_y=None, lines=()):
Yulia Portnova6d72d7f2015-02-04 16:48:50 +020031 """
32 Renders vertical bar group chart
33
34 :param legend - list of legend values.
35 Example: ['bar1', 'bar2', 'bar3']
36 :param dataset - list of values for each type (value, deviation)
37 Example:
38 [
Yulia Portnova407ca952015-04-10 10:38:15 +030039 [(10,1), (11, 2), (10,1)], # bar1 values
Yulia Portnova6d72d7f2015-02-04 16:48:50 +020040 [(30,(29,33)),(35,(33,36)), (30,(29,33))], # bar2 values
41 [(20,(19,21)),(20,(13, 24)), (20,(19,21))] # bar 3 values
42 ]
43 :param width - width of chart
44 :param height - height of chart
45 :param scale_x - x ace scale
46 :param scale_y - y ace scale
47
48 :returns url to chart
49
50 dataset example:
51 {
52 'relese_1': {
53 'randr': (1, 0.1),
54 'randwr': (2, 0.2)
55 }
56 'release_2': {
57 'randr': (3, 0.3),
58 'randwr': (4, 0.4)
59 }
60 }
61 """
koder aka kdanilov66839a92015-04-11 13:22:31 +030062
Yulia Portnova6d72d7f2015-02-04 16:48:50 +020063 bar = VerticalBarGroup([], encoding='text')
64 bar.title(title)
65
Yulia Portnova4ef6e9d2015-04-15 12:14:10 +030066 dataset = bars_data + bars_dev_top + bars_dev_bottom + \
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +030067 [l[0] for l in lines]
Yulia Portnova6d72d7f2015-02-04 16:48:50 +020068
Yulia Portnova8ca20572015-04-14 14:09:39 +030069 bar.dataset(dataset, series=len(bars_data))
Yulia Portnova0d1b45f2015-04-28 16:51:11 +030070
71 axes_type = ""
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +030072
Yulia Portnova6d72d7f2015-02-04 16:48:50 +020073 if scale_x:
74 bar.axes.label(0, *scale_x)
Yulia Portnova0d1b45f2015-04-28 16:51:11 +030075 axes_type += "x"
76 if label_x:
77 bar.axes.style(len(axes_type), '000000', '13')
78 bar.axes.label(len(axes_type), label_x)
79 axes_type += "x"
Yulia Portnova6d72d7f2015-02-04 16:48:50 +020080
Yulia Portnova8ca20572015-04-14 14:09:39 +030081 max_value = (max([max(l) for l in dataset[:2]]))
Yulia Portnova0d1b45f2015-04-28 16:51:11 +030082 bar.axes.range(len(axes_type), 0, max_value)
83 bar.axes.style(len(axes_type), 'N*s*')
84 axes_type += "y"
85
86 if label_y:
87 bar.axes.style(len(axes_type), '000000', '13')
88 bar.axes.label(len(axes_type), None, label_y)
89 axes_type += "y"
Yulia Portnova0d2bd0a2015-02-11 17:42:44 +020090
Yulia Portnova8ca20572015-04-14 14:09:39 +030091 bar.scale(*[0, max_value] * 3)
Yulia Portnova0d2bd0a2015-02-11 17:42:44 +020092
Yulia Portnova6d72d7f2015-02-04 16:48:50 +020093 bar.bar('r', '.1', '1')
Yulia Portnova407ca952015-04-10 10:38:15 +030094 for i in range(1):
Yulia Portnova8ca20572015-04-14 14:09:39 +030095 bar.marker('E', '000000', '%s:%s' % ((len(bars_data) + i*2), i),
Yulia Portnova6d72d7f2015-02-04 16:48:50 +020096 '', '1:10')
Yulia Portnova407ca952015-04-10 10:38:15 +030097 bar.color(*COLORS)
Yulia Portnova6d72d7f2015-02-04 16:48:50 +020098 bar.size(width, height)
99
Yulia Portnova8ca20572015-04-14 14:09:39 +0300100 scale = [0, max_value] * len(bars_dev_top + bars_dev_bottom + bars_data)
Yulia Portnova407ca952015-04-10 10:38:15 +0300101 if lines:
102 line_n = 0
103 for data, label, axe, leg in lines:
Yulia Portnova8ca20572015-04-14 14:09:39 +0300104 bar.marker('D', COLORS[len(bars_data) + line_n],
105 (len(bars_data + bars_dev_top + bars_dev_bottom))
106 + line_n, 0, 3)
Yulia Portnova4ef6e9d2015-04-15 12:14:10 +0300107 # max_val_l = max(data)
Yulia Portnova407ca952015-04-10 10:38:15 +0300108 if axe:
Yulia Portnova4ef6e9d2015-04-15 12:14:10 +0300109 max_val_l = max(data)
Yulia Portnova407ca952015-04-10 10:38:15 +0300110 bar.axes.type(axes_type + axe)
111 bar.axes.range(len(axes_type), 0, max_val_l)
112 bar.axes.style(len(axes_type), 'N*s*')
113 bar.axes.label(len(axes_type) + 1, None, label)
114 bar.axes.style(len(axes_type) + 1, '000000', '13')
115 axes_type += axe
116 line_n += 1
Yulia Portnova4ef6e9d2015-04-15 12:14:10 +0300117 scale += [0, max_val_l]
118 else:
119 scale += [0, max_value]
Yulia Portnova407ca952015-04-10 10:38:15 +0300120 legend.append(leg)
Yulia Portnova4ef6e9d2015-04-15 12:14:10 +0300121 # scale += [0, max_val_l]
Yulia Portnova407ca952015-04-10 10:38:15 +0300122
123 bar.legend(*legend)
124 bar.scale(*scale)
koder aka kdanilovd5ed4da2015-05-07 23:33:23 +0300125 img_path = file_name + ".png"
koder aka kdanilovcee43342015-04-14 22:52:53 +0300126
Yulia Portnovae3a49982015-02-16 16:06:09 +0200127 if not os.path.exists(img_path):
koder aka kdanilovcee43342015-04-14 22:52:53 +0300128 bar.save(img_path)
129
130 return get_top_top_dir(img_path)
Yulia Portnova6d72d7f2015-02-04 16:48:50 +0200131
132
Yulia Portnova919f3be2015-02-06 12:49:22 +0200133def render_lines(title, legend, dataset, scale_x, width=700, height=400):
134 line = Line([], encoding="text")
135 line.title(title)
136 line.dataset(dataset)
137
138 line.axes('xy')
139 max_value = (max([max(l) for l in dataset]))
140 line.axes.range(1, 0, max_value)
141 line.scale(0, max_value)
142 line.axes.label(0, *scale_x)
143 line.legend(*legend)
144 line.color(*COLORS[:len(legend)])
145 line.size(width, height)
Yulia Portnovae3a49982015-02-16 16:06:09 +0200146
Yulia Portnovae3a49982015-02-16 16:06:09 +0200147 img_name = hashlib.md5(str(line)).hexdigest() + ".png"
koder aka kdanilov66839a92015-04-11 13:22:31 +0300148 img_path = os.path.join(cfg_dict['charts_img_path'], img_name)
Yulia Portnovae3a49982015-02-16 16:06:09 +0200149 if not os.path.exists(img_path):
koder aka kdanilovcee43342015-04-14 22:52:53 +0300150 line.save(img_path)
151
152 return get_top_top_dir(img_path)