Fix slash in pychart
[odoo/odoo.git] / addons / stock / report / stock_graph.py
1 from mx.DateTime import *
2 from pychart import *
3 import pychart.legend
4 import time
5 from report.misc import choice_colors
6
7 #
8 # dRAw a graph for stocks
9 #
10 class stock_graph(object):
11         def __init__(self, io):
12                 self._datas = {}
13                 self._canvas = canvas.init(fname=io, format='pdf')
14                 self._canvas.set_author("Tiny ERP")
15                 self._names = {}
16                 self.val_min = ''
17                 self.val_max = ''
18
19         def add(self, product_id, product_name, datas):
20                 if hasattr(product_name, 'replace'):
21                         product_name=product_name.replace('/', '//')
22                 if product_id not in self._datas:
23                         self._datas[product_id] = {}
24                 self._names[product_id] = product_name
25                 for (dt,stock) in datas:
26                         if not dt in self._datas[product_id]:
27                                 self._datas[product_id][dt]=0
28                         self._datas[product_id][dt]+=stock
29                         if self.val_min:
30                                 self.val_min = min(self.val_min,dt)
31                         else:
32                                 self.val_min = dt
33                         self.val_max = max(self.val_max,dt)
34
35         def draw(self):
36                 colors = choice_colors(len(self._datas.keys()))
37                 user_color = {}
38                 for user in self._datas.keys():
39                         user_color[user] = colors.pop()
40
41                 val_min = int(time.mktime(time.strptime(self.val_min,'%Y-%m-%d')))
42                 val_max = int(time.mktime(time.strptime(self.val_max,'%Y-%m-%d')))
43
44                 plots = []
45                 for product_id in self._datas:
46                         f = fill_style.Plain()
47                         f.bgcolor = user_color[user]
48                         datas = self._datas[product_id].items()
49                         datas = map(lambda x: (int(time.mktime(time.strptime(x[0],'%Y-%m-%d'))),x[1]), datas)
50                         datas.sort()
51                         datas2 = []
52                         val = 0
53                         for d in datas:
54                                 val+=d[1]
55
56                                 if len(datas2):
57                                         d2 = d[0]-60*61*24
58                                         if datas2[-1][0]<d2-1000:
59                                                 datas2.append((d2,datas2[-1][1]))
60                                 datas2.append((d[0],val))
61                         if len(datas2) and datas2[-1][0]<val_max-100:
62                                 datas2.append((val_max, datas2[-1][1]))
63                         if len(datas2)==1:
64                                 datas2.append( (datas2[0][0]+100, datas2[0][1]) )
65                         st = line_style.T()
66                         st.color = user_color[product_id]
67                         st.width = 1
68                         st.cap_style=1
69                         st.join_style=1
70                         plot = line_plot.T(label=self._names[product_id], data=datas2, line_style=st)
71                         plots.append(plot)
72
73                 interval = max((val_max-val_min)/15, 86400)
74                 x_axis = axis.X(format=lambda x:'/a60{}'+time.strftime('%Y-%m-%d',time.gmtime(x)), tic_interval=interval, label=None)
75                 ar = area.T(size = (620,435), x_range=(val_min,val_max+1), y_axis = axis.Y(format="%d", label="Virtual Stock (Unit)"), x_axis=x_axis)
76                 for plot in plots:
77                         ar.add_plot(plot)
78                 ar.draw(self._canvas)
79
80         def close(self):
81                 self._canvas.close()
82
83 if __name__ == '__main__':
84         gt = stock_graph('test.pdf')
85         gt.add(1, 'Pomme', [('2005-07-29', 6), ('2005-07-30', -2), ('2005-07-31', 4)])
86         gt.add(2, 'Cailloux', [('2005-07-29', 9), ('2005-07-30', -4), ('2005-07-31', 2)])
87         gt.draw()
88         gt.close()