author | Adrien Di Mascio <Adrien.DiMascio@logilab.fr> |
Thu, 21 May 2009 17:31:05 +0200 | |
changeset 1892 | fb80d9c434e5 |
parent 1891 | dd7c1d7715e7 |
child 1911 | 16af47588d41 |
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 |
1888
f36d43f00f32
[views] plot with flot and get rid of matplotlib
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1886
diff
changeset
|
10 |
import time |
f36d43f00f32
[views] plot with flot and get rid of matplotlib
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1886
diff
changeset
|
11 |
|
f36d43f00f32
[views] plot with flot and get rid of matplotlib
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1886
diff
changeset
|
12 |
from simplejson import dumps |
0 | 13 |
|
14 |
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
|
15 |
from logilab.mtconverter import html_escape |
0 | 16 |
|
1892
fb80d9c434e5
[views] extract a PieWidget from pie views
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1891
diff
changeset
|
17 |
from cubicweb.utils import make_uid, UStringIO |
742
99115e029dca
replaced most of __selectors__ assignments with __select__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
18 |
from cubicweb.vregistry import objectify_selector |
0 | 19 |
from cubicweb.web.views import baseviews |
20 |
||
742
99115e029dca
replaced most of __selectors__ assignments with __select__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
21 |
@objectify_selector |
1888
f36d43f00f32
[views] plot with flot and get rid of matplotlib
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1886
diff
changeset
|
22 |
def at_least_two_columns(cls, req, rset, *args, **kwargs): |
f36d43f00f32
[views] plot with flot and get rid of matplotlib
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1886
diff
changeset
|
23 |
if not rset: |
f36d43f00f32
[views] plot with flot and get rid of matplotlib
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1886
diff
changeset
|
24 |
return 0 |
f36d43f00f32
[views] plot with flot and get rid of matplotlib
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1886
diff
changeset
|
25 |
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
|
26 |
|
f36d43f00f32
[views] plot with flot and get rid of matplotlib
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1886
diff
changeset
|
27 |
@objectify_selector |
f36d43f00f32
[views] plot with flot and get rid of matplotlib
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1886
diff
changeset
|
28 |
def all_columns_are_numbers(cls, req, rset, *args, **kwargs): |
0 | 29 |
"""accept result set with at least one line and two columns of result |
30 |
all columns after second must be of numerical types""" |
|
31 |
for etype in rset.description[0]: |
|
32 |
if etype not in ('Int', 'Float'): |
|
33 |
return 0 |
|
34 |
return 1 |
|
35 |
||
1886
f0e28ddba7c5
[views] add pie chart views with google chart / GChartWrapper
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
36 |
@objectify_selector |
1888
f36d43f00f32
[views] plot with flot and get rid of matplotlib
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1886
diff
changeset
|
37 |
def second_column_is_number(cls, req, rset, *args, **kwargs): |
1886
f0e28ddba7c5
[views] add pie chart views with google chart / GChartWrapper
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
38 |
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
|
39 |
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
|
40 |
return 0 |
f0e28ddba7c5
[views] add pie chart views with google chart / GChartWrapper
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
41 |
return 1 |
f0e28ddba7c5
[views] add pie chart views with google chart / GChartWrapper
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
42 |
|
1888
f36d43f00f32
[views] plot with flot and get rid of matplotlib
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1886
diff
changeset
|
43 |
@objectify_selector |
f36d43f00f32
[views] plot with flot and get rid of matplotlib
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1886
diff
changeset
|
44 |
def columns_are_date_then_numbers(cls, req, rset, *args, **kwargs): |
f36d43f00f32
[views] plot with flot and get rid of matplotlib
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1886
diff
changeset
|
45 |
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
|
46 |
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
|
47 |
return 0 |
f36d43f00f32
[views] plot with flot and get rid of matplotlib
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1886
diff
changeset
|
48 |
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
|
49 |
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
|
50 |
return 0 |
f36d43f00f32
[views] plot with flot and get rid of matplotlib
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1886
diff
changeset
|
51 |
return 1 |
f36d43f00f32
[views] plot with flot and get rid of matplotlib
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1886
diff
changeset
|
52 |
|
0 | 53 |
|
1888
f36d43f00f32
[views] plot with flot and get rid of matplotlib
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1886
diff
changeset
|
54 |
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
|
55 |
filtered = [] |
f36d43f00f32
[views] plot with flot and get rid of matplotlib
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1886
diff
changeset
|
56 |
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
|
57 |
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
|
58 |
continue |
f36d43f00f32
[views] plot with flot and get rid of matplotlib
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1886
diff
changeset
|
59 |
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
|
60 |
return sorted(filtered) |
0 | 61 |
|
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
|
62 |
def datetime2ticks(date): |
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
|
63 |
return time.mktime(date.timetuple()) * 1000 |
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
|
64 |
|
1892
fb80d9c434e5
[views] extract a PieWidget from pie views
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1891
diff
changeset
|
65 |
class PlotWidget(object): |
fb80d9c434e5
[views] extract a PieWidget from pie views
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1891
diff
changeset
|
66 |
# 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
|
67 |
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
|
68 |
if w: |
fb80d9c434e5
[views] extract a PieWidget from pie views
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1891
diff
changeset
|
69 |
self.w = w |
fb80d9c434e5
[views] extract a PieWidget from pie views
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1891
diff
changeset
|
70 |
else: |
fb80d9c434e5
[views] extract a PieWidget from pie views
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1891
diff
changeset
|
71 |
self._stream = UStringIO() |
fb80d9c434e5
[views] extract a PieWidget from pie views
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1891
diff
changeset
|
72 |
self.w = self._stream.write |
fb80d9c434e5
[views] extract a PieWidget from pie views
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1891
diff
changeset
|
73 |
|
fb80d9c434e5
[views] extract a PieWidget from pie views
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1891
diff
changeset
|
74 |
def render(self, *args, **kwargs): |
fb80d9c434e5
[views] extract a PieWidget from pie views
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1891
diff
changeset
|
75 |
w = kwargs.pop('w', None) |
fb80d9c434e5
[views] extract a PieWidget from pie views
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1891
diff
changeset
|
76 |
self._initialize_stream(w) |
fb80d9c434e5
[views] extract a PieWidget from pie views
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1891
diff
changeset
|
77 |
self._render(*args, **kwargs) |
fb80d9c434e5
[views] extract a PieWidget from pie views
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1891
diff
changeset
|
78 |
if w is None: |
fb80d9c434e5
[views] extract a PieWidget from pie views
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1891
diff
changeset
|
79 |
return self._stream.getvalue() |
fb80d9c434e5
[views] extract a PieWidget from pie views
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1891
diff
changeset
|
80 |
|
fb80d9c434e5
[views] extract a PieWidget from pie views
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1891
diff
changeset
|
81 |
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
|
82 |
"""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
|
83 |
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
|
84 |
%(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
|
85 |
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
|
86 |
{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
|
87 |
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
|
88 |
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
|
89 |
xaxis: {mode: %(mode)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
|
90 |
jQuery('#%(figid)s').bind('plothover', onPlotHover); |
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 |
''' |
1892
fb80d9c434e5
[views] extract a PieWidget from pie views
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1891
diff
changeset
|
92 |
|
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
|
93 |
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
|
94 |
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
|
95 |
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
|
96 |
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
|
97 |
|
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 |
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
|
99 |
# 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
|
100 |
# 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
|
101 |
# 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
|
102 |
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
|
103 |
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
|
104 |
return dumps(plot) |
1892
fb80d9c434e5
[views] extract a PieWidget from pie views
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1891
diff
changeset
|
105 |
|
fb80d9c434e5
[views] extract a PieWidget from pie views
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1891
diff
changeset
|
106 |
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
|
107 |
# XXX IE requires excanvas.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
|
108 |
req.add_js( ('jquery.flot.js', '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
|
109 |
figid = u'figure%s' % make_uid('foo') |
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
|
110 |
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
|
111 |
plotdata = [] |
1892
fb80d9c434e5
[views] extract a PieWidget from pie views
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1891
diff
changeset
|
112 |
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
|
113 |
(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
|
114 |
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
|
115 |
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
|
116 |
plotdefs.append('var %s = %s;' % (plotid, self.dump_plot(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
|
117 |
plotdata.append("{label: '%s', data: %s}" % (label, plotid)) |
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 |
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
|
119 |
{'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
|
120 |
'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
|
121 |
'plotdata': ','.join(plotdata), |
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
|
122 |
'mode': self.timemode and "'time'" or 'null'}) |
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): |
f36d43f00f32
[views] plot with flot and get rid of matplotlib
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1886
diff
changeset
|
126 |
id = 'plot' |
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 |
f36d43f00f32
[views] plot with flot and get rid of matplotlib
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1886
diff
changeset
|
133 |
rqlst = self.rset.syntax_tree() |
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:] |
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
|
136 |
abscissa = [row[0] for row in self.rset] |
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 = [] |
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
|
138 |
nbcols = len(self.rset.rows[0]) |
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): |
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
|
140 |
data = [row[col] for row in self.rset] |
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
|
141 |
plots.append(filterout_nulls(abscissa, 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
|
142 |
plotwidget = FlotPlotWidget(varnames, plots, timemode=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
|
143 |
plotwidget.render(self.req, 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) |
fb80d9c434e5
[views] extract a PieWidget from pie views
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1891
diff
changeset
|
172 |
self.w(u'<img src="%s" />' % html_escape(piechart.url)) |
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): |
f0e28ddba7c5
[views] add pie chart views with google chart / GChartWrapper
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
175 |
id = 'piechart' |
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 |
|
f0e28ddba7c5
[views] add pie chart views with google chart / GChartWrapper
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
180 |
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
|
181 |
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
|
182 |
for row in self.rset] |
1892
fb80d9c434e5
[views] extract a PieWidget from pie views
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1891
diff
changeset
|
183 |
values = [(row[1] or 0) for row in self.rset] |
fb80d9c434e5
[views] extract a PieWidget from pie views
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1891
diff
changeset
|
184 |
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
|
185 |
title=title) |
1886
f0e28ddba7c5
[views] add pie chart views with google chart / GChartWrapper
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
186 |
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
|
187 |
height = height or width |
1892
fb80d9c434e5
[views] extract a PieWidget from pie views
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1891
diff
changeset
|
188 |
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
|
189 |
|
f0e28ddba7c5
[views] add pie chart views with google chart / GChartWrapper
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
190 |
|
f0e28ddba7c5
[views] add pie chart views with google chart / GChartWrapper
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
191 |
class PieChart3DView(PieChartView): |
f0e28ddba7c5
[views] add pie chart views with google chart / GChartWrapper
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
192 |
id = 'piechart3D' |
f0e28ddba7c5
[views] add pie chart views with google chart / GChartWrapper
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
193 |
pieclass = Pie3D |