Commit 27ac952d authored by LE GAC Renaud's avatar LE GAC Renaud
Browse files

Add the function do_barchart and redesing the dashboard.

parent 709f622c
......@@ -4,17 +4,24 @@
"""
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
from datetime import datetime
from gluon.storage import Storage
from graph_tools import (do_linechart,
from graph_tools import (do_barchart,
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
......@@ -36,21 +43,21 @@ def dashboard():
fields = ('cumulative','id_graphs', '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)
# the cumulative sum of publications for the current year
ax = do_linechart(db.publications, selector)
do_linechart(db.publications, selector, ax=axes[0])
do_labels(axes[0], "", T(TITLE_Y))
# # histogram of the number of publications per year
# submitted = db.publications.submitted
# data = [int(el.submitted[:4]) for el in db(submitted.len()>=4).select(submitted)]
# df = DataFrame(data)
# ax = df.plot(kind='hist',
# xlim=[1999.5, 2015.5])
#
# # cumulative histogram number of publication per year
# histogram of the number of publications per year
do_barchart(db.publications, selector, ax=axes[1])
do_labels(axes[1], "", "")
# delegate the rendering to the view
response.view = "graphs/index.html"
return dict(data=savefig(ax.get_figure(), "svg"))
return dict(data=savefig(fig, "svg"))
def index():
......@@ -72,6 +79,9 @@ def index():
else:
ax = do_linechart(db.publications, selector)
do_labels(ax, "", T(TITLE_Y))
do_title(ax, db, selector)
# delegate the rendering to the view
fmt = "svg" if selector.format == "html" else selector.format
return dict(data=savefig(ax.get_figure(), fmt))
......@@ -6,6 +6,7 @@ import matplotlib
import re
from datetime import datetime
from gluon import current
from pandas import DataFrame, DatetimeIndex, to_datetime
from reporting_tools import repr_team_project
......@@ -16,21 +17,61 @@ from StringIO import StringIO
FROM_TO = "from %s to %s"
TITLE = "Publications"
TITLE_CUMSUM = "Cumulative sum of publications"
TITLE_Y = "Number of publications"
def do_empty(db, selector):
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):
"""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.
Returns:
ax (matplotlib.AxesSubplot): axes subplot
matplotlib.AxesSubplot: axes subplot
"""
T = current.T
year_start = selector.year_start
year_end = selector.year_end
......@@ -50,11 +91,8 @@ def do_empty(db, selector):
months = to_datetime(li)
df = DataFrame([0]*12*nyear, index=months)
ax = df.plot(legend=False, x_compat=True, ylim=(0, 100))
do_labels(ax, '', T(TITLE_Y))
ax = df.plot(legend=False, x_compat=True, ylim=(0, 100), ax=ax)
do_tick(ax)
do_title(ax, db, selector)
return ax
......@@ -88,13 +126,14 @@ def do_legend(ax):
ncol=2)
def do_linechart(publications, selector):
def do_linechart(publications, selector, ax=None):
"""Build a line chart showing the number of publications as a function
of the time.
Args:
publications (gluon.dal.Table): the publications table.
selector (plugin_dbui.Selector): the selector with user criteria.
ax (matplotlib.AxesSubplot): useful when working with subplot.
Returns:
matplotlib.AxesSubplot: axes subplot
......@@ -102,7 +141,6 @@ def do_linechart(publications, selector):
"""
db = publications._db
rx_submitted = re.compile(REG_SUBMITTED)
T = current.T
# query directive to count publications including
# foreign key constraints and user requirements
......@@ -145,11 +183,8 @@ def do_linechart(publications, selector):
# 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)
do_labels(ax, "", T(TITLE_Y))
ax = df.plot(legend=False, x_compat=True, ax=ax)
do_tick(ax)
do_title(ax, db, selector)
return ax
......@@ -185,7 +220,7 @@ def do_query(publications, selector):
return query
def do_stackedchart(publications, selector, graph):
def do_stackedchart(publications, selector, graph, ax=None):
"""Build a stacked chart showing the number of publications as a function
of the time.
......@@ -193,6 +228,7 @@ def do_stackedchart(publications, selector, graph):
publications (gluon.dal.Table): the publications table.
selector (plugin_dbui.Selector): the selector with user criteria.
graph (gluon.dal.Row): the chart configuration.
ax (matplotlib.AxesSubplot): useful when working with subplot.
Returns:
matplotlib.AxesSubplot: axes subplot
......@@ -200,7 +236,6 @@ def do_stackedchart(publications, selector, graph):
"""
db = publications._db
rx_submitted = re.compile(REG_SUBMITTED)
T = current.T
# the stack field
tablename = graph.stack_axis
......@@ -265,11 +300,10 @@ def do_stackedchart(publications, selector, graph):
ax = df.plot(colormap="Pastel1",
kind="area",
stacked=True,
x_compat=True)
x_compat=True,
ax=ax)
do_labels(ax, "", T(TITLE_Y))
do_tick(ax)
do_title(ax, db, selector)
return 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