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 | |
| 6 | COLORS = ["red", "orange", "yellow", "green", "blue", "indigo", "violet"] |
| 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)) |
| 60 | |
| 61 | bar.axes.type('xy') |
| 62 | if scale_x: |
| 63 | bar.axes.label(0, *scale_x) |
| 64 | scale_y = scale_y or range(int(max([max(l) for l in values]) + 2)) |
| 65 | bar.axes.range(1, *scale_y) |
| 66 | |
| 67 | bar.bar('r', '.1', '1') |
| 68 | for i in range(len(legend)): |
| 69 | bar.marker('E', '000000', '%s:%s' % ((len(values) + i*2), i), |
| 70 | '', '1:10') |
| 71 | bar.legend(*legend) |
| 72 | bar.color(*COLORS[:len(values)]) |
| 73 | bar.size(width, height) |
| 74 | |
| 75 | return str(bar) |
| 76 | |
| 77 | |
| 78 | def render_lines(): |
| 79 | line = Line([]) |
| 80 | line.dataset([[1,2,3], [3,2,1], [5,6,7]]) |
| 81 | scale_y = range(int(max([max(l) for l in |
| 82 | [[1,2,3], [3,2,1], [5,6,7]]]) + 2)) |
| 83 | line.axes.range(1, *scale_y) |
| 84 | # G.legend('Animals','Vegetables','Minerals') |
| 85 | # G.axes('y') |