Commit b6f7a0b8 authored by LE GAC Renaud's avatar LE GAC Renaud
Browse files

The stackedchart handles the times axis.

parent 85c9e03d
......@@ -246,13 +246,15 @@ def do_stackedchart(publications, selector, graph, ax=None):
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.
ax (matplotlib.AxesSubplot): an existing axe,
useful when working with subplot.
Returns:
matplotlib.AxesSubplot: axes subplot
"""
db = publications._db
is_month = selector.time == current.T("month")
rx_submitted = re.compile(REG_SUBMITTED)
# the stack field
......@@ -266,10 +268,15 @@ def do_stackedchart(publications, selector, graph, ax=None):
query = do_query(publications, selector)
# count the number of publications
# they are grouped per year, per month and per stacked value
group_by = [publications.submitted[:4],
publications.submitted[5:7],
stackedfield]
# they are grouped per year and per stacked value
# or per year, month and per stacked value depending on the time axis
if is_month:
group_by = [publications.submitted[:4],
publications.submitted[5:7],
stackedfield]
else:
group_by = [publications.submitted[:4],
stackedfield]
setrows = db(query)
if setrows.count() == 0:
......@@ -282,16 +289,21 @@ def do_stackedchart(publications, selector, graph, ax=None):
groupby=group_by,
orderby=group_by)
# build the dataframe
# build the data points
data = []
for row in rows:
submitted = row.publications.submitted
if not rx_submitted.match(submitted):
continue
li = [submitted[:7], row[tablename][fieldname], row[count]]
data.append(li)
if is_month:
data.append([submitted[:7], row[tablename][fieldname], row[count]])
else:
data.append([submitted[:4], row[tablename][fieldname], row[count]])
# build the dataframe
df = DataFrame(data, columns=['time', fieldname, 'count'])
# move to a DataSerie in which values are ordered by stacked value
......@@ -311,18 +323,23 @@ def do_stackedchart(publications, selector, graph, ax=None):
df = df.cumsum()
# convert the index into a Datetime index
df.index = DatetimeIndex(df.index)
if is_month:
df.index = DatetimeIndex(df.index)
# instantiate the plot
# x_compat is required to have a nice x axis labelling in all cases
if is_month:
kwargs = dict(kind='area', x_compat=True)
else:
kwargs = dict(kind='bar', width=1., linewidth=0)
ax = df.plot(colormap="Pastel1",
kind="area",
stacked=True,
x_compat=True,
ax=ax)
ylim=(0, None),
ax=ax,
**kwargs)
do_tick(ax)
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