~technofluid-team/openobject-addons/technofluid_multiple_installations

« back to all changes in this revision

Viewing changes to stock/report/stock_graph.py

  • Committer: pinky
  • Date: 2006-12-07 13:41:40 UTC
  • Revision ID: pinky-dedd7f8a42bd4557112a0513082691b8590ad6cc
New trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 product_id not in self._datas:
 
21
                        self._datas[product_id] = {}
 
22
                self._names[product_id] = product_name
 
23
                for (dt,stock) in datas:
 
24
                        if not dt in self._datas[product_id]:
 
25
                                self._datas[product_id][dt]=0
 
26
                        self._datas[product_id][dt]+=stock
 
27
                        if self.val_min:
 
28
                                self.val_min = min(self.val_min,dt)
 
29
                        else:
 
30
                                self.val_min = dt
 
31
                        self.val_max = max(self.val_max,dt)
 
32
 
 
33
        def draw(self):
 
34
                colors = choice_colors(len(self._datas.keys()))
 
35
                user_color = {}
 
36
                for user in self._datas.keys():
 
37
                        user_color[user] = colors.pop()
 
38
 
 
39
                val_min = int(time.mktime(time.strptime(self.val_min,'%Y-%m-%d')))
 
40
                val_max = int(time.mktime(time.strptime(self.val_max,'%Y-%m-%d')))
 
41
 
 
42
                plots = []
 
43
                for product_id in self._datas:
 
44
                        f = fill_style.Plain()
 
45
                        f.bgcolor = user_color[user]
 
46
                        datas = self._datas[product_id].items()
 
47
                        datas = map(lambda x: (int(time.mktime(time.strptime(x[0],'%Y-%m-%d'))),x[1]), datas)
 
48
                        datas.sort()
 
49
                        datas2 = []
 
50
                        val = 0
 
51
                        for d in datas:
 
52
                                val+=d[1]
 
53
 
 
54
                                if len(datas2):
 
55
                                        d2 = d[0]-60*61*24
 
56
                                        if datas2[-1][0]<d2-1000:
 
57
                                                datas2.append((d2,datas2[-1][1]))
 
58
                                datas2.append((d[0],val))
 
59
                        if len(datas2) and datas2[-1][0]<val_max-100:
 
60
                                datas2.append((val_max, datas2[-1][1]))
 
61
                        if len(datas2)==1:
 
62
                                datas2.append( (datas2[0][0]+100, datas2[0][1]) )
 
63
                        st = line_style.T()
 
64
                        st.color = user_color[product_id]
 
65
                        st.width = 1
 
66
                        st.cap_style=1
 
67
                        st.join_style=1
 
68
                        plot = line_plot.T(label=self._names[product_id], data=datas2, line_style=st)
 
69
                        plots.append(plot)
 
70
 
 
71
                interval = max((val_max-val_min)/15, 86400)
 
72
                x_axis = axis.X(format=lambda x:'/a60{}'+time.strftime('%Y-%m-%d',time.gmtime(x)), tic_interval=interval, label=None)
 
73
                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)
 
74
                for plot in plots:
 
75
                        ar.add_plot(plot)
 
76
                ar.draw(self._canvas)
 
77
 
 
78
        def close(self):
 
79
                self._canvas.close()
 
80
 
 
81
if __name__ == '__main__':
 
82
        gt = stock_graph('test.pdf')
 
83
        gt.add(1, 'Pomme', [('2005-07-29', 6), ('2005-07-30', -2), ('2005-07-31', 4)])
 
84
        gt.add(2, 'Cailloux', [('2005-07-29', 9), ('2005-07-30', -4), ('2005-07-31', 2)])
 
85
        gt.draw()
 
86
        gt.close()