# -*- coding: utf-8 -*- """ Controllers for building graphs using pandas library """ import base64 import matplotlib matplotlib.use("Agg") import matplotlib.pyplot as plt from gluon.storage import Storage from graph_tools import (FROM_TO, LABELY_YEAR, linechart, savefig, stackchart) def dashboard(): """Return a pre-configure linechart for public used. Cumulative distribution for the publications are shown for the current year. """ 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 = "" # figure layout fig, (ax1, ax2) = plt.subplots(nrows=1, ncols=2, sharey=True) # 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 title = (T(FROM_TO) % (min_year, max_year)).decode("utf-8") ylabel = T(LABELY_YEAR).decode("utf-8") linechart(db, selector, target=ax1, xlabel="", ylabel=ylabel, title=title) # 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) # delegate the rendering to the view response.view = "graphs/index.html" return dict(data=savefig(fig, "svg")) def publications_versus_time(): """Generate graph showing the number of publication per month / year either as a line chart or as a stacked histograms. """ # user criteria and graph configuration selector = request.vars graph = db.graphs[selector.Graph_selectorId_graphs] axis = graph.stack_axis # instantiate the graph ax = (stackchart(db, selector) if axis else linechart(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) response.view = "graphs/index.%s" % extension return dict(data=data)