Commit 373cc198 authored by LE GAC Renaud's avatar LE GAC Renaud
Browse files

Modify the dashboard using the new linechart.

parent b6f7a0b8
......@@ -8,8 +8,7 @@ import matplotlib.pyplot as plt
from datetime import datetime
from gluon.storage import Storage
from graph_tools import (do_barchart,
do_labels,
from graph_tools import (do_labels,
do_linechart,
do_stackedchart,
do_title,
......@@ -35,6 +34,7 @@ def dashboard():
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 = ''
......@@ -45,14 +45,18 @@ def dashboard():
# figure layout
fig, axes = plt.subplots(nrows=1, ncols=2, sharey=True)
fig.subplots_adjust(wspace=0)
fig.subplots_adjust(wspace=0.1)
# the cumulative sum of publications for the current year
do_linechart(db.publications, selector, ax=axes[0])
do_linechart(db.publications, selector, target=axes[0])
do_labels(axes[0], "", T(TITLE_Y))
# histogram of the number of publications per year
do_barchart(db.publications, selector, ax=axes[1])
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
......
......@@ -19,54 +19,13 @@ TITLE = "Publications"
TITLE_CUMSUM = "Cumulative sum of publications"
def do_barchart(publications, selector, ax=None):
"""Bar chart showing the number of publications per year.
Args:
publications (gluon.dal.Table): the publications table.
selector (plugin_dbui.Selector): selector with user critera
ax (matplotlib.AxesSubplot): useful when working with subplot.
Returns:
matplotlib.AxesSubplot: axes subplot
"""
db = publications._db
submitted_year = publications.submitted[:4]
now = datetime.now()
query = submitted_year > 1900
query &= submitted_year <= now.year
setrows = db(query)
if setrows.count() == 0:
return do_empty(db, selector)
count = publications.id.count()
rows = setrows.select(submitted_year,
count,
groupby=submitted_year,
orderby=submitted_year)
data = [[row[submitted_year], row[count]] for row in rows]
df = DataFrame(data, columns=['year', 'count'])
df = df.set_index('year')
ax = df.plot(colormap="Pastel1", kind='bar', legend=False, ax=ax)
do_tick(ax)
return ax
def do_empty(db, selector, ax=None):
def do_empty(db, selector, target=None):
"""Empty graph as a function of month.
Args:
db (gluon.DAL): database connection
selector (plugin_dbui.Selector): selector with user critera
ax (matplotlib.AxesSubplot): useful when working with subplot.
target (matplotlib.AxesSubplot): useful when working with subplot.
Returns:
matplotlib.AxesSubplot: axes subplot
......@@ -91,7 +50,7 @@ def do_empty(db, selector, ax=None):
months = to_datetime(li)
df = DataFrame([0]*12*nyear, index=months)
ax = df.plot(legend=False, x_compat=True, ylim=(0, 100), ax=ax)
ax = df.plot(legend=False, x_compat=True, ylim=(0, 100), ax=target)
do_tick(ax)
return ax
......@@ -126,14 +85,14 @@ def do_legend(ax):
ncol=2)
def do_linechart(publications, selector, ax=None):
def do_linechart(publications, selector, target=None):
"""Build a line chart showing the number of publications as a function
of the time either month or year.
Args:
publications (gluon.dal.Table): the publications table.
selector (plugin_dbui.Selector): the selector with user criteria.
ax (matplotlib.AxesSubplot): an existing axes,
target (matplotlib.AxesSubplot): an existing axes,
useful when working with subplot.
Returns:
......@@ -201,7 +160,17 @@ def do_linechart(publications, selector, ax=None):
# instantiate the plot
# x_compat is required to have a nice x axis labelling in all cases
ax = df.plot(legend=False, x_compat=True, ax=ax, ylim=(0, None))
# target = axn when the target is specified
kwargs = dict(ax=target, legend=False, x_compat=True)
# NOTE
# 151103 pandas 0.15.2 / matplotlib 1.3.1
# Curves are not displayed when running two subplots with sahrey=True
# and when the ylim keyword argument is defined.
if target is None:
kwargs['ylim'] = (0, None)
ax = df.plot(**kwargs)
do_tick(ax)
return ax
......@@ -238,7 +207,7 @@ def do_query(publications, selector):
return query
def do_stackedchart(publications, selector, graph, ax=None):
def do_stackedchart(publications, selector, graph, target=None):
"""Build a stacked chart showing the number of publications as a function
of the time.
......@@ -336,7 +305,7 @@ def do_stackedchart(publications, selector, graph, ax=None):
ax = df.plot(colormap="Pastel1",
stacked=True,
ylim=(0, None),
ax=ax,
ax=target,
**kwargs)
do_tick(ax)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment