~jgrandguillaume-c2c/openobject-addons/multi-company-cost-price

« back to all changes in this revision

Viewing changes to project/report/gantt.py

  • Committer: Joël Grand-Guillaume
  • Date: 2010-04-08 09:00:10 UTC
  • mfrom: (2533.3.664)
  • Revision ID: joel.grandguillaume@camptocamp.com-20100408090010-c0pqjan341s18bxs
[MRG] Merge from last trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# -*- coding: utf-8 -*-
2
 
##############################################################################
3
 
#    
4
 
#    OpenERP, Open Source Management Solution
5
 
#    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
6
 
#
7
 
#    This program is free software: you can redistribute it and/or modify
8
 
#    it under the terms of the GNU Affero General Public License as
9
 
#    published by the Free Software Foundation, either version 3 of the
10
 
#    License, or (at your option) any later version.
11
 
#
12
 
#    This program is distributed in the hope that it will be useful,
13
 
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 
#    GNU Affero General Public License for more details.
16
 
#
17
 
#    You should have received a copy of the GNU Affero General Public License
18
 
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.     
19
 
#
20
 
##############################################################################
21
 
from mx.DateTime import RelativeDateTime, now, DateTime, localtime
22
 
from pychart import *
23
 
import pychart.legend
24
 
from report.misc import choice_colors
25
 
 
26
 
#
27
 
# Draw a graph
28
 
29
 
class GanttCanvas(object):
30
 
    def __init__(self, io, convertors=(lambda x:x,lambda x:x)):
31
 
        self._datas = {}
32
 
        self._canvas = canvas.init(fname=io, format='pdf')
33
 
        self._canvas.set_author("Open ERP")
34
 
        self._names = {}
35
 
        self._conv = convertors
36
 
        self._min = 0
37
 
        self._max = 0
38
 
 
39
 
    def add(self, user, name, datas):
40
 
        if hasattr(user, 'replace'):
41
 
            user=user.replace('/', '//')
42
 
        if hasattr(name, 'replace'):
43
 
            name=name.replace('/', '//')
44
 
        name = name.encode('ascii', 'ignore')
45
 
        if user not in self._datas:
46
 
            self._datas[user] = []
47
 
        for f in datas:
48
 
            x = map(self._conv[0], f)
49
 
            if x[0]<self._min or not self._min:
50
 
                self._min = x[0]
51
 
            if x[1]>self._max or not self._max:
52
 
                self._max = x[1]
53
 
            self._datas[user].append( (name, x))
54
 
            self._names.setdefault(name, x[0])
55
 
 
56
 
    def draw(self):
57
 
        colors = choice_colors(len(self._datas.keys()))
58
 
        user_color = {}
59
 
        for user in self._datas.keys():
60
 
            user_color[user] = colors.pop()
61
 
 
62
 
        names = []
63
 
        for n in self._names:
64
 
            names.append((self._names[n], n))
65
 
        names.sort()
66
 
        names.reverse()
67
 
        def _interval_get(*args):
68
 
            result = []
69
 
            for i in range(20):
70
 
                d = localtime(self._min + (((self._max-self._min)/20)*(i+1)))
71
 
                res = DateTime(d.year, d.month, d.day).ticks()
72
 
                if (not result) or result[-1]<>res:
73
 
                    result.append(res)
74
 
            return result
75
 
 
76
 
        ar = area.T(y_coord = category_coord.T(names, 1),
77
 
            x_grid_style=line_style.gray50_dash1,
78
 
            x_grid_interval=_interval_get,
79
 
            x_range = (self._min,self._max),
80
 
            x_axis=axis.X(label="Date", format=self._conv[1]),
81
 
            y_axis=axis.Y(label="Tasks"),
82
 
            legend = legend.T(), size = (680,450))
83
 
 
84
 
        for user in self._datas:
85
 
            chart_object.set_defaults(interval_bar_plot.T, direction="horizontal", data=self._datas[user])
86
 
            f = fill_style.Plain()
87
 
            f.bgcolor = user_color[user]
88
 
            ar.add_plot(interval_bar_plot.T(fill_styles = [f, None], label=user, cluster=(0,1)))
89
 
 
90
 
        ar.draw(self._canvas)
91
 
 
92
 
    def close(self):
93
 
        self._canvas.close()
94
 
 
95
 
if __name__ == '__main__':
96
 
    date_to_int = lambda x: int(x.ticks())
97
 
    int_to_date = lambda x: '/a60{}'+localtime(x).strftime('%d %m %Y')
98
 
    gt = GanttCanvas('test.pdf', convertors=(date_to_int, int_to_date))
99
 
    gt.add('nicoe', 'Graphe de gantt', [(DateTime(2005,6,12), DateTime(2005,6,13))])
100
 
    gt.add('nicoe', 'Tarifs', [(DateTime(2005,6,19), DateTime(2005,6,21))])
101
 
    gt.add('gaetan', 'Calcul des prix', [(DateTime(2005,6,12), DateTime(2005,6,13))])
102
 
    gt.add('nico', 'Mise a jour du site', [(DateTime(2005,6,13), DateTime(2005,6,16))])
103
 
    gt.add('tom', 'Coucou', [(DateTime(2005,6,11), DateTime(2005,6,12))])
104
 
    gt.draw()
105
 
    gt.close()
106
 
 
107
 
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
108