1 # -*- encoding: utf-8 -*-
2 ##############################################################################
4 # Copyright (c) 2004-2008 Tiny SPRL (http://tiny.be) All Rights Reserved.
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
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 ###############################################################################
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) ]
38 return colorline
[0:-1:len(colorline
)/(n
+1)]
42 def tinygraph(subplot
, type='pie', axis
={}, axis_data
={}, datas
=[], axis_group_field
={}, orientation
='horizontal', overlap
=1.0):
47 'min': lambda x
,y
: min(x
,y
),
48 'max': lambda x
,y
: max(x
,y
),
49 '**': lambda x
,y
: x
**y
55 for field
in axis
[1:]:
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]], {})
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
])
68 data_all
[d
[axis
[0]]][group_eval
] = d
[field
]
69 data_axis
.append(data_all
)
70 axis_group
= axis_group
.keys()
77 font_property
= FontProperties(size
=8)
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
)
92 width
= 0.9 / (float(n
))
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')
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
)))
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:
118 if orientation
=='horizontal':
119 aa
= subplot
.barh(ind
, tuple(value
), width
, left
=yoff
, color
=color
, edgecolor
="#333333")[0]
121 aa
= subplot
.bar(ind
, tuple(value
), width
, bottom
=yoff
, color
=color
, edgecolor
="#333333")[0]
123 for j
in range(len(yoff
)):
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
)
132 t1
= [ axis_data
[x
]['string'] for x
in axis
[1:]]
133 subplot
.legend(gvalue
,t1
,shadow
=True,loc
='best',prop
= font_property
)
137 raise Exception, 'Graph type '+type+' does not exist !'
139 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: