author | Adrien Di Mascio <Adrien.DiMascio@logilab.fr> |
Wed, 20 May 2009 22:28:45 +0200 | |
changeset 1886 | f0e28ddba7c5 |
parent 1133 | 8a409ea0c9ec |
child 1888 | f36d43f00f32 |
permissions | -rw-r--r-- |
1886
f0e28ddba7c5
[views] add pie chart views with google chart / GChartWrapper
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
1 |
"""basic plot views |
f0e28ddba7c5
[views] add pie chart views with google chart / GChartWrapper
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
2 |
|
f0e28ddba7c5
[views] add pie chart views with google chart / GChartWrapper
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
3 |
:organization: Logilab |
f0e28ddba7c5
[views] add pie chart views with google chart / GChartWrapper
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
4 |
:copyright: 2007-2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL. |
f0e28ddba7c5
[views] add pie chart views with google chart / GChartWrapper
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
5 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
f0e28ddba7c5
[views] add pie chart views with google chart / GChartWrapper
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
6 |
""" |
f0e28ddba7c5
[views] add pie chart views with google chart / GChartWrapper
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
7 |
__docformat__ = "restructuredtext en" |
f0e28ddba7c5
[views] add pie chart views with google chart / GChartWrapper
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
8 |
|
0 | 9 |
import os |
10 |
||
11 |
from logilab.common import flatten |
|
1886
f0e28ddba7c5
[views] add pie chart views with google chart / GChartWrapper
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
12 |
from logilab.mtconverter import html_escape |
0 | 13 |
|
742
99115e029dca
replaced most of __selectors__ assignments with __select__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
14 |
from cubicweb.vregistry import objectify_selector |
0 | 15 |
from cubicweb.web.views import baseviews |
16 |
||
742
99115e029dca
replaced most of __selectors__ assignments with __select__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
17 |
@objectify_selector |
0 | 18 |
def plot_selector(cls, req, rset, *args, **kwargs): |
19 |
"""accept result set with at least one line and two columns of result |
|
20 |
all columns after second must be of numerical types""" |
|
1886
f0e28ddba7c5
[views] add pie chart views with google chart / GChartWrapper
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
21 |
if not rset: |
0 | 22 |
return 0 |
23 |
if len(rset.rows[0]) < 2: |
|
24 |
return 0 |
|
25 |
for etype in rset.description[0]: |
|
26 |
if etype not in ('Int', 'Float'): |
|
27 |
return 0 |
|
28 |
return 1 |
|
29 |
||
1886
f0e28ddba7c5
[views] add pie chart views with google chart / GChartWrapper
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
30 |
@objectify_selector |
f0e28ddba7c5
[views] add pie chart views with google chart / GChartWrapper
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
31 |
def piechart_selector(cls, req, rset, *args, **kwargs): |
f0e28ddba7c5
[views] add pie chart views with google chart / GChartWrapper
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
32 |
if not rset: |
f0e28ddba7c5
[views] add pie chart views with google chart / GChartWrapper
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
33 |
return 0 |
f0e28ddba7c5
[views] add pie chart views with google chart / GChartWrapper
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
34 |
if len(rset.rows[0]) < 2: |
f0e28ddba7c5
[views] add pie chart views with google chart / GChartWrapper
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
35 |
return 0 |
f0e28ddba7c5
[views] add pie chart views with google chart / GChartWrapper
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
36 |
etype = rset.description[0][1] |
f0e28ddba7c5
[views] add pie chart views with google chart / GChartWrapper
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
37 |
if etype not in ('Int', 'Float'): |
f0e28ddba7c5
[views] add pie chart views with google chart / GChartWrapper
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
38 |
return 0 |
f0e28ddba7c5
[views] add pie chart views with google chart / GChartWrapper
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
39 |
return 1 |
f0e28ddba7c5
[views] add pie chart views with google chart / GChartWrapper
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
40 |
|
0 | 41 |
try: |
42 |
import matplotlib |
|
43 |
import sys |
|
44 |
if 'matplotlib.backends' not in sys.modules: |
|
45 |
matplotlib.use('Agg') |
|
1132 | 46 |
from pylab import figure |
0 | 47 |
except ImportError: |
48 |
pass |
|
49 |
else: |
|
50 |
class PlotView(baseviews.AnyRsetView): |
|
51 |
id = 'plot' |
|
52 |
title = _('generic plot') |
|
53 |
binary = True |
|
54 |
content_type = 'image/png' |
|
55 |
_plot_count = 0 |
|
742
99115e029dca
replaced most of __selectors__ assignments with __select__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
56 |
__select__ = plot_selector() |
0 | 57 |
|
58 |
def call(self, width=None, height=None): |
|
59 |
# compute dimensions |
|
60 |
if width is None: |
|
61 |
if 'width' in self.req.form: |
|
62 |
width = int(self.req.form['width']) |
|
63 |
else: |
|
64 |
width = 500 |
|
65 |
||
66 |
if height is None: |
|
67 |
if 'height' in self.req.form: |
|
68 |
height = int(self.req.form['height']) |
|
69 |
else: |
|
70 |
height = 400 |
|
71 |
dpi = 100. |
|
72 |
||
73 |
# compute data |
|
74 |
abscisses = [row[0] for row in self.rset] |
|
75 |
courbes = [] |
|
76 |
nbcols = len(self.rset.rows[0]) |
|
1133 | 77 |
for col in xrange(1, nbcols): |
0 | 78 |
courbe = [row[col] for row in self.rset] |
79 |
courbes.append(courbe) |
|
80 |
if not courbes: |
|
81 |
raise Exception('no data') |
|
82 |
# plot data |
|
83 |
fig = figure(figsize=(width/dpi, height/dpi), dpi=dpi) |
|
84 |
ax = fig.add_subplot(111) |
|
85 |
colors = 'brgybrgy' |
|
86 |
try: |
|
87 |
float(abscisses[0]) |
|
88 |
xlabels = None |
|
89 |
except ValueError: |
|
90 |
xlabels = abscisses |
|
91 |
abscisses = range(len(xlabels)) |
|
1133 | 92 |
for idx, courbe in enumerate(courbes): |
0 | 93 |
ax.plot(abscisses, courbe, '%sv-' % colors[idx], label=self.rset.description[0][idx+1]) |
94 |
ax.autoscale_view() |
|
95 |
alldata = flatten(courbes) |
|
96 |
m, M = min(alldata or [0]), max(alldata or [1]) |
|
97 |
if m is None: m = 0 |
|
98 |
if M is None: M = 0 |
|
99 |
margin = float(M-m)/10 |
|
100 |
ax.set_ylim(m-margin, M+margin) |
|
101 |
ax.grid(True) |
|
102 |
ax.legend(loc='best') |
|
103 |
if xlabels is not None: |
|
104 |
ax.set_xticks(abscisses) |
|
105 |
ax.set_xticklabels(xlabels) |
|
106 |
try: |
|
107 |
fig.autofmt_xdate() |
|
108 |
except AttributeError: |
|
109 |
# XXX too old version of matplotlib. Ignore safely. |
|
110 |
pass |
|
111 |
||
112 |
# save plot |
|
113 |
filename = self.build_figname() |
|
114 |
fig.savefig(filename, dpi=100) |
|
115 |
img = open(filename, 'rb') |
|
116 |
self.w(img.read()) |
|
117 |
img.close() |
|
118 |
os.remove(filename) |
|
119 |
||
120 |
def build_figname(self): |
|
121 |
self.__class__._plot_count += 1 |
|
1886
f0e28ddba7c5
[views] add pie chart views with google chart / GChartWrapper
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
122 |
return '/tmp/burndown_chart_%s_%d.png' % (self.config.appid, |
f0e28ddba7c5
[views] add pie chart views with google chart / GChartWrapper
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
123 |
self.__class__._plot_count) |
f0e28ddba7c5
[views] add pie chart views with google chart / GChartWrapper
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
124 |
|
f0e28ddba7c5
[views] add pie chart views with google chart / GChartWrapper
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
125 |
try: |
f0e28ddba7c5
[views] add pie chart views with google chart / GChartWrapper
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
126 |
from GChartWrapper import Pie, Pie3D |
f0e28ddba7c5
[views] add pie chart views with google chart / GChartWrapper
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
127 |
except ImportError: |
f0e28ddba7c5
[views] add pie chart views with google chart / GChartWrapper
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
128 |
pass |
f0e28ddba7c5
[views] add pie chart views with google chart / GChartWrapper
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
129 |
else: |
f0e28ddba7c5
[views] add pie chart views with google chart / GChartWrapper
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
130 |
class PieChartView(baseviews.AnyRsetView): |
f0e28ddba7c5
[views] add pie chart views with google chart / GChartWrapper
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
131 |
id = 'piechart' |
f0e28ddba7c5
[views] add pie chart views with google chart / GChartWrapper
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
132 |
pieclass = Pie |
f0e28ddba7c5
[views] add pie chart views with google chart / GChartWrapper
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
133 |
__select__ = piechart_selector() |
f0e28ddba7c5
[views] add pie chart views with google chart / GChartWrapper
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
134 |
|
f0e28ddba7c5
[views] add pie chart views with google chart / GChartWrapper
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
135 |
def call(self, title=None, width=None, height=None): |
f0e28ddba7c5
[views] add pie chart views with google chart / GChartWrapper
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
136 |
piechart = self.pieclass([(row[1] or 0) for row in self.rset]) |
f0e28ddba7c5
[views] add pie chart views with google chart / GChartWrapper
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
137 |
labels = ['%s: %s' % (row[0].encode(self.req.encoding), row[1]) |
f0e28ddba7c5
[views] add pie chart views with google chart / GChartWrapper
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
138 |
for row in self.rset] |
f0e28ddba7c5
[views] add pie chart views with google chart / GChartWrapper
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
139 |
piechart.label(*labels) |
f0e28ddba7c5
[views] add pie chart views with google chart / GChartWrapper
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
140 |
if width is not None: |
f0e28ddba7c5
[views] add pie chart views with google chart / GChartWrapper
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
141 |
height = height or width |
f0e28ddba7c5
[views] add pie chart views with google chart / GChartWrapper
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
142 |
piechart.size(width, height) |
f0e28ddba7c5
[views] add pie chart views with google chart / GChartWrapper
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
143 |
if title: |
f0e28ddba7c5
[views] add pie chart views with google chart / GChartWrapper
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
144 |
piechart.title(title) |
f0e28ddba7c5
[views] add pie chart views with google chart / GChartWrapper
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
145 |
self.w(u'<img src="%s" />' % html_escape(piechart.url)) |
f0e28ddba7c5
[views] add pie chart views with google chart / GChartWrapper
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
146 |
|
f0e28ddba7c5
[views] add pie chart views with google chart / GChartWrapper
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
147 |
|
f0e28ddba7c5
[views] add pie chart views with google chart / GChartWrapper
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
148 |
class PieChart3DView(PieChartView): |
f0e28ddba7c5
[views] add pie chart views with google chart / GChartWrapper
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
149 |
id = 'piechart3D' |
f0e28ddba7c5
[views] add pie chart views with google chart / GChartWrapper
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
150 |
pieclass = Pie3D |