Fix F1 bug in editable lists
[openerp-client.git] / bin / tinygraph / __init__.py
blob2dc56ac814fd5f654e426238f612db4954b60adb
1 # -*- encoding: utf-8 -*-
2 ##############################################################################
4 # Copyright (c) 2004-2008 Tiny SPRL (http://tiny.be) All Rights Reserved.
6 # $Id$
8 # WARNING: This program as such is intended to be used by professional
9 # programmers who take the whole responsability of assessing all potential
10 # consequences resulting from its eventual inadequacies and bugs
11 # End users who are looking for a ready-to-use solution with commercial
12 # garantees and support are strongly adviced to contract a Free Software
13 # Service Company
15 # This program is Free Software; you can redistribute it and/or
16 # modify it under the terms of the GNU General Public License
17 # as published by the Free Software Foundation; either version 2
18 # of the License, or (at your option) any later version.
20 # This program is distributed in the hope that it will be useful,
21 # but WITHOUT ANY WARRANTY; without even the implied warranty of
22 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 # GNU General Public License for more details.
25 # You should have received a copy of the GNU General Public License
26 # along with this program; if not, write to the Free Software
27 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
28 ###############################################################################
29 import matplotlib
30 matplotlib.use('GTKCairo')
32 from pylab import arange
33 from matplotlib.font_manager import FontProperties
35 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) ]
36 def choice_colors(n):
37 if n:
38 return colorline[0:-1:len(colorline)/(n+1)]
39 return []
42 def tinygraph(subplot, type='pie', axis={}, axis_data={}, datas=[], axis_group_field={}, orientation='horizontal', overlap=1.0):
43 subplot.clear()
44 operators = {
45 '+': lambda x,y: x+y,
46 '*': lambda x,y: x*y,
47 'min': lambda x,y: min(x,y),
48 'max': lambda x,y: max(x,y),
49 '**': lambda x,y: x**y
51 axis_group = {}
52 keys = {}
53 data_axis = []
54 data_all = {}
55 for field in axis[1:]:
56 data_all = {}
57 for d in datas:
58 group_eval = ','.join(map(lambda x: d[x], axis_group_field.keys()))
59 axis_group[group_eval] = 1
61 data_all.setdefault(d[axis[0]], {})
62 keys[d[axis[0]]] = 1
64 if group_eval in data_all[d[axis[0]]]:
65 oper = operators[axis_data[field].get('operator', '+')]
66 data_all[d[axis[0]]][group_eval] = oper(data_all[d[axis[0]]][group_eval], d[field])
67 else:
68 data_all[d[axis[0]]][group_eval] = d[field]
69 data_axis.append(data_all)
70 axis_group = axis_group.keys()
71 axis_group.sort()
72 keys = keys.keys()
73 keys.sort()
75 if not datas:
76 return False
77 font_property = FontProperties(size=8)
78 if type == 'pie':
79 labels = tuple(data_all.keys())
80 value = tuple(map(lambda x: reduce(lambda x,y=0: x+y, data_all[x].values(), 0), labels))
81 explode = map(lambda x: (x%4==2) and 0.06 or 0.0,range(len(value)))
82 colors = choice_colors(len(value))
83 aa = subplot.pie(value, autopct='%1.1f%%', shadow=True, explode=explode, colors=colors)
84 labels = map(lambda x: x.split('/')[-1], labels)
85 subplot.legend(aa, labels, shadow = True, loc = 'best', prop = font_property)
87 elif type == 'bar':
88 n = len(axis)-1
89 gvalue = []
90 gvalue2 = []
91 if float(n):
92 width = 0.9 / (float(n))
93 else:
94 width = 0.9
95 ind = map(lambda x: x+width*n/2, arange(len(keys)))
96 if orientation=='horizontal':
97 subplot.set_yticks(ind)
98 subplot.set_yticklabels(tuple(keys), visible=True, ha='right', size=8)
99 subplot.xaxis.grid(True,'major',linestyle='-',color='gray')
100 else:
101 subplot.set_xticks(ind)
102 subplot.set_xticklabels(tuple(keys), visible=True, ha='right', size=8, rotation='vertical')
103 subplot.yaxis.grid(True,'major',linestyle='-',color='gray')
105 colors = choice_colors(max(n,len(axis_group)))
106 for i in range(n):
107 datas = data_axis[i]
108 ind = map(lambda x: x+width*i*overlap+((1.0-overlap)*n*width)/4, arange(len(keys)))
109 #ind = map(lambda x: x, arange(len(keys)))
110 yoff = map(lambda x:0.0, keys)
112 for y in range(len(axis_group)):
113 value = [ datas[x].get(axis_group[y],0.0) for x in keys]
114 if len(axis_group)>1:
115 color = colors[y]
116 else:
117 color = colors[i]
118 if orientation=='horizontal':
119 aa = subplot.barh(ind, tuple(value), width, left=yoff, color=color, edgecolor="#333333")[0]
120 else:
121 aa = subplot.bar(ind, tuple(value), width, bottom=yoff, color=color, edgecolor="#333333")[0]
122 gvalue2.append(aa)
123 for j in range(len(yoff)):
124 yoff[j]+=value[j]
125 gvalue.append(aa)
127 if True:
128 if len(axis_group)>1:
129 axis_group = map(lambda x: x.split('/')[-1], axis_group)
130 subplot.legend(gvalue2,axis_group,shadow=True,loc='best',prop = font_property)
131 else:
132 t1 = [ axis_data[x]['string'] for x in axis[1:]]
133 subplot.legend(gvalue,t1,shadow=True,loc='best',prop = font_property)
134 else:
135 pass
136 else:
137 raise Exception, 'Graph type '+type+' does not exist !'
139 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: