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 | e3a4998 | 2015-02-16 16:06:09 +0200 | [diff] [blame] | 9 | from config import CHARTS_IMG_PATH |
| 10 | |
Yulia Portnova | 6d72d7f | 2015-02-04 16:48:50 +0200 | [diff] [blame] | 11 | |
Yulia Portnova | 919f3be | 2015-02-06 12:49:22 +0200 | [diff] [blame] | 12 | COLORS = ["1569C7", "81D8D0", "307D7E", "5CB3FF", "blue", "indigo"] |
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 | 6d72d7f | 2015-02-04 16:48:50 +0200 | [diff] [blame] | 22 | def render_vertical_bar(title, legend, dataset, width=700, height=400, scale_x=None, |
| 23 | scale_y=None): |
| 24 | """ |
| 25 | Renders vertical bar group chart |
| 26 | |
| 27 | :param legend - list of legend values. |
| 28 | Example: ['bar1', 'bar2', 'bar3'] |
| 29 | :param dataset - list of values for each type (value, deviation) |
| 30 | Example: |
| 31 | [ |
| 32 | [(10,(9,11)), (11, (3,12)), (10,(9,11))], # bar1 values |
| 33 | [(30,(29,33)),(35,(33,36)), (30,(29,33))], # bar2 values |
| 34 | [(20,(19,21)),(20,(13, 24)), (20,(19,21))] # bar 3 values |
| 35 | ] |
| 36 | :param width - width of chart |
| 37 | :param height - height of chart |
| 38 | :param scale_x - x ace scale |
| 39 | :param scale_y - y ace scale |
| 40 | |
| 41 | :returns url to chart |
| 42 | |
| 43 | dataset example: |
| 44 | { |
| 45 | 'relese_1': { |
| 46 | 'randr': (1, 0.1), |
| 47 | 'randwr': (2, 0.2) |
| 48 | } |
| 49 | 'release_2': { |
| 50 | 'randr': (3, 0.3), |
| 51 | 'randwr': (4, 0.4) |
| 52 | } |
| 53 | } |
| 54 | """ |
| 55 | bar = VerticalBarGroup([], encoding='text') |
| 56 | bar.title(title) |
| 57 | |
| 58 | values = [] |
| 59 | deviations = [] |
| 60 | |
| 61 | for d in dataset: |
| 62 | val, dev = zip(*d) |
| 63 | |
| 64 | display_dev = [] |
| 65 | for i in range(len(val)): |
| 66 | display_dev.append((val[i]-dev[i], val[i]+dev[i])) |
| 67 | values.append(val) |
| 68 | # deviations.extend(zip(*dev)) |
| 69 | deviations.extend(zip(*display_dev)) |
| 70 | |
| 71 | bar.dataset(values + deviations, series=len(values)) |
Yulia Portnova | 0d2bd0a | 2015-02-11 17:42:44 +0200 | [diff] [blame] | 72 | bar.axes.type('xyy') |
Yulia Portnova | 7b3b1d4 | 2015-02-13 14:08:35 +0200 | [diff] [blame] | 73 | bar.axes.label(2, None, 'Kbps') |
Yulia Portnova | 6d72d7f | 2015-02-04 16:48:50 +0200 | [diff] [blame] | 74 | if scale_x: |
| 75 | bar.axes.label(0, *scale_x) |
Yulia Portnova | 6d72d7f | 2015-02-04 16:48:50 +0200 | [diff] [blame] | 76 | |
Yulia Portnova | 919f3be | 2015-02-06 12:49:22 +0200 | [diff] [blame] | 77 | max_value = (max([max(l) for l in values + deviations])) |
| 78 | bar.axes.range(1, 0, max_value) |
Yulia Portnova | 0d2bd0a | 2015-02-11 17:42:44 +0200 | [diff] [blame] | 79 | bar.axes.style(1, 'N*s*') |
Yulia Portnova | ffec760 | 2015-02-12 11:16:11 +0200 | [diff] [blame] | 80 | bar.axes.style(2, '000000', '13') |
Yulia Portnova | 0d2bd0a | 2015-02-11 17:42:44 +0200 | [diff] [blame] | 81 | |
Yulia Portnova | 919f3be | 2015-02-06 12:49:22 +0200 | [diff] [blame] | 82 | bar.scale(0, max_value) |
Yulia Portnova | 0d2bd0a | 2015-02-11 17:42:44 +0200 | [diff] [blame] | 83 | |
Yulia Portnova | 6d72d7f | 2015-02-04 16:48:50 +0200 | [diff] [blame] | 84 | bar.bar('r', '.1', '1') |
| 85 | for i in range(len(legend)): |
| 86 | bar.marker('E', '000000', '%s:%s' % ((len(values) + i*2), i), |
| 87 | '', '1:10') |
| 88 | bar.legend(*legend) |
| 89 | bar.color(*COLORS[:len(values)]) |
| 90 | bar.size(width, height) |
| 91 | |
Yulia Portnova | e3a4998 | 2015-02-16 16:06:09 +0200 | [diff] [blame] | 92 | img_name = hashlib.md5(str(bar)).hexdigest() + ".png" |
| 93 | img_path = os.path.join(CHARTS_IMG_PATH, img_name) |
| 94 | if not os.path.exists(img_path): |
| 95 | save_image(bar, img_path) |
| 96 | return str(bar) |
| 97 | return img_path |
Yulia Portnova | 6d72d7f | 2015-02-04 16:48:50 +0200 | [diff] [blame] | 98 | |
| 99 | |
Yulia Portnova | 919f3be | 2015-02-06 12:49:22 +0200 | [diff] [blame] | 100 | def render_lines(title, legend, dataset, scale_x, width=700, height=400): |
| 101 | line = Line([], encoding="text") |
| 102 | line.title(title) |
| 103 | line.dataset(dataset) |
| 104 | |
| 105 | line.axes('xy') |
| 106 | max_value = (max([max(l) for l in dataset])) |
| 107 | line.axes.range(1, 0, max_value) |
| 108 | line.scale(0, max_value) |
| 109 | line.axes.label(0, *scale_x) |
| 110 | line.legend(*legend) |
| 111 | line.color(*COLORS[:len(legend)]) |
| 112 | line.size(width, height) |
Yulia Portnova | e3a4998 | 2015-02-16 16:06:09 +0200 | [diff] [blame] | 113 | |
Yulia Portnova | e3a4998 | 2015-02-16 16:06:09 +0200 | [diff] [blame] | 114 | |
| 115 | img_name = hashlib.md5(str(line)).hexdigest() + ".png" |
| 116 | img_path = os.path.join(CHARTS_IMG_PATH, img_name) |
| 117 | if not os.path.exists(img_path): |
| 118 | save_image(line, img_path) |
| 119 | return str(line) |
| 120 | return img_path |