1 ##############################################################################
3 # Copyright (c) 2004-2008 Tiny SPRL (http://tiny.be) All Rights Reserved.
7 # WARNING: This program as such is intended to be used by professional
8 # programmers who take the whole responsability of assessing all potential
9 # consequences resulting from its eventual inadequacies and bugs
10 # End users who are looking for a ready-to-use solution with commercial
11 # garantees and support are strongly adviced to contract a Free Software
14 # This program is Free Software; you can redistribute it and/or
15 # modify it under the terms of the GNU General Public License
16 # as published by the Free Software Foundation; either version 2
17 # of the License, or (at your option) any later version.
19 # This program is distributed in the hope that it will be useful,
20 # but WITHOUT ANY WARRANTY; without even the implied warranty of
21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 # GNU General Public License for more details.
24 # You should have received a copy of the GNU General Public License
25 # along with this program; if not, write to the Free Software
26 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 ###############################################################################
28 from mx.DateTime import *
32 from report.misc import choice_colors
35 # dRAw a graph for stocks
37 class stock_graph(object):
38 def __init__(self, io):
40 self._canvas = canvas.init(fname=io, format='pdf')
41 self._canvas.set_author("Tiny ERP")
46 def add(self, product_id, product_name, datas):
47 if hasattr(product_name, 'replace'):
48 product_name=product_name.replace('/', '//')
49 if product_id not in self._datas:
50 self._datas[product_id] = {}
51 self._names[product_id] = product_name
52 for (dt,stock) in datas:
53 if not dt in self._datas[product_id]:
54 self._datas[product_id][dt]=0
55 self._datas[product_id][dt]+=stock
57 self.val_min = min(self.val_min,dt)
60 self.val_max = max(self.val_max,dt)
63 colors = choice_colors(len(self._datas.keys()))
65 for user in self._datas.keys():
66 user_color[user] = colors.pop()
68 val_min = int(time.mktime(time.strptime(self.val_min,'%Y-%m-%d')))
69 val_max = int(time.mktime(time.strptime(self.val_max,'%Y-%m-%d')))
72 for product_id in self._datas:
73 f = fill_style.Plain()
74 f.bgcolor = user_color[user]
75 datas = self._datas[product_id].items()
76 datas = map(lambda x: (int(time.mktime(time.strptime(x[0],'%Y-%m-%d'))),x[1]), datas)
85 if datas2[-1][0]<d2-1000:
86 datas2.append((d2,datas2[-1][1]))
87 datas2.append((d[0],val))
88 if len(datas2) and datas2[-1][0]<val_max-100:
89 datas2.append((val_max, datas2[-1][1]))
91 datas2.append( (datas2[0][0]+100, datas2[0][1]) )
93 st.color = user_color[product_id]
97 plot = line_plot.T(label=self._names[product_id], data=datas2, line_style=st)
100 interval = max((val_max-val_min)/15, 86400)
101 x_axis = axis.X(format=lambda x:'/a60{}'+time.strftime('%Y-%m-%d',time.gmtime(x)), tic_interval=interval, label=None)
102 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)
105 ar.draw(self._canvas)
110 if __name__ == '__main__':
111 gt = stock_graph('test.pdf')
112 gt.add(1, 'Pomme', [('2005-07-29', 6), ('2005-07-30', -2), ('2005-07-31', 4)])
113 gt.add(2, 'Cailloux', [('2005-07-29', 9), ('2005-07-30', -4), ('2005-07-31', 2)])