blob: ec2a86fb80f92b4e02d1b3688bff7d90fdc807a4 [file] [log] [blame]
Yulia Portnovae3a49982015-02-16 16:06:09 +02001import hashlib
2import os
3import threading
4
Yulia Portnova6d72d7f2015-02-04 16:48:50 +02005from GChartWrapper import VerticalBarGroup
6from GChartWrapper import Line
7from GChartWrapper import constants
8
Yulia Portnovae3a49982015-02-16 16:06:09 +02009from config import CHARTS_IMG_PATH
10
Yulia Portnova6d72d7f2015-02-04 16:48:50 +020011
Yulia Portnova919f3be2015-02-06 12:49:22 +020012COLORS = ["1569C7", "81D8D0", "307D7E", "5CB3FF", "blue", "indigo"]
Yulia Portnova6d72d7f2015-02-04 16:48:50 +020013constants.MARKERS += 'E' # append E marker to available markers
14
15
Yulia Portnovae3a49982015-02-16 16:06:09 +020016def 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 Portnova6d72d7f2015-02-04 16:48:50 +020022def 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 Portnova0d2bd0a2015-02-11 17:42:44 +020072 bar.axes.type('xyy')
Yulia Portnova7b3b1d42015-02-13 14:08:35 +020073 bar.axes.label(2, None, 'Kbps')
Yulia Portnova6d72d7f2015-02-04 16:48:50 +020074 if scale_x:
75 bar.axes.label(0, *scale_x)
Yulia Portnova6d72d7f2015-02-04 16:48:50 +020076
Yulia Portnova919f3be2015-02-06 12:49:22 +020077 max_value = (max([max(l) for l in values + deviations]))
78 bar.axes.range(1, 0, max_value)
Yulia Portnova0d2bd0a2015-02-11 17:42:44 +020079 bar.axes.style(1, 'N*s*')
Yulia Portnovaffec7602015-02-12 11:16:11 +020080 bar.axes.style(2, '000000', '13')
Yulia Portnova0d2bd0a2015-02-11 17:42:44 +020081
Yulia Portnova919f3be2015-02-06 12:49:22 +020082 bar.scale(0, max_value)
Yulia Portnova0d2bd0a2015-02-11 17:42:44 +020083
Yulia Portnova6d72d7f2015-02-04 16:48:50 +020084 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 Portnovae3a49982015-02-16 16:06:09 +020092 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 Portnova6d72d7f2015-02-04 16:48:50 +020098
99
Yulia Portnova919f3be2015-02-06 12:49:22 +0200100def 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 Portnovae3a49982015-02-16 16:06:09 +0200113
Yulia Portnovae3a49982015-02-16 16:06:09 +0200114
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