# -*- coding: utf-8 -*- """ Controllers for building graphs using pandas library """ import base64 import matplotlib matplotlib.use('Agg') import matplotlib.pyplot as plt from datetime import datetime from gluon.storage import Storage from graph_tools import (do_labels, do_linechart, do_stackedchart, do_title, savefig) from pandas import DataFrame from plugin_dbui import Selector from reporting_tools import repr_team_project TITLE_Y = "Number of publications" def dashboard(): """Return a pre-configure linechart for public used. Cumulative distribution for the publications are shown for the current year. """ cfg = Storage() cfg.Graph_selectorCumulative = 'True' cfg.Graph_selectorId = '' cfg.Graph_selectorId_authors_roles = '' cfg.Graph_selectorId_graphs = '' cfg.Graph_selectorId_projects = '' cfg.Graphs_selectorId_teams = '' cfg.Graph_selectorTime = T('month') cfg.Graph_selectorYear_start = datetime.now().year cfg.Graph_selectorYear_end = '' request.vars.update(cfg) fields = ('cumulative','id_graphs', 'time', 'year_start','year_end') selector = Selector(virtdb.graph_selector, exclude_fields=fields) # figure layout fig, axes = plt.subplots(nrows=1, ncols=2, sharey=True) fig.subplots_adjust(wspace=0.1) # the cumulative sum of publications for the current year do_linechart(db.publications, selector, target=axes[0]) do_labels(axes[0], "", T(TITLE_Y)) # histogram of the number of publications per year selector.cumulative = False selector.time = T('year') selector.year_start = '' do_linechart(db.publications, selector, target=axes[1]) do_labels(axes[1], "", "") # delegate the rendering to the view response.view = "graphs/index.html" return dict(data=savefig(fig, "svg")) def index(): """Generate graph showing the number of publication per month / year either as a linechart or as a stacked histograms. """ fields = ('cumulative','id_graphs', 'time', 'year_start','year_end') selector = Selector(virtdb.graph_selector, exclude_fields=fields) # graph configuration graph = db.graphs[selector.id_graphs] # stacked chart if graph.stack_axis: ax = do_stackedchart(db.publications, selector, graph) # line char else: ax = do_linechart(db.publications, selector) do_labels(ax, "", T(TITLE_Y)) do_title(ax, db, selector) # delegate the rendering to the view extension = request.extension fmt = "svg" if extension == "html" else extension # base64 string encoding data = savefig(ax.get_figure(), fmt) if fmt in ("pdf", "png"): data = base64.b64encode(data) return dict(data=data)