graphs.py 2.88 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
LE GAC Renaud's avatar
LE GAC Renaud committed
7
matplotlib.use("Agg")
8
import matplotlib.pyplot as plt
LE GAC Renaud's avatar
LE GAC Renaud committed
9 10

from gluon.storage import Storage
11 12 13 14 15
from graph_tools import (FROM_TO,
                         LABELY_YEAR,
                         linechart,
                         savefig,
                         stackchart)
16 17


LE GAC Renaud's avatar
LE GAC Renaud committed
18
def dashboard():
19
    """Return a pre-configure linechart for public used.
20
    Cumulative distribution for the publications are shown for
21
    the current year.
22

23
    """
24 25 26 27 28 29 30 31 32 33 34 35
    current_year = request.now.year

    selector = Storage()
    selector.Graph_selectorCumulative = "true"
    selector.Graph_selectorId = ""
    selector.Graph_selectorId_authors_roles = ""
    selector.Graph_selectorId_graphs = ""
    selector.Graph_selectorId_projects = ""
    selector.Graphs_selectorId_teams = ""
    selector.Graph_selectorTime = ""
    selector.Graph_selectorYear_start = ""
    selector.Graph_selectorYear_end = ""
36

37
    # figure layout
38
    fig, (ax1, ax2) = plt.subplots(nrows=1, ncols=2, sharey=True)
39

40 41 42 43 44 45 46 47
    # the number of publications per year except for the current year
    min_year = "2009"
    max_year = str(current_year - 1)

    selector.Graph_selectorCumulative = "false"
    selector.Graph_selectorTime = T("year")
    selector.Graph_selectorYear_start = min_year
    selector.Graph_selectorYear_end = max_year
48

49 50
    title = (T(FROM_TO) % (min_year, max_year)).decode("utf-8")
    ylabel = T(LABELY_YEAR).decode("utf-8")
51

52 53 54 55 56 57
    linechart(db,
              selector,
              target=ax1,
              xlabel="",
              ylabel=ylabel,
              title=title)
58

59 60 61 62 63 64 65 66 67
    # the cumulative sum of publications for the current year on a month
    year = str(current_year)
    selector.Graph_selectorCumulative = "true"
    selector.Graph_selectorTime = T("month")
    selector.Graph_selectorYear_start = year
    selector.Graph_selectorYear_end = ""

    title = (T("In %s") % year).decode("utf-8")
    linechart(db, selector, target=ax2, title=title)
68 69 70

    # delegate the rendering to the view
    response.view = "graphs/index.html"
71
    return dict(data=savefig(fig, "svg"))
72 73


74
def publications_versus_time():
75
    """Generate graph showing the number of publication per month / year
76
    either as a line chart or as a stacked histograms.
77

78
    """
79 80 81 82
    # user criteria and graph configuration
    selector = request.vars
    graph = db.graphs[selector.Graph_selectorId_graphs]
    axis = graph.stack_axis
LE GAC Renaud's avatar
LE GAC Renaud committed
83

84 85
    # instantiate the graph
    ax = (stackchart(db, selector) if axis else linechart(db, selector))
86

87
    # delegate the rendering to the view
88 89 90
    extension = request.extension
    fmt = "svg" if extension == "html" else extension

91 92 93 94 95
    # base64 string encoding
    data = savefig(ax.get_figure(), fmt)
    if fmt in ("pdf", "png"):
        data = base64.b64encode(data)

96
    response.view = "graphs/index.%s" % extension
97
    return dict(data=data)