graphs.py 2.78 KB
Newer Older
LE GAC Renaud's avatar
LE GAC Renaud committed
1
# -*- coding: utf-8 -*-
2
""" Controllers for building graphs using pandas library
LE GAC Renaud's avatar
LE GAC Renaud committed
3 4

"""
5
import base64
6
import matplotlib
7
matplotlib.use('Agg')
8
import matplotlib.pyplot as plt
LE GAC Renaud's avatar
LE GAC Renaud committed
9

10
from datetime import datetime
LE GAC Renaud's avatar
LE GAC Renaud committed
11
from gluon.storage import Storage
12
from graph_tools import (do_labels,
13
                         do_linechart,
14
                         do_stackedchart,
15
                         do_title,
16
                         savefig)
17
from pandas import DataFrame
LE GAC Renaud's avatar
LE GAC Renaud committed
18 19 20 21
from plugin_dbui import Selector
from reporting_tools import repr_team_project


22 23 24
TITLE_Y = "Number of publications"


LE GAC Renaud's avatar
LE GAC Renaud committed
25
def dashboard():
26
    """Return a pre-configure linechart for public used.
27
    Cumulative distribution for the publications are shown for
28
    the current year.
29

30
    """
31
    cfg = Storage()
32
    cfg.Graph_selectorCumulative = 'True'
33 34 35 36 37
    cfg.Graph_selectorId = ''
    cfg.Graph_selectorId_authors_roles = ''
    cfg.Graph_selectorId_graphs = ''
    cfg.Graph_selectorId_projects = ''
    cfg.Graphs_selectorId_teams = ''
38
    cfg.Graph_selectorTime = T('month')
39 40 41 42 43
    cfg.Graph_selectorYear_start = datetime.now().year
    cfg.Graph_selectorYear_end = ''

    request.vars.update(cfg)

44
    fields = ('cumulative','id_graphs', 'time', 'year_start','year_end')
45
    selector = Selector(virtdb.graph_selector, exclude_fields=fields)
46

47 48
    # figure layout
    fig, axes = plt.subplots(nrows=1, ncols=2, sharey=True)
49
    fig.subplots_adjust(wspace=0.1)
50

51
    # the cumulative sum of publications for the current year
52
    do_linechart(db.publications, selector, target=axes[0])
53
    do_labels(axes[0], "", T(TITLE_Y))
54

55
    # histogram of the number of publications per year
56 57 58 59 60
    selector.cumulative = False
    selector.time = T('year')
    selector.year_start = ''

    do_linechart(db.publications, selector, target=axes[1])
61
    do_labels(axes[1], "", "")
62 63 64

    # delegate the rendering to the view
    response.view = "graphs/index.html"
65
    return dict(data=savefig(fig, "svg"))
66 67


68
def index():
69 70
    """Generate graph showing the number of publication per month / year
    either as a linechart or as a stacked histograms.
71

72
    """
73
    fields = ('cumulative','id_graphs', 'time', 'year_start','year_end')
74
    selector = Selector(virtdb.graph_selector, exclude_fields=fields)
75

76
    # graph configuration
77
    graph = db.graphs[selector.id_graphs]
78

79 80
    # stacked chart
    if graph.stack_axis:
81
        ax = do_stackedchart(db.publications, selector, graph)
82

83 84
    # line char
    else:
85
        ax = do_linechart(db.publications, selector)
LE GAC Renaud's avatar
LE GAC Renaud committed
86

87 88 89
    do_labels(ax, "", T(TITLE_Y))
    do_title(ax, db, selector)

90
    # delegate the rendering to the view
91 92 93
    extension = request.extension
    fmt = "svg" if extension == "html" else extension

94 95 96 97 98 99
    # base64 string encoding
    data = savefig(ax.get_figure(), fmt)
    if fmt in ("pdf", "png"):
        data = base64.b64encode(data)

    return dict(data=data)