~technofluid-team/openobject-addons/technofluid_multiple_installations

« back to all changes in this revision

Viewing changes to project/report/gantt.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 RelativeDateTime, now, DateTime, localtime
 
2
from pychart import *
 
3
import pychart.legend
 
4
from report.misc import choice_colors
 
5
 
 
6
#
 
7
# Draw a graph
 
8
 
9
class GanttCanvas(object):
 
10
        def __init__(self, io, convertors=(lambda x:x,lambda x:x)):
 
11
                self._datas = {}
 
12
                self._canvas = canvas.init(fname=io, format='pdf')
 
13
                self._canvas.set_author("Tiny ERP")
 
14
                self._names = {}
 
15
                self._conv = convertors
 
16
                self._min = 0
 
17
                self._max = 0
 
18
 
 
19
        def add(self, user, name, datas):
 
20
                if user not in self._datas:
 
21
                        self._datas[user] = []
 
22
                for f in datas:
 
23
                        x = map(self._conv[0], f)
 
24
                        if x[0]<self._min or not self._min:
 
25
                                self._min = x[0]
 
26
                        if x[1]>self._max or not self._max:
 
27
                                self._max = x[1]
 
28
                        self._datas[user].append( (name, x))
 
29
                        self._names.setdefault(name, x[0])
 
30
 
 
31
        def draw(self):
 
32
                colors = choice_colors(len(self._datas.keys()))
 
33
                user_color = {}
 
34
                for user in self._datas.keys():
 
35
                        user_color[user] = colors.pop()
 
36
 
 
37
                names = []
 
38
                for n in self._names:
 
39
                        names.append((self._names[n], n))
 
40
                names.sort()
 
41
                names.reverse()
 
42
                def _interval_get(*args):
 
43
                        result = []
 
44
                        for i in range(20):
 
45
                                d = localtime(self._min + (((self._max-self._min)/20)*(i+1)))
 
46
                                res = DateTime(d.year, d.month, d.day).ticks()
 
47
                                if (not result) or result[-1]<>res:
 
48
                                        result.append(res)
 
49
                        return result
 
50
 
 
51
                ar = area.T(y_coord = category_coord.T(names, 1),
 
52
                        x_grid_style=line_style.gray50_dash1,
 
53
                        x_grid_interval=_interval_get,
 
54
                        x_range = (self._min,self._max),
 
55
                        x_axis=axis.X(label="Date", format=self._conv[1]),
 
56
                        y_axis=axis.Y(label="Tasks"),
 
57
                        legend = legend.T(), size = (680,450))
 
58
 
 
59
                for user in self._datas:
 
60
                        chart_object.set_defaults(interval_bar_plot.T, direction="horizontal", data=self._datas[user])
 
61
                        f = fill_style.Plain()
 
62
                        f.bgcolor = user_color[user]
 
63
                        ar.add_plot(interval_bar_plot.T(fill_styles = [f, None], label=user, cluster=(0,1)))
 
64
 
 
65
                ar.draw(self._canvas)
 
66
 
 
67
        def close(self):
 
68
                self._canvas.close()
 
69
 
 
70
if __name__ == '__main__':
 
71
        date_to_int = lambda x: int(x.ticks())
 
72
        int_to_date = lambda x: '/a60{}'+localtime(x).strftime('%d %m %Y')
 
73
        gt = GanttCanvas('test.pdf', convertors=(date_to_int, int_to_date))
 
74
        gt.add('nicoe', 'Graphe de gantt', [(DateTime(2005,6,12), DateTime(2005,6,13))])
 
75
        gt.add('nicoe', 'Tarifs', [(DateTime(2005,6,19), DateTime(2005,6,21))])
 
76
        gt.add('gaetan', 'Calcul des prix', [(DateTime(2005,6,12), DateTime(2005,6,13))])
 
77
        gt.add('nico', 'Mise a jour du site', [(DateTime(2005,6,13), DateTime(2005,6,16))])
 
78
        gt.add('tom', 'Coucou', [(DateTime(2005,6,11), DateTime(2005,6,12))])
 
79
        gt.draw()
 
80
        gt.close()