web/views/plots.py
author Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
Thu, 21 May 2009 00:44:57 +0200
changeset 1888 f36d43f00f32
parent 1886 f0e28ddba7c5
child 1891 dd7c1d7715e7
permissions -rw-r--r--
[views] plot with flot and get rid of matplotlib
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     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
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    13
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    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
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    16
1888
f36d43f00f32 [views] plot with flot and get rid of matplotlib
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1886
diff changeset
    17
from cubicweb.utils import make_uid
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
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    19
from cubicweb.web.views import baseviews
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    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
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    29
    """accept result set with at least one line and two columns of result
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    30
    all columns after second must be of numerical types"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    31
    for etype in rset.description[0]:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    32
        if etype not in ('Int', 'Float'):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    33
            return 0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    34
    return 1
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    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
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    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
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    61
1888
f36d43f00f32 [views] plot with flot and get rid of matplotlib
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1886
diff changeset
    62
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
    63
    id = 'plot'
f36d43f00f32 [views] plot with flot and get rid of matplotlib
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1886
diff changeset
    64
    title = _('generic plot')
f36d43f00f32 [views] plot with flot and get rid of matplotlib
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1886
diff changeset
    65
    __select__ = at_least_two_columns() & all_columns_are_numbers()
f36d43f00f32 [views] plot with flot and get rid of matplotlib
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1886
diff changeset
    66
    mode = 'null' # null or time, meant for jquery.flot.js
f36d43f00f32 [views] plot with flot and get rid of matplotlib
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1886
diff changeset
    67
f36d43f00f32 [views] plot with flot and get rid of matplotlib
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1886
diff changeset
    68
    def _build_abscissa(self):
f36d43f00f32 [views] plot with flot and get rid of matplotlib
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1886
diff changeset
    69
        return [row[0] for row in self.rset]
f36d43f00f32 [views] plot with flot and get rid of matplotlib
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1886
diff changeset
    70
f36d43f00f32 [views] plot with flot and get rid of matplotlib
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1886
diff changeset
    71
    def _build_data(self, abscissa, plot):
f36d43f00f32 [views] plot with flot and get rid of matplotlib
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1886
diff changeset
    72
        return filterout_nulls(abscissa, plot)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    73
1888
f36d43f00f32 [views] plot with flot and get rid of matplotlib
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1886
diff changeset
    74
    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
    75
        # XXX add excanvas.js if IE
f36d43f00f32 [views] plot with flot and get rid of matplotlib
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1886
diff changeset
    76
        self.req.add_js( ('jquery.flot.js', 'cubicweb.flot.js') )
f36d43f00f32 [views] plot with flot and get rid of matplotlib
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1886
diff changeset
    77
        # prepare data
f36d43f00f32 [views] plot with flot and get rid of matplotlib
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1886
diff changeset
    78
        abscissa = self._build_abscissa()
f36d43f00f32 [views] plot with flot and get rid of matplotlib
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1886
diff changeset
    79
        plots = []
f36d43f00f32 [views] plot with flot and get rid of matplotlib
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1886
diff changeset
    80
        nbcols = len(self.rset.rows[0])
f36d43f00f32 [views] plot with flot and get rid of matplotlib
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1886
diff changeset
    81
        for col in xrange(1, nbcols):
f36d43f00f32 [views] plot with flot and get rid of matplotlib
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1886
diff changeset
    82
            plots.append([row[col] for row in self.rset])
f36d43f00f32 [views] plot with flot and get rid of matplotlib
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1886
diff changeset
    83
        # plot data
f36d43f00f32 [views] plot with flot and get rid of matplotlib
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1886
diff changeset
    84
        plotuid = 'plot%s' % make_uid('foo')
f36d43f00f32 [views] plot with flot and get rid of matplotlib
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1886
diff changeset
    85
        self.w(u'<div id="%s" style="width: %spx; height: %spx;"></div>' %
f36d43f00f32 [views] plot with flot and get rid of matplotlib
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1886
diff changeset
    86
               (plotuid, width, height))
f36d43f00f32 [views] plot with flot and get rid of matplotlib
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1886
diff changeset
    87
        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
    88
        # 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
    89
        varnames = [var.name for var in rqlst.children[0].get_selected_variables()][1:]
f36d43f00f32 [views] plot with flot and get rid of matplotlib
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1886
diff changeset
    90
        plotdefs = []
f36d43f00f32 [views] plot with flot and get rid of matplotlib
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1886
diff changeset
    91
        plotdata = []
f36d43f00f32 [views] plot with flot and get rid of matplotlib
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1886
diff changeset
    92
        for idx, (varname, plot) in enumerate(zip(varnames, plots)):
f36d43f00f32 [views] plot with flot and get rid of matplotlib
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1886
diff changeset
    93
            plotid = '%s_%s' % (plotuid, idx)
f36d43f00f32 [views] plot with flot and get rid of matplotlib
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1886
diff changeset
    94
            data = self._build_data(abscissa, plot)
f36d43f00f32 [views] plot with flot and get rid of matplotlib
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1886
diff changeset
    95
            plotdefs.append('var %s = %s;' % (plotid, dumps(data)))
f36d43f00f32 [views] plot with flot and get rid of matplotlib
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1886
diff changeset
    96
            plotdata.append("{label: '%s', data: %s}" % (varname, plotid))
f36d43f00f32 [views] plot with flot and get rid of matplotlib
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1886
diff changeset
    97
        self.req.html_headers.add_onload('''
f36d43f00f32 [views] plot with flot and get rid of matplotlib
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1886
diff changeset
    98
%(plotdefs)s
f36d43f00f32 [views] plot with flot and get rid of matplotlib
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1886
diff changeset
    99
jQuery.plot(jQuery("#%(plotuid)s"), [%(plotdata)s],
f36d43f00f32 [views] plot with flot and get rid of matplotlib
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1886
diff changeset
   100
    {points: {show: true},
f36d43f00f32 [views] plot with flot and get rid of matplotlib
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1886
diff changeset
   101
     lines: {show: true},
f36d43f00f32 [views] plot with flot and get rid of matplotlib
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1886
diff changeset
   102
     grid: {hoverable: true},
f36d43f00f32 [views] plot with flot and get rid of matplotlib
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1886
diff changeset
   103
     xaxis: {mode: %(mode)s}});
f36d43f00f32 [views] plot with flot and get rid of matplotlib
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1886
diff changeset
   104
jQuery('#%(plotuid)s').bind('plothover', onPlotHover);
f36d43f00f32 [views] plot with flot and get rid of matplotlib
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1886
diff changeset
   105
''' % {'plotdefs': '\n'.join(plotdefs),
f36d43f00f32 [views] plot with flot and get rid of matplotlib
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1886
diff changeset
   106
       'plotuid': plotuid,
f36d43f00f32 [views] plot with flot and get rid of matplotlib
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1886
diff changeset
   107
       'plotdata': ','.join(plotdata),
f36d43f00f32 [views] plot with flot and get rid of matplotlib
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1886
diff changeset
   108
       'mode': self.mode})
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   109
1888
f36d43f00f32 [views] plot with flot and get rid of matplotlib
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1886
diff changeset
   110
f36d43f00f32 [views] plot with flot and get rid of matplotlib
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1886
diff changeset
   111
class TimeSeriePlotView(PlotView):
f36d43f00f32 [views] plot with flot and get rid of matplotlib
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1886
diff changeset
   112
    id = 'plot'
f36d43f00f32 [views] plot with flot and get rid of matplotlib
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1886
diff changeset
   113
    title = _('generic plot')
f36d43f00f32 [views] plot with flot and get rid of matplotlib
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1886
diff changeset
   114
    __select__ = at_least_two_columns() & columns_are_date_then_numbers()
f36d43f00f32 [views] plot with flot and get rid of matplotlib
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1886
diff changeset
   115
    mode = '"time"'
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   116
1888
f36d43f00f32 [views] plot with flot and get rid of matplotlib
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1886
diff changeset
   117
    def _build_abscissa(self):
f36d43f00f32 [views] plot with flot and get rid of matplotlib
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1886
diff changeset
   118
        abscissa = [time.mktime(row[0].timetuple()) * 1000
f36d43f00f32 [views] plot with flot and get rid of matplotlib
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1886
diff changeset
   119
                    for row in self.rset]
f36d43f00f32 [views] plot with flot and get rid of matplotlib
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1886
diff changeset
   120
        return abscissa
f36d43f00f32 [views] plot with flot and get rid of matplotlib
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1886
diff changeset
   121
f36d43f00f32 [views] plot with flot and get rid of matplotlib
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1886
diff changeset
   122
    def _build_data(self, abscissa, plot):
f36d43f00f32 [views] plot with flot and get rid of matplotlib
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1886
diff changeset
   123
        data = []
f36d43f00f32 [views] plot with flot and get rid of matplotlib
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1886
diff changeset
   124
        # XXX find a way to get rid of the 3rd column and find 'mode' in JS
f36d43f00f32 [views] plot with flot and get rid of matplotlib
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1886
diff changeset
   125
        for x, y in 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
   126
            data.append( (x, y, x) )
f36d43f00f32 [views] plot with flot and get rid of matplotlib
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1886
diff changeset
   127
        return data
1886
f0e28ddba7c5 [views] add pie chart views with google chart / GChartWrapper
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1133
diff changeset
   128
f0e28ddba7c5 [views] add pie chart views with google chart / GChartWrapper
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1133
diff changeset
   129
try:
f0e28ddba7c5 [views] add pie chart views with google chart / GChartWrapper
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1133
diff changeset
   130
    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
   131
except ImportError:
f0e28ddba7c5 [views] add pie chart views with google chart / GChartWrapper
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1133
diff changeset
   132
    pass
f0e28ddba7c5 [views] add pie chart views with google chart / GChartWrapper
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1133
diff changeset
   133
else:
f0e28ddba7c5 [views] add pie chart views with google chart / GChartWrapper
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1133
diff changeset
   134
    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
   135
        id = 'piechart'
f0e28ddba7c5 [views] add pie chart views with google chart / GChartWrapper
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1133
diff changeset
   136
        pieclass = Pie
1888
f36d43f00f32 [views] plot with flot and get rid of matplotlib
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1886
diff changeset
   137
        __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
   138
f0e28ddba7c5 [views] add pie chart views with google chart / GChartWrapper
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1133
diff changeset
   139
        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
   140
            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
   141
            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
   142
                      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
   143
            piechart.label(*labels)
f0e28ddba7c5 [views] add pie chart views with google chart / GChartWrapper
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1133
diff changeset
   144
            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
   145
                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
   146
                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
   147
            if title:
f0e28ddba7c5 [views] add pie chart views with google chart / GChartWrapper
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1133
diff changeset
   148
                piechart.title(title)
f0e28ddba7c5 [views] add pie chart views with google chart / GChartWrapper
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1133
diff changeset
   149
            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
   150
f0e28ddba7c5 [views] add pie chart views with google chart / GChartWrapper
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1133
diff changeset
   151
f0e28ddba7c5 [views] add pie chart views with google chart / GChartWrapper
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1133
diff changeset
   152
    class PieChart3DView(PieChartView):
f0e28ddba7c5 [views] add pie chart views with google chart / GChartWrapper
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1133
diff changeset
   153
        id = 'piechart3D'
f0e28ddba7c5 [views] add pie chart views with google chart / GChartWrapper
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1133
diff changeset
   154
        pieclass = Pie3D