Commit 85c9e03d authored by LE GAC Renaud's avatar LE GAC Renaud
Browse files

The linechart handles the times axis.

parent e2037aa2
......@@ -40,7 +40,7 @@ def dashboard():
request.vars.update(cfg)
fields = ('cumulative','id_graphs', 'year_start','year_end')
fields = ('cumulative','id_graphs', 'time', 'year_start','year_end')
selector = Selector(virtdb.graph_selector, exclude_fields=fields)
# figure layout
......@@ -65,7 +65,7 @@ def index():
either as a linechart or as a stacked histograms.
"""
fields = ('cumulative','id_graphs', 'year_start','year_end')
fields = ('cumulative','id_graphs', 'time', 'year_start','year_end')
selector = Selector(virtdb.graph_selector, exclude_fields=fields)
# graph configuration
......
......@@ -128,18 +128,21 @@ def do_legend(ax):
def do_linechart(publications, selector, ax=None):
"""Build a line chart showing the number of publications as a function
of the time.
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): useful when working with subplot.
ax (matplotlib.AxesSubplot): an existing axes,
useful when working with subplot.
Returns:
matplotlib.AxesSubplot: axes subplot
"""
db = publications._db
is_cumu = selector.cumulative
is_month = selector.time == current.T("month")
rx_submitted = re.compile(REG_SUBMITTED)
# query directive to count publications including
......@@ -148,8 +151,12 @@ def do_linechart(publications, selector, ax=None):
query = do_query(publications, selector)
# count the number of publications
# they are grouped per year and and per month
group_by = [publications.submitted[:4], publications.submitted[5:7]]
# they are grouped per year or per year and month
# depending on the time axis
if is_month:
group_by = [publications.submitted[:4], publications.submitted[5:7]]
else:
group_by = publications.submitted[:4]
setrows = db(query)
if setrows.count() == 0:
......@@ -161,29 +168,40 @@ def do_linechart(publications, selector, ax=None):
groupby=group_by,
orderby=group_by)
# build the dataframe
# the index of the DataSerie is the time (month, ...)
data = []
# build the list of data points
data, y = [], 0.
for row in rows:
submitted = row.publications.submitted
if not rx_submitted.match(submitted):
continue
data.append([submitted[:7], row[count]])
if is_cumu:
y += row[count]
else:
y = row[count]
# a continous line when month axis is selected
if is_month:
data.append([submitted[:7], y])
# a step line for the year axis
else:
year = int(submitted[:4])
data.append(["%i-06" % (year-1), y])
data.append(["%i-06" % year, y])
# instantiate the dataframe
# the index is the time
df = DataFrame(data, columns=['time', 'count'])
df = df.set_index('time')
# cumulative serie
if selector.cumulative:
df = df.cumsum()
# convert the index into a Datetime index
df.index = DatetimeIndex(df.index)
# 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)
ax = df.plot(legend=False, x_compat=True, ax=ax, ylim=(0, None))
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