author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Mon, 21 Dec 2009 18:43:16 +0100 | |
changeset 4151 | 66fe38345a65 |
parent 4045 | f4a52abb6f4f |
child 4252 | 6c4f109c2b03 |
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 |
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1956
diff
changeset
|
6 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
1886
f0e28ddba7c5
[views] add pie chart views with google chart / GChartWrapper
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
7 |
""" |
f0e28ddba7c5
[views] add pie chart views with google chart / GChartWrapper
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
8 |
__docformat__ = "restructuredtext en" |
f0e28ddba7c5
[views] add pie chart views with google chart / GChartWrapper
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
9 |
|
0 | 10 |
import os |
1888
f36d43f00f32
[views] plot with flot and get rid of matplotlib
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1886
diff
changeset
|
11 |
import time |
f36d43f00f32
[views] plot with flot and get rid of matplotlib
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1886
diff
changeset
|
12 |
|
f36d43f00f32
[views] plot with flot and get rid of matplotlib
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1886
diff
changeset
|
13 |
from simplejson import dumps |
0 | 14 |
|
15 |
from logilab.common import flatten |
|
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2161
diff
changeset
|
16 |
from logilab.mtconverter import xml_escape |
0 | 17 |
|
2009
b1e2b9e4c809
[utils] move datetime2ticks from cw.web.views.plots to cw.utils
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1991
diff
changeset
|
18 |
from cubicweb.utils import make_uid, UStringIO, datetime2ticks |
2657
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2312
diff
changeset
|
19 |
from cubicweb.appobject import objectify_selector |
0 | 20 |
from cubicweb.web.views import baseviews |
21 |
||
742
99115e029dca
replaced most of __selectors__ assignments with __select__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
22 |
@objectify_selector |
2161
200481e7b156
prepare time where it won't be mandatory to give rset to select()
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2009
diff
changeset
|
23 |
def at_least_two_columns(cls, req, rset=None, *args, **kwargs): |
1888
f36d43f00f32
[views] plot with flot and get rid of matplotlib
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1886
diff
changeset
|
24 |
if not rset: |
f36d43f00f32
[views] plot with flot and get rid of matplotlib
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1886
diff
changeset
|
25 |
return 0 |
f36d43f00f32
[views] plot with flot and get rid of matplotlib
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1886
diff
changeset
|
26 |
return len(rset.rows[0]) >= 2 |
f36d43f00f32
[views] plot with flot and get rid of matplotlib
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1886
diff
changeset
|
27 |
|
f36d43f00f32
[views] plot with flot and get rid of matplotlib
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1886
diff
changeset
|
28 |
@objectify_selector |
2161
200481e7b156
prepare time where it won't be mandatory to give rset to select()
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2009
diff
changeset
|
29 |
def all_columns_are_numbers(cls, req, rset=None, *args, **kwargs): |
0 | 30 |
"""accept result set with at least one line and two columns of result |
31 |
all columns after second must be of numerical types""" |
|
32 |
for etype in rset.description[0]: |
|
33 |
if etype not in ('Int', 'Float'): |
|
34 |
return 0 |
|
35 |
return 1 |
|
36 |
||
1886
f0e28ddba7c5
[views] add pie chart views with google chart / GChartWrapper
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
37 |
@objectify_selector |
2161
200481e7b156
prepare time where it won't be mandatory to give rset to select()
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2009
diff
changeset
|
38 |
def second_column_is_number(cls, req, rset=None, *args, **kwargs): |
1886
f0e28ddba7c5
[views] add pie chart views with google chart / GChartWrapper
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
39 |
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
|
40 |
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
|
41 |
return 0 |
f0e28ddba7c5
[views] add pie chart views with google chart / GChartWrapper
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
42 |
return 1 |
f0e28ddba7c5
[views] add pie chart views with google chart / GChartWrapper
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
43 |
|
1888
f36d43f00f32
[views] plot with flot and get rid of matplotlib
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1886
diff
changeset
|
44 |
@objectify_selector |
2161
200481e7b156
prepare time where it won't be mandatory to give rset to select()
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2009
diff
changeset
|
45 |
def columns_are_date_then_numbers(cls, req, rset=None, *args, **kwargs): |
1888
f36d43f00f32
[views] plot with flot and get rid of matplotlib
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1886
diff
changeset
|
46 |
etypes = rset.description[0] |
f36d43f00f32
[views] plot with flot and get rid of matplotlib
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1886
diff
changeset
|
47 |
if etypes[0] not in ('Date', 'Datetime'): |
f36d43f00f32
[views] plot with flot and get rid of matplotlib
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1886
diff
changeset
|
48 |
return 0 |
f36d43f00f32
[views] plot with flot and get rid of matplotlib
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1886
diff
changeset
|
49 |
for etype in etypes[1:]: |
f36d43f00f32
[views] plot with flot and get rid of matplotlib
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1886
diff
changeset
|
50 |
if etype not in ('Int', 'Float'): |
f36d43f00f32
[views] plot with flot and get rid of matplotlib
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1886
diff
changeset
|
51 |
return 0 |
f36d43f00f32
[views] plot with flot and get rid of matplotlib
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1886
diff
changeset
|
52 |
return 1 |
f36d43f00f32
[views] plot with flot and get rid of matplotlib
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1886
diff
changeset
|
53 |
|
0 | 54 |
|
1888
f36d43f00f32
[views] plot with flot and get rid of matplotlib
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1886
diff
changeset
|
55 |
def filterout_nulls(abscissa, plot): |
f36d43f00f32
[views] plot with flot and get rid of matplotlib
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1886
diff
changeset
|
56 |
filtered = [] |
f36d43f00f32
[views] plot with flot and get rid of matplotlib
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1886
diff
changeset
|
57 |
for x, y in zip(abscissa, plot): |
f36d43f00f32
[views] plot with flot and get rid of matplotlib
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1886
diff
changeset
|
58 |
if x is None or y is None: |
f36d43f00f32
[views] plot with flot and get rid of matplotlib
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1886
diff
changeset
|
59 |
continue |
f36d43f00f32
[views] plot with flot and get rid of matplotlib
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1886
diff
changeset
|
60 |
filtered.append( (x, y) ) |
f36d43f00f32
[views] plot with flot and get rid of matplotlib
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1886
diff
changeset
|
61 |
return sorted(filtered) |
0 | 62 |
|
1892
fb80d9c434e5
[views] extract a PieWidget from pie views
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1891
diff
changeset
|
63 |
class PlotWidget(object): |
fb80d9c434e5
[views] extract a PieWidget from pie views
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1891
diff
changeset
|
64 |
# XXX refactor with cubicweb.web.views.htmlwidgets.HtmlWidget |
fb80d9c434e5
[views] extract a PieWidget from pie views
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1891
diff
changeset
|
65 |
def _initialize_stream(self, w=None): |
fb80d9c434e5
[views] extract a PieWidget from pie views
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1891
diff
changeset
|
66 |
if w: |
fb80d9c434e5
[views] extract a PieWidget from pie views
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1891
diff
changeset
|
67 |
self.w = w |
fb80d9c434e5
[views] extract a PieWidget from pie views
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1891
diff
changeset
|
68 |
else: |
fb80d9c434e5
[views] extract a PieWidget from pie views
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1891
diff
changeset
|
69 |
self._stream = UStringIO() |
fb80d9c434e5
[views] extract a PieWidget from pie views
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1891
diff
changeset
|
70 |
self.w = self._stream.write |
fb80d9c434e5
[views] extract a PieWidget from pie views
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1891
diff
changeset
|
71 |
|
fb80d9c434e5
[views] extract a PieWidget from pie views
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1891
diff
changeset
|
72 |
def render(self, *args, **kwargs): |
fb80d9c434e5
[views] extract a PieWidget from pie views
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1891
diff
changeset
|
73 |
w = kwargs.pop('w', None) |
fb80d9c434e5
[views] extract a PieWidget from pie views
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1891
diff
changeset
|
74 |
self._initialize_stream(w) |
fb80d9c434e5
[views] extract a PieWidget from pie views
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1891
diff
changeset
|
75 |
self._render(*args, **kwargs) |
fb80d9c434e5
[views] extract a PieWidget from pie views
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1891
diff
changeset
|
76 |
if w is None: |
fb80d9c434e5
[views] extract a PieWidget from pie views
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1891
diff
changeset
|
77 |
return self._stream.getvalue() |
fb80d9c434e5
[views] extract a PieWidget from pie views
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1891
diff
changeset
|
78 |
|
fb80d9c434e5
[views] extract a PieWidget from pie views
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1891
diff
changeset
|
79 |
class FlotPlotWidget(PlotWidget): |
1891
dd7c1d7715e7
[views][plots] extract the plotting mechanism in an HTMLWidget to make mit more re-usable
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1888
diff
changeset
|
80 |
"""PlotRenderer widget using Flot""" |
dd7c1d7715e7
[views][plots] extract the plotting mechanism in an HTMLWidget to make mit more re-usable
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1888
diff
changeset
|
81 |
onload = u''' |
dd7c1d7715e7
[views][plots] extract the plotting mechanism in an HTMLWidget to make mit more re-usable
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1888
diff
changeset
|
82 |
%(plotdefs)s |
dd7c1d7715e7
[views][plots] extract the plotting mechanism in an HTMLWidget to make mit more re-usable
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1888
diff
changeset
|
83 |
jQuery.plot(jQuery("#%(figid)s"), [%(plotdata)s], |
dd7c1d7715e7
[views][plots] extract the plotting mechanism in an HTMLWidget to make mit more re-usable
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1888
diff
changeset
|
84 |
{points: {show: true}, |
dd7c1d7715e7
[views][plots] extract the plotting mechanism in an HTMLWidget to make mit more re-usable
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1888
diff
changeset
|
85 |
lines: {show: true}, |
dd7c1d7715e7
[views][plots] extract the plotting mechanism in an HTMLWidget to make mit more re-usable
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1888
diff
changeset
|
86 |
grid: {hoverable: true}, |
dd7c1d7715e7
[views][plots] extract the plotting mechanism in an HTMLWidget to make mit more re-usable
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1888
diff
changeset
|
87 |
xaxis: {mode: %(mode)s}}); |
1911
16af47588d41
make emacs happier
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1892
diff
changeset
|
88 |
jQuery("#%(figid)s").bind("plothover", onPlotHover); |
1891
dd7c1d7715e7
[views][plots] extract the plotting mechanism in an HTMLWidget to make mit more re-usable
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1888
diff
changeset
|
89 |
''' |
1892
fb80d9c434e5
[views] extract a PieWidget from pie views
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1891
diff
changeset
|
90 |
|
1891
dd7c1d7715e7
[views][plots] extract the plotting mechanism in an HTMLWidget to make mit more re-usable
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1888
diff
changeset
|
91 |
def __init__(self, labels, plots, timemode=False): |
dd7c1d7715e7
[views][plots] extract the plotting mechanism in an HTMLWidget to make mit more re-usable
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1888
diff
changeset
|
92 |
self.labels = labels |
dd7c1d7715e7
[views][plots] extract the plotting mechanism in an HTMLWidget to make mit more re-usable
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1888
diff
changeset
|
93 |
self.plots = plots # list of list of couples |
dd7c1d7715e7
[views][plots] extract the plotting mechanism in an HTMLWidget to make mit more re-usable
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1888
diff
changeset
|
94 |
self.timemode = timemode |
dd7c1d7715e7
[views][plots] extract the plotting mechanism in an HTMLWidget to make mit more re-usable
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1888
diff
changeset
|
95 |
|
dd7c1d7715e7
[views][plots] extract the plotting mechanism in an HTMLWidget to make mit more re-usable
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1888
diff
changeset
|
96 |
def dump_plot(self, plot): |
dd7c1d7715e7
[views][plots] extract the plotting mechanism in an HTMLWidget to make mit more re-usable
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1888
diff
changeset
|
97 |
# XXX for now, the only way that we have to customize properly |
dd7c1d7715e7
[views][plots] extract the plotting mechanism in an HTMLWidget to make mit more re-usable
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1888
diff
changeset
|
98 |
# datetime labels on tooltips is to insert an additional column |
dd7c1d7715e7
[views][plots] extract the plotting mechanism in an HTMLWidget to make mit more re-usable
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1888
diff
changeset
|
99 |
# cf. function onPlotHover in cubicweb.flot.js |
dd7c1d7715e7
[views][plots] extract the plotting mechanism in an HTMLWidget to make mit more re-usable
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1888
diff
changeset
|
100 |
if self.timemode: |
dd7c1d7715e7
[views][plots] extract the plotting mechanism in an HTMLWidget to make mit more re-usable
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1888
diff
changeset
|
101 |
plot = [(datetime2ticks(x), y, datetime2ticks(x)) for x,y in plot] |
dd7c1d7715e7
[views][plots] extract the plotting mechanism in an HTMLWidget to make mit more re-usable
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1888
diff
changeset
|
102 |
return dumps(plot) |
1892
fb80d9c434e5
[views] extract a PieWidget from pie views
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1891
diff
changeset
|
103 |
|
fb80d9c434e5
[views] extract a PieWidget from pie views
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1891
diff
changeset
|
104 |
def _render(self, req, width=500, height=400): |
1891
dd7c1d7715e7
[views][plots] extract the plotting mechanism in an HTMLWidget to make mit more re-usable
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1888
diff
changeset
|
105 |
# XXX IE requires excanvas.js |
3097
a7adeca85656
[views] #344733: make burndown chart visible with IE6/7
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
2657
diff
changeset
|
106 |
req.add_js( ('jquery.flot.js', 'cubicweb.flot.js', 'excanvas.js') ) |
3338
cd137d90f896
can't use anymore make_uid to generate a valid js var name
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3097
diff
changeset
|
107 |
figid = u'figure%s' % req.varmaker.next() |
1891
dd7c1d7715e7
[views][plots] extract the plotting mechanism in an HTMLWidget to make mit more re-usable
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1888
diff
changeset
|
108 |
plotdefs = [] |
dd7c1d7715e7
[views][plots] extract the plotting mechanism in an HTMLWidget to make mit more re-usable
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1888
diff
changeset
|
109 |
plotdata = [] |
1892
fb80d9c434e5
[views] extract a PieWidget from pie views
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1891
diff
changeset
|
110 |
self.w(u'<div id="%s" style="width: %spx; height: %spx;"></div>' % |
fb80d9c434e5
[views] extract a PieWidget from pie views
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1891
diff
changeset
|
111 |
(figid, width, height)) |
1891
dd7c1d7715e7
[views][plots] extract the plotting mechanism in an HTMLWidget to make mit more re-usable
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1888
diff
changeset
|
112 |
for idx, (label, plot) in enumerate(zip(self.labels, self.plots)): |
dd7c1d7715e7
[views][plots] extract the plotting mechanism in an HTMLWidget to make mit more re-usable
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1888
diff
changeset
|
113 |
plotid = '%s_%s' % (figid, idx) |
dd7c1d7715e7
[views][plots] extract the plotting mechanism in an HTMLWidget to make mit more re-usable
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1888
diff
changeset
|
114 |
plotdefs.append('var %s = %s;' % (plotid, self.dump_plot(plot))) |
3714
3cb7dcd9ae77
ugly workaround to javascript serialisation problem #481437 (not considered fixed by this changeset)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
3689
diff
changeset
|
115 |
# XXX ugly but required in order to not crash my demo |
3cb7dcd9ae77
ugly workaround to javascript serialisation problem #481437 (not considered fixed by this changeset)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
3689
diff
changeset
|
116 |
plotdata.append("{label: '%s', data: %s}" % (label.replace(u'&', u''), plotid)) |
1891
dd7c1d7715e7
[views][plots] extract the plotting mechanism in an HTMLWidget to make mit more re-usable
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1888
diff
changeset
|
117 |
req.html_headers.add_onload(self.onload % |
dd7c1d7715e7
[views][plots] extract the plotting mechanism in an HTMLWidget to make mit more re-usable
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1888
diff
changeset
|
118 |
{'plotdefs': '\n'.join(plotdefs), |
dd7c1d7715e7
[views][plots] extract the plotting mechanism in an HTMLWidget to make mit more re-usable
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1888
diff
changeset
|
119 |
'figid': figid, |
dd7c1d7715e7
[views][plots] extract the plotting mechanism in an HTMLWidget to make mit more re-usable
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1888
diff
changeset
|
120 |
'plotdata': ','.join(plotdata), |
3785
78909a156011
read form arg specifying json call context (to make it work under json controller)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3714
diff
changeset
|
121 |
'mode': self.timemode and "'time'" or 'null'}, |
78909a156011
read form arg specifying json call context (to make it work under json controller)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3714
diff
changeset
|
122 |
jsoncall=req.form.get('jsoncall', False)) |
1892
fb80d9c434e5
[views] extract a PieWidget from pie views
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1891
diff
changeset
|
123 |
|
1891
dd7c1d7715e7
[views][plots] extract the plotting mechanism in an HTMLWidget to make mit more re-usable
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1888
diff
changeset
|
124 |
|
1888
f36d43f00f32
[views] plot with flot and get rid of matplotlib
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1886
diff
changeset
|
125 |
class PlotView(baseviews.AnyRsetView): |
3408
c92170fca813
[api] use __regid__ instead of deprecated id
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3338
diff
changeset
|
126 |
__regid__ = 'plot' |
1888
f36d43f00f32
[views] plot with flot and get rid of matplotlib
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1886
diff
changeset
|
127 |
title = _('generic plot') |
f36d43f00f32
[views] plot with flot and get rid of matplotlib
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1886
diff
changeset
|
128 |
__select__ = at_least_two_columns() & all_columns_are_numbers() |
1891
dd7c1d7715e7
[views][plots] extract the plotting mechanism in an HTMLWidget to make mit more re-usable
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1888
diff
changeset
|
129 |
timemode = False |
0 | 130 |
|
1888
f36d43f00f32
[views] plot with flot and get rid of matplotlib
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1886
diff
changeset
|
131 |
def call(self, width=500, height=400): |
f36d43f00f32
[views] plot with flot and get rid of matplotlib
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1886
diff
changeset
|
132 |
# prepare data |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3408
diff
changeset
|
133 |
rqlst = self.cw_rset.syntax_tree() |
1888
f36d43f00f32
[views] plot with flot and get rid of matplotlib
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1886
diff
changeset
|
134 |
# XXX try to make it work with unions |
f36d43f00f32
[views] plot with flot and get rid of matplotlib
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1886
diff
changeset
|
135 |
varnames = [var.name for var in rqlst.children[0].get_selected_variables()][1:] |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3408
diff
changeset
|
136 |
abscissa = [row[0] for row in self.cw_rset] |
1891
dd7c1d7715e7
[views][plots] extract the plotting mechanism in an HTMLWidget to make mit more re-usable
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1888
diff
changeset
|
137 |
plots = [] |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3408
diff
changeset
|
138 |
nbcols = len(self.cw_rset.rows[0]) |
1891
dd7c1d7715e7
[views][plots] extract the plotting mechanism in an HTMLWidget to make mit more re-usable
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1888
diff
changeset
|
139 |
for col in xrange(1, nbcols): |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3408
diff
changeset
|
140 |
data = [row[col] for row in self.cw_rset] |
1991
8a9d8f1ee100
bugfix for plots
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
1977
diff
changeset
|
141 |
plots.append(filterout_nulls(abscissa, data)) |
1891
dd7c1d7715e7
[views][plots] extract the plotting mechanism in an HTMLWidget to make mit more re-usable
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1888
diff
changeset
|
142 |
plotwidget = FlotPlotWidget(varnames, plots, timemode=self.timemode) |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3408
diff
changeset
|
143 |
plotwidget.render(self._cw, width, height, w=self.w) |
0 | 144 |
|
1888
f36d43f00f32
[views] plot with flot and get rid of matplotlib
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1886
diff
changeset
|
145 |
|
f36d43f00f32
[views] plot with flot and get rid of matplotlib
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1886
diff
changeset
|
146 |
class TimeSeriePlotView(PlotView): |
f36d43f00f32
[views] plot with flot and get rid of matplotlib
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1886
diff
changeset
|
147 |
__select__ = at_least_two_columns() & columns_are_date_then_numbers() |
1891
dd7c1d7715e7
[views][plots] extract the plotting mechanism in an HTMLWidget to make mit more re-usable
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1888
diff
changeset
|
148 |
timemode = True |
1886
f0e28ddba7c5
[views] add pie chart views with google chart / GChartWrapper
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
149 |
|
1892
fb80d9c434e5
[views] extract a PieWidget from pie views
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1891
diff
changeset
|
150 |
|
1886
f0e28ddba7c5
[views] add pie chart views with google chart / GChartWrapper
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
151 |
try: |
f0e28ddba7c5
[views] add pie chart views with google chart / GChartWrapper
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
152 |
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
|
153 |
except ImportError: |
f0e28ddba7c5
[views] add pie chart views with google chart / GChartWrapper
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
154 |
pass |
f0e28ddba7c5
[views] add pie chart views with google chart / GChartWrapper
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
155 |
else: |
1892
fb80d9c434e5
[views] extract a PieWidget from pie views
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1891
diff
changeset
|
156 |
|
fb80d9c434e5
[views] extract a PieWidget from pie views
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1891
diff
changeset
|
157 |
class PieChartWidget(PlotWidget): |
fb80d9c434e5
[views] extract a PieWidget from pie views
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1891
diff
changeset
|
158 |
def __init__(self, labels, values, pieclass=Pie, title=None): |
fb80d9c434e5
[views] extract a PieWidget from pie views
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1891
diff
changeset
|
159 |
self.labels = labels |
fb80d9c434e5
[views] extract a PieWidget from pie views
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1891
diff
changeset
|
160 |
self.values = values |
fb80d9c434e5
[views] extract a PieWidget from pie views
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1891
diff
changeset
|
161 |
self.pieclass = pieclass |
fb80d9c434e5
[views] extract a PieWidget from pie views
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1891
diff
changeset
|
162 |
self.title = title |
fb80d9c434e5
[views] extract a PieWidget from pie views
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1891
diff
changeset
|
163 |
|
fb80d9c434e5
[views] extract a PieWidget from pie views
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1891
diff
changeset
|
164 |
def _render(self, width=None, height=None): |
fb80d9c434e5
[views] extract a PieWidget from pie views
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1891
diff
changeset
|
165 |
piechart = self.pieclass(self.values) |
fb80d9c434e5
[views] extract a PieWidget from pie views
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1891
diff
changeset
|
166 |
piechart.label(*self.labels) |
fb80d9c434e5
[views] extract a PieWidget from pie views
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1891
diff
changeset
|
167 |
if width is not None: |
fb80d9c434e5
[views] extract a PieWidget from pie views
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1891
diff
changeset
|
168 |
height = height or width |
fb80d9c434e5
[views] extract a PieWidget from pie views
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1891
diff
changeset
|
169 |
piechart.size(width, height) |
fb80d9c434e5
[views] extract a PieWidget from pie views
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1891
diff
changeset
|
170 |
if self.title: |
fb80d9c434e5
[views] extract a PieWidget from pie views
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1891
diff
changeset
|
171 |
piechart.title(self.title) |
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2161
diff
changeset
|
172 |
self.w(u'<img src="%s" />' % xml_escape(piechart.url)) |
1892
fb80d9c434e5
[views] extract a PieWidget from pie views
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1891
diff
changeset
|
173 |
|
1886
f0e28ddba7c5
[views] add pie chart views with google chart / GChartWrapper
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
174 |
class PieChartView(baseviews.AnyRsetView): |
3408
c92170fca813
[api] use __regid__ instead of deprecated id
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3338
diff
changeset
|
175 |
__regid__ = 'piechart' |
1886
f0e28ddba7c5
[views] add pie chart views with google chart / GChartWrapper
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
176 |
pieclass = Pie |
1892
fb80d9c434e5
[views] extract a PieWidget from pie views
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1891
diff
changeset
|
177 |
|
1888
f36d43f00f32
[views] plot with flot and get rid of matplotlib
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1886
diff
changeset
|
178 |
__select__ = at_least_two_columns() & second_column_is_number() |
1886
f0e28ddba7c5
[views] add pie chart views with google chart / GChartWrapper
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
179 |
|
1956
9865daa96cd7
[views][plot] piechart view now accepts either final or non final entities in the first column
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1911
diff
changeset
|
180 |
def _guess_vid(self, row): |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3408
diff
changeset
|
181 |
etype = self.cw_rset.description[row][0] |
4045
f4a52abb6f4f
cw 3.6 api update
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3890
diff
changeset
|
182 |
if self._cw.vreg.schema.eschema(etype).final: |
1956
9865daa96cd7
[views][plot] piechart view now accepts either final or non final entities in the first column
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1911
diff
changeset
|
183 |
return 'final' |
9865daa96cd7
[views][plot] piechart view now accepts either final or non final entities in the first column
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1911
diff
changeset
|
184 |
return 'textincontext' |
9865daa96cd7
[views][plot] piechart view now accepts either final or non final entities in the first column
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1911
diff
changeset
|
185 |
|
1886
f0e28ddba7c5
[views] add pie chart views with google chart / GChartWrapper
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
186 |
def call(self, title=None, width=None, height=None): |
1956
9865daa96cd7
[views][plot] piechart view now accepts either final or non final entities in the first column
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1911
diff
changeset
|
187 |
labels = [] |
9865daa96cd7
[views][plot] piechart view now accepts either final or non final entities in the first column
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1911
diff
changeset
|
188 |
values = [] |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3408
diff
changeset
|
189 |
for rowidx, (_, value) in enumerate(self.cw_rset): |
1956
9865daa96cd7
[views][plot] piechart view now accepts either final or non final entities in the first column
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1911
diff
changeset
|
190 |
if value is not None: |
9865daa96cd7
[views][plot] piechart view now accepts either final or non final entities in the first column
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1911
diff
changeset
|
191 |
vid = self._guess_vid(rowidx) |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3408
diff
changeset
|
192 |
label = '%s: %s' % (self.view(vid, self.cw_rset, row=rowidx, col=0), |
1956
9865daa96cd7
[views][plot] piechart view now accepts either final or non final entities in the first column
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1911
diff
changeset
|
193 |
value) |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3408
diff
changeset
|
194 |
labels.append(label.encode(self._cw.encoding)) |
1956
9865daa96cd7
[views][plot] piechart view now accepts either final or non final entities in the first column
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1911
diff
changeset
|
195 |
values.append(value) |
1892
fb80d9c434e5
[views] extract a PieWidget from pie views
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1891
diff
changeset
|
196 |
pie = PieChartWidget(labels, values, pieclass=self.pieclass, |
fb80d9c434e5
[views] extract a PieWidget from pie views
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1891
diff
changeset
|
197 |
title=title) |
1886
f0e28ddba7c5
[views] add pie chart views with google chart / GChartWrapper
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
198 |
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
|
199 |
height = height or width |
1892
fb80d9c434e5
[views] extract a PieWidget from pie views
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1891
diff
changeset
|
200 |
pie.render(width, height, w=self.w) |
1886
f0e28ddba7c5
[views] add pie chart views with google chart / GChartWrapper
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
201 |
|
f0e28ddba7c5
[views] add pie chart views with google chart / GChartWrapper
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
202 |
|
f0e28ddba7c5
[views] add pie chart views with google chart / GChartWrapper
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
203 |
class PieChart3DView(PieChartView): |
3408
c92170fca813
[api] use __regid__ instead of deprecated id
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3338
diff
changeset
|
204 |
__regid__ = 'piechart3D' |
1886
f0e28ddba7c5
[views] add pie chart views with google chart / GChartWrapper
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
205 |
pieclass = Pie3D |