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