Yulia Portnova | e3a4998 | 2015-02-16 16:06:09 +0200 | [diff] [blame] | 1 | import hashlib |
| 2 | import os |
| 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 | |
Yulia Portnova | e3a4998 | 2015-02-16 16:06:09 +0200 | [diff] [blame] | 16 | def save_image(chart, img_path): |
| 17 | t = threading.Thread(target=chart.save, kwargs={'fname': img_path}) |
| 18 | t.daemon = True |
| 19 | t.start() |
| 20 | |
| 21 | |
Yulia Portnova | 407ca95 | 2015-04-10 10:38:15 +0300 | [diff] [blame] | 22 | def render_vertical_bar(title, legend, dataset, width=700, height=400, |
| 23 | scale_x=None, scale_y=None, label_x=None, |
| 24 | label_y=None, lines=()): |
Yulia Portnova | 6d72d7f | 2015-02-04 16:48:50 +0200 | [diff] [blame] | 25 | """ |
| 26 | Renders vertical bar group chart |
| 27 | |
| 28 | :param legend - list of legend values. |
| 29 | Example: ['bar1', 'bar2', 'bar3'] |
| 30 | :param dataset - list of values for each type (value, deviation) |
| 31 | Example: |
| 32 | [ |
Yulia Portnova | 407ca95 | 2015-04-10 10:38:15 +0300 | [diff] [blame] | 33 | [(10,1), (11, 2), (10,1)], # bar1 values |
Yulia Portnova | 6d72d7f | 2015-02-04 16:48:50 +0200 | [diff] [blame] | 34 | [(30,(29,33)),(35,(33,36)), (30,(29,33))], # bar2 values |
| 35 | [(20,(19,21)),(20,(13, 24)), (20,(19,21))] # bar 3 values |
| 36 | ] |
| 37 | :param width - width of chart |
| 38 | :param height - height of chart |
| 39 | :param scale_x - x ace scale |
| 40 | :param scale_y - y ace scale |
| 41 | |
| 42 | :returns url to chart |
| 43 | |
| 44 | dataset example: |
| 45 | { |
| 46 | 'relese_1': { |
| 47 | 'randr': (1, 0.1), |
| 48 | 'randwr': (2, 0.2) |
| 49 | } |
| 50 | 'release_2': { |
| 51 | 'randr': (3, 0.3), |
| 52 | 'randwr': (4, 0.4) |
| 53 | } |
| 54 | } |
| 55 | """ |
koder aka kdanilov | 66839a9 | 2015-04-11 13:22:31 +0300 | [diff] [blame^] | 56 | |
Yulia Portnova | 6d72d7f | 2015-02-04 16:48:50 +0200 | [diff] [blame] | 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 Portnova | 407ca95 | 2015-04-10 10:38:15 +0300 | [diff] [blame] | 73 | # bar.dataset(values + deviations, series=len(values)) |
koder aka kdanilov | 66839a9 | 2015-04-11 13:22:31 +0300 | [diff] [blame^] | 74 | bar.dataset(values + deviations + [l[0] for l in lines], |
| 75 | series=len(values)) |
Yulia Portnova | 0d2bd0a | 2015-02-11 17:42:44 +0200 | [diff] [blame] | 76 | bar.axes.type('xyy') |
Yulia Portnova | 407ca95 | 2015-04-10 10:38:15 +0300 | [diff] [blame] | 77 | bar.axes.label(2, None, label_x) |
Yulia Portnova | 6d72d7f | 2015-02-04 16:48:50 +0200 | [diff] [blame] | 78 | if scale_x: |
| 79 | bar.axes.label(0, *scale_x) |
Yulia Portnova | 6d72d7f | 2015-02-04 16:48:50 +0200 | [diff] [blame] | 80 | |
Yulia Portnova | 407ca95 | 2015-04-10 10:38:15 +0300 | [diff] [blame] | 81 | max_value = (max([max(l) for l in values + deviations + [lines[1][0]]])) |
Yulia Portnova | 919f3be | 2015-02-06 12:49:22 +0200 | [diff] [blame] | 82 | bar.axes.range(1, 0, max_value) |
Yulia Portnova | 0d2bd0a | 2015-02-11 17:42:44 +0200 | [diff] [blame] | 83 | bar.axes.style(1, 'N*s*') |
Yulia Portnova | ffec760 | 2015-02-12 11:16:11 +0200 | [diff] [blame] | 84 | bar.axes.style(2, '000000', '13') |
Yulia Portnova | 0d2bd0a | 2015-02-11 17:42:44 +0200 | [diff] [blame] | 85 | |
Yulia Portnova | 407ca95 | 2015-04-10 10:38:15 +0300 | [diff] [blame] | 86 | bar.scale(*[0, max_value] * len(values + deviations)) |
Yulia Portnova | 0d2bd0a | 2015-02-11 17:42:44 +0200 | [diff] [blame] | 87 | |
Yulia Portnova | 6d72d7f | 2015-02-04 16:48:50 +0200 | [diff] [blame] | 88 | bar.bar('r', '.1', '1') |
Yulia Portnova | 407ca95 | 2015-04-10 10:38:15 +0300 | [diff] [blame] | 89 | for i in range(1): |
Yulia Portnova | 6d72d7f | 2015-02-04 16:48:50 +0200 | [diff] [blame] | 90 | bar.marker('E', '000000', '%s:%s' % ((len(values) + i*2), i), |
| 91 | '', '1:10') |
Yulia Portnova | 407ca95 | 2015-04-10 10:38:15 +0300 | [diff] [blame] | 92 | bar.color(*COLORS) |
Yulia Portnova | 6d72d7f | 2015-02-04 16:48:50 +0200 | [diff] [blame] | 93 | bar.size(width, height) |
| 94 | |
Yulia Portnova | 407ca95 | 2015-04-10 10:38:15 +0300 | [diff] [blame] | 95 | axes_type = "xyy" |
| 96 | |
| 97 | scale = [0, max_value] * len(values + deviations) |
| 98 | if lines: |
| 99 | line_n = 0 |
| 100 | for data, label, axe, leg in lines: |
| 101 | bar.marker('D', COLORS[len(values) + line_n], |
| 102 | (len(values + deviations)) + line_n, 0, 3) |
| 103 | max_val_l = max(data) |
| 104 | if axe: |
| 105 | bar.axes.type(axes_type + axe) |
| 106 | bar.axes.range(len(axes_type), 0, max_val_l) |
| 107 | bar.axes.style(len(axes_type), 'N*s*') |
| 108 | bar.axes.label(len(axes_type) + 1, None, label) |
| 109 | bar.axes.style(len(axes_type) + 1, '000000', '13') |
| 110 | axes_type += axe |
| 111 | line_n += 1 |
| 112 | legend.append(leg) |
| 113 | scale += [0, max_val_l] |
| 114 | |
| 115 | bar.legend(*legend) |
| 116 | bar.scale(*scale) |
Yulia Portnova | e3a4998 | 2015-02-16 16:06:09 +0200 | [diff] [blame] | 117 | img_name = hashlib.md5(str(bar)).hexdigest() + ".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) |
Yulia Portnova | e3a4998 | 2015-02-16 16:06:09 +0200 | [diff] [blame] | 119 | if not os.path.exists(img_path): |
| 120 | save_image(bar, img_path) |
| 121 | return str(bar) |
| 122 | return img_path |
Yulia Portnova | 6d72d7f | 2015-02-04 16:48:50 +0200 | [diff] [blame] | 123 | |
| 124 | |
Yulia Portnova | 919f3be | 2015-02-06 12:49:22 +0200 | [diff] [blame] | 125 | def render_lines(title, legend, dataset, scale_x, width=700, height=400): |
| 126 | line = Line([], encoding="text") |
| 127 | line.title(title) |
| 128 | line.dataset(dataset) |
| 129 | |
| 130 | line.axes('xy') |
| 131 | max_value = (max([max(l) for l in dataset])) |
| 132 | line.axes.range(1, 0, max_value) |
| 133 | line.scale(0, max_value) |
| 134 | line.axes.label(0, *scale_x) |
| 135 | line.legend(*legend) |
| 136 | line.color(*COLORS[:len(legend)]) |
| 137 | line.size(width, height) |
Yulia Portnova | e3a4998 | 2015-02-16 16:06:09 +0200 | [diff] [blame] | 138 | |
Yulia Portnova | e3a4998 | 2015-02-16 16:06:09 +0200 | [diff] [blame] | 139 | img_name = hashlib.md5(str(line)).hexdigest() + ".png" |
koder aka kdanilov | 66839a9 | 2015-04-11 13:22:31 +0300 | [diff] [blame^] | 140 | img_path = os.path.join(cfg_dict['charts_img_path'], img_name) |
Yulia Portnova | e3a4998 | 2015-02-16 16:06:09 +0200 | [diff] [blame] | 141 | if not os.path.exists(img_path): |
| 142 | save_image(line, img_path) |
| 143 | return str(line) |
| 144 | return img_path |