~ubuntu-branches/ubuntu/trusty/openerp-client/trusty

« back to all changes in this revision

Viewing changes to bin/tinygraph/__init__.py

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Baumann
  • Date: 2008-12-07 20:17:00 UTC
  • Revision ID: james.westby@ubuntu.com-20081207201700-a875pic3sd7xkoru
Tags: upstream-5.0.0~alpha
ImportĀ upstreamĀ versionĀ 5.0.0~alpha

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- encoding: utf-8 -*-
 
2
##############################################################################
 
3
#
 
4
#    OpenERP, Open Source Management Solution   
 
5
#    Copyright (C) 2004-2008 Tiny SPRL (<http://tiny.be>). All Rights Reserved
 
6
#    $Id$
 
7
#
 
8
#    This program is free software: you can redistribute it and/or modify
 
9
#    it under the terms of the GNU General Public License as published by
 
10
#    the Free Software Foundation, either version 3 of the License, or
 
11
#    (at your option) any later version.
 
12
#
 
13
#    This program is distributed in the hope that it will be useful,
 
14
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
#    GNU General Public License for more details.
 
17
#
 
18
#    You should have received a copy of the GNU General Public License
 
19
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
20
#
 
21
##############################################################################
 
22
import matplotlib
 
23
matplotlib.use('GTKCairo')
 
24
 
 
25
from pylab import arange
 
26
from matplotlib.font_manager import FontProperties
 
27
 
 
28
colorline = ['#%02x%02x%02x' % (25+((r+10)%11)*23,5+((g+1)%11)*20,25+((b+4)%11)*23) for r in range(11) for g in range(11) for b in range(11) ]
 
29
def choice_colors(n):
 
30
    if n:
 
31
        return colorline[0:-1:len(colorline)/(n+1)]
 
32
    return []
 
33
 
 
34
 
 
35
def tinygraph(subplot, type='pie', axis={}, axis_data={}, datas=[], axis_group_field={}, orientation='horizontal', overlap=1.0):
 
36
    subplot.clear()
 
37
    operators = {
 
38
        '+': lambda x,y: x+y,
 
39
        '*': lambda x,y: x*y,
 
40
        'min': lambda x,y: min(x,y),
 
41
        'max': lambda x,y: max(x,y),
 
42
        '**': lambda x,y: x**y
 
43
    }
 
44
    axis_group = {}
 
45
    keys = {}
 
46
    data_axis = []
 
47
    data_all = {}
 
48
    for field in axis[1:]:
 
49
        data_all = {}
 
50
        for d in datas:
 
51
            group_eval = ','.join(map(lambda x: d[x], axis_group_field.keys()))
 
52
            axis_group[group_eval] = 1
 
53
 
 
54
            data_all.setdefault(d[axis[0]], {})
 
55
            keys[d[axis[0]]] = 1
 
56
 
 
57
            if group_eval in  data_all[d[axis[0]]]:
 
58
                oper = operators[axis_data[field].get('operator', '+')]
 
59
                data_all[d[axis[0]]][group_eval] = oper(data_all[d[axis[0]]][group_eval], d[field])
 
60
            else:
 
61
                data_all[d[axis[0]]][group_eval] = d[field]
 
62
        data_axis.append(data_all)
 
63
    axis_group = axis_group.keys()
 
64
    axis_group.sort()
 
65
    keys = keys.keys()
 
66
    keys.sort()
 
67
 
 
68
    if not datas:
 
69
        return False
 
70
    font_property = FontProperties(size=8)
 
71
    if type == 'pie':
 
72
        labels = tuple(data_all.keys())
 
73
        value = tuple(map(lambda x: reduce(lambda x,y=0: x+y, data_all[x].values(), 0), labels))
 
74
        explode = map(lambda x: (x%4==2) and 0.06 or 0.0,range(len(value)))
 
75
        colors = choice_colors(len(value))
 
76
        aa = subplot.pie(value, autopct='%1.1f%%', shadow=True, explode=explode, colors=colors)
 
77
        labels = map(lambda x: x.split('/')[-1], labels)
 
78
        subplot.legend(aa, labels, shadow = True, loc = 'best', prop = font_property)
 
79
 
 
80
    elif type == 'bar':
 
81
        n = len(axis)-1
 
82
        gvalue = []
 
83
        gvalue2 = []
 
84
        if float(n):
 
85
            width =  0.9 / (float(n))
 
86
        else:
 
87
            width = 0.9
 
88
        ind = map(lambda x: x+width*n/2, arange(len(keys)))
 
89
        if orientation=='horizontal':
 
90
            subplot.set_yticks(ind)
 
91
            subplot.set_yticklabels(tuple(keys), visible=True, ha='right', size=8)
 
92
            subplot.xaxis.grid(True,'major',linestyle='-',color='gray')
 
93
        else:
 
94
            subplot.set_xticks(ind)
 
95
            subplot.set_xticklabels(tuple(keys), visible=True, ha='right', size=8, rotation='vertical')
 
96
            subplot.yaxis.grid(True,'major',linestyle='-',color='gray')
 
97
 
 
98
        colors = choice_colors(max(n,len(axis_group)))
 
99
        for i in range(n):
 
100
            datas = data_axis[i]
 
101
            ind = map(lambda x: x+width*i*overlap+((1.0-overlap)*n*width)/4, arange(len(keys)))
 
102
            #ind = map(lambda x: x, arange(len(keys)))
 
103
            yoff = map(lambda x:0.0, keys)
 
104
 
 
105
            for y in range(len(axis_group)):
 
106
                value = [ datas[x].get(axis_group[y],0.0) for x in keys]
 
107
                if len(axis_group)>1:
 
108
                    color = colors[y]
 
109
                else:
 
110
                    color = colors[i]
 
111
                if orientation=='horizontal':
 
112
                    aa = subplot.barh(ind, tuple(value), width, left=yoff, color=color, edgecolor="#333333")[0]
 
113
                else:
 
114
                    aa = subplot.bar(ind, tuple(value), width, bottom=yoff, color=color, edgecolor="#333333")[0]
 
115
                gvalue2.append(aa)
 
116
                for j in range(len(yoff)):
 
117
                    yoff[j]+=value[j]
 
118
            gvalue.append(aa)
 
119
 
 
120
        if True:
 
121
            if len(axis_group)>1:
 
122
                axis_group = map(lambda x: x.split('/')[-1], axis_group)
 
123
                subplot.legend(gvalue2,axis_group,shadow=True,loc='best',prop = font_property)
 
124
            else:
 
125
                t1 = [ axis_data[x]['string'] for x in axis[1:]]
 
126
                subplot.legend(gvalue,t1,shadow=True,loc='best',prop = font_property)
 
127
        else:
 
128
            pass
 
129
    else:
 
130
        raise Exception, 'Graph type '+type+' does not exist !'
 
131
 
 
132
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
 
133