Yulia Portnova | e3a4998 | 2015-02-16 16:06:09 +0200 | [diff] [blame] | 1 | import os |
koder aka kdanilov | cee4334 | 2015-04-14 22:52:53 +0300 | [diff] [blame] | 2 | import hashlib |
Yulia Portnova | e3a4998 | 2015-02-16 16:06:09 +0200 | [diff] [blame] | 3 | import threading |
| 4 | |
Yulia Portnova | 6d72d7f | 2015-02-04 16:48:50 +0200 | [diff] [blame] | 5 | from GChartWrapper import VerticalBarGroup |
| 6 | from GChartWrapper import Line |
| 7 | from GChartWrapper import constants |
| 8 | |
Yulia Portnova | 407ca95 | 2015-04-10 10:38:15 +0300 | [diff] [blame] | 9 | from config import cfg_dict |
Yulia Portnova | e3a4998 | 2015-02-16 16:06:09 +0200 | [diff] [blame] | 10 | |
Yulia Portnova | 6d72d7f | 2015-02-04 16:48:50 +0200 | [diff] [blame] | 11 | |
Yulia Portnova | 407ca95 | 2015-04-10 10:38:15 +0300 | [diff] [blame] | 12 | COLORS = ["1569C7", "81D8D0", "307D7E", "5CB3FF", "0040FF", "81DAF5"] |
Yulia Portnova | 6d72d7f | 2015-02-04 16:48:50 +0200 | [diff] [blame] | 13 | constants.MARKERS += 'E' # append E marker to available markers |
| 14 | |
| 15 | |
koder aka kdanilov | cee4334 | 2015-04-14 22:52:53 +0300 | [diff] [blame] | 16 | def get_top_top_dir(path): |
| 17 | top_top_dir = os.path.dirname(os.path.dirname(path)) |
| 18 | return path[len(top_top_dir) + 1:] |
Yulia Portnova | e3a4998 | 2015-02-16 16:06:09 +0200 | [diff] [blame] | 19 | |
| 20 | |
Yulia Portnova | 8ca2057 | 2015-04-14 14:09:39 +0300 | [diff] [blame] | 21 | def render_vertical_bar(title, legend, bars_data, bars_dev_top, |
gstepanov | 69339ac | 2015-04-16 20:09:33 +0300 | [diff] [blame^] | 22 | bars_dev_bottom, file_name, |
Yulia Portnova | 8ca2057 | 2015-04-14 14:09:39 +0300 | [diff] [blame] | 23 | width=700, height=400, |
Yulia Portnova | 407ca95 | 2015-04-10 10:38:15 +0300 | [diff] [blame] | 24 | scale_x=None, scale_y=None, label_x=None, |
| 25 | label_y=None, lines=()): |
Yulia Portnova | 6d72d7f | 2015-02-04 16:48:50 +0200 | [diff] [blame] | 26 | """ |
| 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 Portnova | 407ca95 | 2015-04-10 10:38:15 +0300 | [diff] [blame] | 34 | [(10,1), (11, 2), (10,1)], # bar1 values |
Yulia Portnova | 6d72d7f | 2015-02-04 16:48:50 +0200 | [diff] [blame] | 35 | [(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 | """ |
koder aka kdanilov | 66839a9 | 2015-04-11 13:22:31 +0300 | [diff] [blame] | 57 | |
Yulia Portnova | 6d72d7f | 2015-02-04 16:48:50 +0200 | [diff] [blame] | 58 | bar = VerticalBarGroup([], encoding='text') |
| 59 | bar.title(title) |
| 60 | |
Yulia Portnova | 4ef6e9d | 2015-04-15 12:14:10 +0300 | [diff] [blame] | 61 | dataset = bars_data + bars_dev_top + bars_dev_bottom + \ |
| 62 | [l[0] for l in lines] |
Yulia Portnova | 6d72d7f | 2015-02-04 16:48:50 +0200 | [diff] [blame] | 63 | |
Yulia Portnova | 8ca2057 | 2015-04-14 14:09:39 +0300 | [diff] [blame] | 64 | bar.dataset(dataset, series=len(bars_data)) |
Yulia Portnova | 0d2bd0a | 2015-02-11 17:42:44 +0200 | [diff] [blame] | 65 | bar.axes.type('xyy') |
Yulia Portnova | 407ca95 | 2015-04-10 10:38:15 +0300 | [diff] [blame] | 66 | bar.axes.label(2, None, label_x) |
Yulia Portnova | 4ef6e9d | 2015-04-15 12:14:10 +0300 | [diff] [blame] | 67 | |
Yulia Portnova | 6d72d7f | 2015-02-04 16:48:50 +0200 | [diff] [blame] | 68 | if scale_x: |
| 69 | bar.axes.label(0, *scale_x) |
Yulia Portnova | 6d72d7f | 2015-02-04 16:48:50 +0200 | [diff] [blame] | 70 | |
Yulia Portnova | 8ca2057 | 2015-04-14 14:09:39 +0300 | [diff] [blame] | 71 | max_value = (max([max(l) for l in dataset[:2]])) |
Yulia Portnova | 919f3be | 2015-02-06 12:49:22 +0200 | [diff] [blame] | 72 | bar.axes.range(1, 0, max_value) |
Yulia Portnova | 0d2bd0a | 2015-02-11 17:42:44 +0200 | [diff] [blame] | 73 | bar.axes.style(1, 'N*s*') |
Yulia Portnova | ffec760 | 2015-02-12 11:16:11 +0200 | [diff] [blame] | 74 | bar.axes.style(2, '000000', '13') |
Yulia Portnova | 0d2bd0a | 2015-02-11 17:42:44 +0200 | [diff] [blame] | 75 | |
Yulia Portnova | 4ef6e9d | 2015-04-15 12:14:10 +0300 | [diff] [blame] | 76 | |
Yulia Portnova | 8ca2057 | 2015-04-14 14:09:39 +0300 | [diff] [blame] | 77 | bar.scale(*[0, max_value] * 3) |
Yulia Portnova | 0d2bd0a | 2015-02-11 17:42:44 +0200 | [diff] [blame] | 78 | |
Yulia Portnova | 6d72d7f | 2015-02-04 16:48:50 +0200 | [diff] [blame] | 79 | bar.bar('r', '.1', '1') |
Yulia Portnova | 407ca95 | 2015-04-10 10:38:15 +0300 | [diff] [blame] | 80 | for i in range(1): |
Yulia Portnova | 8ca2057 | 2015-04-14 14:09:39 +0300 | [diff] [blame] | 81 | bar.marker('E', '000000', '%s:%s' % ((len(bars_data) + i*2), i), |
Yulia Portnova | 6d72d7f | 2015-02-04 16:48:50 +0200 | [diff] [blame] | 82 | '', '1:10') |
Yulia Portnova | 407ca95 | 2015-04-10 10:38:15 +0300 | [diff] [blame] | 83 | bar.color(*COLORS) |
Yulia Portnova | 6d72d7f | 2015-02-04 16:48:50 +0200 | [diff] [blame] | 84 | bar.size(width, height) |
| 85 | |
Yulia Portnova | 407ca95 | 2015-04-10 10:38:15 +0300 | [diff] [blame] | 86 | axes_type = "xyy" |
| 87 | |
Yulia Portnova | 8ca2057 | 2015-04-14 14:09:39 +0300 | [diff] [blame] | 88 | scale = [0, max_value] * len(bars_dev_top + bars_dev_bottom + bars_data) |
Yulia Portnova | 407ca95 | 2015-04-10 10:38:15 +0300 | [diff] [blame] | 89 | if lines: |
| 90 | line_n = 0 |
| 91 | for data, label, axe, leg in lines: |
Yulia Portnova | 8ca2057 | 2015-04-14 14:09:39 +0300 | [diff] [blame] | 92 | bar.marker('D', COLORS[len(bars_data) + line_n], |
| 93 | (len(bars_data + bars_dev_top + bars_dev_bottom)) |
| 94 | + line_n, 0, 3) |
Yulia Portnova | 4ef6e9d | 2015-04-15 12:14:10 +0300 | [diff] [blame] | 95 | # max_val_l = max(data) |
Yulia Portnova | 407ca95 | 2015-04-10 10:38:15 +0300 | [diff] [blame] | 96 | if axe: |
Yulia Portnova | 4ef6e9d | 2015-04-15 12:14:10 +0300 | [diff] [blame] | 97 | max_val_l = max(data) |
Yulia Portnova | 407ca95 | 2015-04-10 10:38:15 +0300 | [diff] [blame] | 98 | bar.axes.type(axes_type + axe) |
| 99 | bar.axes.range(len(axes_type), 0, max_val_l) |
| 100 | bar.axes.style(len(axes_type), 'N*s*') |
| 101 | bar.axes.label(len(axes_type) + 1, None, label) |
| 102 | bar.axes.style(len(axes_type) + 1, '000000', '13') |
| 103 | axes_type += axe |
| 104 | line_n += 1 |
Yulia Portnova | 4ef6e9d | 2015-04-15 12:14:10 +0300 | [diff] [blame] | 105 | scale += [0, max_val_l] |
| 106 | else: |
| 107 | scale += [0, max_value] |
Yulia Portnova | 407ca95 | 2015-04-10 10:38:15 +0300 | [diff] [blame] | 108 | legend.append(leg) |
Yulia Portnova | 4ef6e9d | 2015-04-15 12:14:10 +0300 | [diff] [blame] | 109 | # scale += [0, max_val_l] |
Yulia Portnova | 407ca95 | 2015-04-10 10:38:15 +0300 | [diff] [blame] | 110 | |
| 111 | bar.legend(*legend) |
| 112 | bar.scale(*scale) |
gstepanov | 69339ac | 2015-04-16 20:09:33 +0300 | [diff] [blame^] | 113 | img_name = file_name + ".png" |
koder aka kdanilov | 66839a9 | 2015-04-11 13:22:31 +0300 | [diff] [blame] | 114 | img_path = os.path.join(cfg_dict['charts_img_path'], img_name) |
koder aka kdanilov | cee4334 | 2015-04-14 22:52:53 +0300 | [diff] [blame] | 115 | |
Yulia Portnova | e3a4998 | 2015-02-16 16:06:09 +0200 | [diff] [blame] | 116 | if not os.path.exists(img_path): |
koder aka kdanilov | cee4334 | 2015-04-14 22:52:53 +0300 | [diff] [blame] | 117 | bar.save(img_path) |
| 118 | |
| 119 | return get_top_top_dir(img_path) |
Yulia Portnova | 6d72d7f | 2015-02-04 16:48:50 +0200 | [diff] [blame] | 120 | |
| 121 | |
Yulia Portnova | 919f3be | 2015-02-06 12:49:22 +0200 | [diff] [blame] | 122 | def render_lines(title, legend, dataset, scale_x, width=700, height=400): |
| 123 | line = Line([], encoding="text") |
| 124 | line.title(title) |
| 125 | line.dataset(dataset) |
| 126 | |
| 127 | line.axes('xy') |
| 128 | max_value = (max([max(l) for l in dataset])) |
| 129 | line.axes.range(1, 0, max_value) |
| 130 | line.scale(0, max_value) |
| 131 | line.axes.label(0, *scale_x) |
| 132 | line.legend(*legend) |
| 133 | line.color(*COLORS[:len(legend)]) |
| 134 | line.size(width, height) |
Yulia Portnova | e3a4998 | 2015-02-16 16:06:09 +0200 | [diff] [blame] | 135 | |
Yulia Portnova | e3a4998 | 2015-02-16 16:06:09 +0200 | [diff] [blame] | 136 | img_name = hashlib.md5(str(line)).hexdigest() + ".png" |
koder aka kdanilov | 66839a9 | 2015-04-11 13:22:31 +0300 | [diff] [blame] | 137 | img_path = os.path.join(cfg_dict['charts_img_path'], img_name) |
Yulia Portnova | e3a4998 | 2015-02-16 16:06:09 +0200 | [diff] [blame] | 138 | if not os.path.exists(img_path): |
koder aka kdanilov | cee4334 | 2015-04-14 22:52:53 +0300 | [diff] [blame] | 139 | line.save(img_path) |
| 140 | |
| 141 | return get_top_top_dir(img_path) |