blob: 12d735f84d0b9101fce2eaa61dfb6ceb83ec5a7b [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 Portnova0d2bd0a2015-02-11 17:42:44 +020070 bar.axes.type('xyy')
Yulia Portnova407ca952015-04-10 10:38:15 +030071 bar.axes.label(2, None, label_x)
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 Portnova6d72d7f2015-02-04 16:48:50 +020075
Yulia Portnova8ca20572015-04-14 14:09:39 +030076 max_value = (max([max(l) for l in dataset[:2]]))
Yulia Portnova919f3be2015-02-06 12:49:22 +020077 bar.axes.range(1, 0, max_value)
Yulia Portnova0d2bd0a2015-02-11 17:42:44 +020078 bar.axes.style(1, 'N*s*')
Yulia Portnovaffec7602015-02-12 11:16:11 +020079 bar.axes.style(2, '000000', '13')
Yulia Portnova0d2bd0a2015-02-11 17:42:44 +020080
Yulia Portnova8ca20572015-04-14 14:09:39 +030081 bar.scale(*[0, max_value] * 3)
Yulia Portnova0d2bd0a2015-02-11 17:42:44 +020082
Yulia Portnova6d72d7f2015-02-04 16:48:50 +020083 bar.bar('r', '.1', '1')
Yulia Portnova407ca952015-04-10 10:38:15 +030084 for i in range(1):
Yulia Portnova8ca20572015-04-14 14:09:39 +030085 bar.marker('E', '000000', '%s:%s' % ((len(bars_data) + i*2), i),
Yulia Portnova6d72d7f2015-02-04 16:48:50 +020086 '', '1:10')
Yulia Portnova407ca952015-04-10 10:38:15 +030087 bar.color(*COLORS)
Yulia Portnova6d72d7f2015-02-04 16:48:50 +020088 bar.size(width, height)
89
Yulia Portnova407ca952015-04-10 10:38:15 +030090 axes_type = "xyy"
91
Yulia Portnova8ca20572015-04-14 14:09:39 +030092 scale = [0, max_value] * len(bars_dev_top + bars_dev_bottom + bars_data)
Yulia Portnova407ca952015-04-10 10:38:15 +030093 if lines:
94 line_n = 0
95 for data, label, axe, leg in lines:
Yulia Portnova8ca20572015-04-14 14:09:39 +030096 bar.marker('D', COLORS[len(bars_data) + line_n],
97 (len(bars_data + bars_dev_top + bars_dev_bottom))
98 + line_n, 0, 3)
Yulia Portnova4ef6e9d2015-04-15 12:14:10 +030099 # max_val_l = max(data)
Yulia Portnova407ca952015-04-10 10:38:15 +0300100 if axe:
Yulia Portnova4ef6e9d2015-04-15 12:14:10 +0300101 max_val_l = max(data)
Yulia Portnova407ca952015-04-10 10:38:15 +0300102 bar.axes.type(axes_type + axe)
103 bar.axes.range(len(axes_type), 0, max_val_l)
104 bar.axes.style(len(axes_type), 'N*s*')
105 bar.axes.label(len(axes_type) + 1, None, label)
106 bar.axes.style(len(axes_type) + 1, '000000', '13')
107 axes_type += axe
108 line_n += 1
Yulia Portnova4ef6e9d2015-04-15 12:14:10 +0300109 scale += [0, max_val_l]
110 else:
111 scale += [0, max_value]
Yulia Portnova407ca952015-04-10 10:38:15 +0300112 legend.append(leg)
Yulia Portnova4ef6e9d2015-04-15 12:14:10 +0300113 # scale += [0, max_val_l]
Yulia Portnova407ca952015-04-10 10:38:15 +0300114
115 bar.legend(*legend)
116 bar.scale(*scale)
gstepanov69339ac2015-04-16 20:09:33 +0300117 img_name = file_name + ".png"
koder aka kdanilov66839a92015-04-11 13:22:31 +0300118 img_path = os.path.join(cfg_dict['charts_img_path'], img_name)
koder aka kdanilovcee43342015-04-14 22:52:53 +0300119
Yulia Portnovae3a49982015-02-16 16:06:09 +0200120 if not os.path.exists(img_path):
koder aka kdanilovcee43342015-04-14 22:52:53 +0300121 bar.save(img_path)
122
123 return get_top_top_dir(img_path)
Yulia Portnova6d72d7f2015-02-04 16:48:50 +0200124
125
Yulia Portnova919f3be2015-02-06 12:49:22 +0200126def render_lines(title, legend, dataset, scale_x, width=700, height=400):
127 line = Line([], encoding="text")
128 line.title(title)
129 line.dataset(dataset)
130
131 line.axes('xy')
132 max_value = (max([max(l) for l in dataset]))
133 line.axes.range(1, 0, max_value)
134 line.scale(0, max_value)
135 line.axes.label(0, *scale_x)
136 line.legend(*legend)
137 line.color(*COLORS[:len(legend)])
138 line.size(width, height)
Yulia Portnovae3a49982015-02-16 16:06:09 +0200139
Yulia Portnovae3a49982015-02-16 16:06:09 +0200140 img_name = hashlib.md5(str(line)).hexdigest() + ".png"
koder aka kdanilov66839a92015-04-11 13:22:31 +0300141 img_path = os.path.join(cfg_dict['charts_img_path'], img_name)
Yulia Portnovae3a49982015-02-16 16:06:09 +0200142 if not os.path.exists(img_path):
koder aka kdanilovcee43342015-04-14 22:52:53 +0300143 line.save(img_path)
144
145 return get_top_top_dir(img_path)