1
# -*- encoding: utf-8 -*-
2
##############################################################################
4
# OpenERP, Open Source Management Solution
5
# Copyright (C) 2004-2008 Tiny SPRL (<http://tiny.be>). All Rights Reserved
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.
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.
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/>.
21
##############################################################################
23
matplotlib.use('GTKCairo')
25
from pylab import arange
26
from matplotlib.font_manager import FontProperties
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) ]
31
return colorline[0:-1:len(colorline)/(n+1)]
35
def tinygraph(subplot, type='pie', axis={}, axis_data={}, datas=[], axis_group_field={}, orientation='horizontal', overlap=1.0):
40
'min': lambda x,y: min(x,y),
41
'max': lambda x,y: max(x,y),
42
'**': lambda x,y: x**y
48
for field in axis[1:]:
51
group_eval = ','.join(map(lambda x: d[x], axis_group_field.keys()))
52
axis_group[group_eval] = 1
54
data_all.setdefault(d[axis[0]], {})
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])
61
data_all[d[axis[0]]][group_eval] = d[field]
62
data_axis.append(data_all)
63
axis_group = axis_group.keys()
70
font_property = FontProperties(size=8)
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)
85
width = 0.9 / (float(n))
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')
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')
98
colors = choice_colors(max(n,len(axis_group)))
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)
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:
111
if orientation=='horizontal':
112
aa = subplot.barh(ind, tuple(value), width, left=yoff, color=color, edgecolor="#333333")[0]
114
aa = subplot.bar(ind, tuple(value), width, bottom=yoff, color=color, edgecolor="#333333")[0]
116
for j in range(len(yoff)):
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)
125
t1 = [ axis_data[x]['string'] for x in axis[1:]]
126
subplot.legend(gvalue,t1,shadow=True,loc='best',prop = font_property)
130
raise Exception, 'Graph type '+type+' does not exist !'
132
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: