Yulia Portnova | 6d72d7f | 2015-02-04 16:48:50 +0200 | [diff] [blame] | 1 | from GChartWrapper import VerticalBarGroup |
| 2 | from GChartWrapper import Line |
| 3 | from GChartWrapper import constants |
| 4 | |
| 5 | |
Yulia Portnova | 919f3be | 2015-02-06 12:49:22 +0200 | [diff] [blame] | 6 | COLORS = ["1569C7", "81D8D0", "307D7E", "5CB3FF", "blue", "indigo"] |
Yulia Portnova | 6d72d7f | 2015-02-04 16:48:50 +0200 | [diff] [blame] | 7 | constants.MARKERS += 'E' # append E marker to available markers |
| 8 | |
| 9 | |
| 10 | def render_vertical_bar(title, legend, dataset, width=700, height=400, scale_x=None, |
| 11 | scale_y=None): |
| 12 | """ |
| 13 | Renders vertical bar group chart |
| 14 | |
| 15 | :param legend - list of legend values. |
| 16 | Example: ['bar1', 'bar2', 'bar3'] |
| 17 | :param dataset - list of values for each type (value, deviation) |
| 18 | Example: |
| 19 | [ |
| 20 | [(10,(9,11)), (11, (3,12)), (10,(9,11))], # bar1 values |
| 21 | [(30,(29,33)),(35,(33,36)), (30,(29,33))], # bar2 values |
| 22 | [(20,(19,21)),(20,(13, 24)), (20,(19,21))] # bar 3 values |
| 23 | ] |
| 24 | :param width - width of chart |
| 25 | :param height - height of chart |
| 26 | :param scale_x - x ace scale |
| 27 | :param scale_y - y ace scale |
| 28 | |
| 29 | :returns url to chart |
| 30 | |
| 31 | dataset example: |
| 32 | { |
| 33 | 'relese_1': { |
| 34 | 'randr': (1, 0.1), |
| 35 | 'randwr': (2, 0.2) |
| 36 | } |
| 37 | 'release_2': { |
| 38 | 'randr': (3, 0.3), |
| 39 | 'randwr': (4, 0.4) |
| 40 | } |
| 41 | } |
| 42 | """ |
| 43 | bar = VerticalBarGroup([], encoding='text') |
| 44 | bar.title(title) |
| 45 | |
| 46 | values = [] |
| 47 | deviations = [] |
| 48 | |
| 49 | for d in dataset: |
| 50 | val, dev = zip(*d) |
| 51 | |
| 52 | display_dev = [] |
| 53 | for i in range(len(val)): |
| 54 | display_dev.append((val[i]-dev[i], val[i]+dev[i])) |
| 55 | values.append(val) |
| 56 | # deviations.extend(zip(*dev)) |
| 57 | deviations.extend(zip(*display_dev)) |
| 58 | |
| 59 | bar.dataset(values + deviations, series=len(values)) |
Yulia Portnova | 0d2bd0a | 2015-02-11 17:42:44 +0200 | [diff] [blame^] | 60 | bar.axes.type('xyy') |
| 61 | bar.axes.label(2, None, 'kb/s') |
Yulia Portnova | 6d72d7f | 2015-02-04 16:48:50 +0200 | [diff] [blame] | 62 | if scale_x: |
| 63 | bar.axes.label(0, *scale_x) |
Yulia Portnova | 6d72d7f | 2015-02-04 16:48:50 +0200 | [diff] [blame] | 64 | |
Yulia Portnova | 919f3be | 2015-02-06 12:49:22 +0200 | [diff] [blame] | 65 | max_value = (max([max(l) for l in values + deviations])) |
| 66 | bar.axes.range(1, 0, max_value) |
Yulia Portnova | 0d2bd0a | 2015-02-11 17:42:44 +0200 | [diff] [blame^] | 67 | bar.axes.style(1, 'N*s*') |
| 68 | |
Yulia Portnova | 919f3be | 2015-02-06 12:49:22 +0200 | [diff] [blame] | 69 | bar.scale(0, max_value) |
Yulia Portnova | 0d2bd0a | 2015-02-11 17:42:44 +0200 | [diff] [blame^] | 70 | |
Yulia Portnova | 6d72d7f | 2015-02-04 16:48:50 +0200 | [diff] [blame] | 71 | bar.bar('r', '.1', '1') |
| 72 | for i in range(len(legend)): |
| 73 | bar.marker('E', '000000', '%s:%s' % ((len(values) + i*2), i), |
| 74 | '', '1:10') |
| 75 | bar.legend(*legend) |
| 76 | bar.color(*COLORS[:len(values)]) |
| 77 | bar.size(width, height) |
| 78 | |
Yulia Portnova | 919f3be | 2015-02-06 12:49:22 +0200 | [diff] [blame] | 79 | return bar |
Yulia Portnova | 6d72d7f | 2015-02-04 16:48:50 +0200 | [diff] [blame] | 80 | |
| 81 | |
Yulia Portnova | 919f3be | 2015-02-06 12:49:22 +0200 | [diff] [blame] | 82 | def render_lines(title, legend, dataset, scale_x, width=700, height=400): |
| 83 | line = Line([], encoding="text") |
| 84 | line.title(title) |
| 85 | line.dataset(dataset) |
| 86 | |
| 87 | line.axes('xy') |
| 88 | max_value = (max([max(l) for l in dataset])) |
| 89 | line.axes.range(1, 0, max_value) |
| 90 | line.scale(0, max_value) |
| 91 | line.axes.label(0, *scale_x) |
| 92 | line.legend(*legend) |
| 93 | line.color(*COLORS[:len(legend)]) |
| 94 | line.size(width, height) |
| 95 | return str(line) |