Commit 634af59e authored by LE GAC Renaud's avatar LE GAC Renaud
Browse files

Refactor graphs table and controller.

parent c7f46f28
......@@ -7,7 +7,7 @@ import matplotlib
import re
from plugin_dbui import INLINE_ALERT
from plugin_dbui import get_id, INLINE_ALERT
from report_objects import do_title, List, Metric1D, Metric2D
from selector import MySelector
from StringIO import StringIO
......@@ -48,17 +48,8 @@ def graph_mpl():
"""Plot list or metric using the matplotlib library
"""
ui_table = virtdb.graph_selector
selector = MySelector(ui_table, exclude_fields=('metric',))
# homomorphism between metric2D and graph
config = db.graphs[request.vars.id_graphs]
config.group_field_y = config.field_horizontal
config.group_field_x = config.field_stacked
config.metric_field_z = 'people.id'
config.aggregation_z = 'size'
# set the matplotlib back end
#
# NOTE: the X11 back end is not needed on the server side. In addition
# Tkinter crash with the message "out of stack space" when the 2nd plot
# is generated.
......@@ -67,17 +58,66 @@ def graph_mpl():
# design to render PDF and SVG image without the X11 interface.
#
matplotlib.use('Agg')
# instantiate the selector
ui_table = virtdb.graph_selector
selector = MySelector(ui_table)
# get the lists/metrics configuration
graph = db.graphs[request.vars.id_graphs]
tablename = graph.report_type
id = get_id(db[tablename], name=graph.report_name)
if not id:
msg = "No %s named %s" % (tablename, graph.report_name)
return INLINE_ALERT % ("Error", msg)
# instantiate the lists/metrics
config = db[tablename][id]
if tablename == "lists":
report = List(config, selector)
elif tablename == "metrics1d":
report = Metric1D(config, selector)
elif tablename == "metrics2d":
report = Metric2D(config, selector)
# decode the graph configuration
try:
kwargs = json.loads(graph.plot)
except ValueError, msg:
return INLINE_ALERT % ("JSON Error", msg)
# generate the plot according to user specification
report = Metric2D(config, selector)
df = report.to_df()
plot = df.plot(kind='bar', stacked=True)
if 'index' in kwargs:
index = kwargs['index']
del kwargs['index']
ax = df[index].plot(**kwargs)
else:
ax = df.T.plot(**kwargs)
# activate minor ticks
ax.minorticks_on()
# push legend on the top outside the plot
# from http://stackoverflow.com/questions/4700614/how-to-put-the-legend-out-of-the-plot
if ax.get_legend():
box = ax.get_position()
ax.set_position([box.x0, box.y0, box.width, box.height * 0.9])
ax.legend(loc='lower right',
bbox_to_anchor=(1.01, 1.),
fontsize=10,
ncol=3)
# open a StingIO file
fi = StringIO()
# get the figure associated to the plot and save it in the file
fig = plot.get_figure()
fig = ax.get_figure()
fig.savefig(fi, format='svg')
fig.clear()
......
......@@ -262,6 +262,7 @@
'Phd defense date': 'Date de la soutenance de la thèse',
'please input your password again': 'please input your password again',
'Please select a metric!': 'Sélectionnez une metrique !',
'Plot': 'Plot',
'plugin not install': 'greffon pas installé',
'Profile': 'Profile',
'Project': 'Projet',
......@@ -279,6 +280,8 @@
'Registration identifier': 'Registration identifier',
'Registration key': 'Registration key',
'Registration successful': 'Registration successful',
'Report Name': 'Report Name',
'Report Type': 'Report Type',
'Reports': 'Rapports',
'Reports (new)': 'Rapports (new)',
'Reports (old)': 'Rapports (old)',
......
......@@ -2,26 +2,23 @@
""" graphs
"""
tp_conditions = \
T("Can be applied on any field of the history table using the SQL WHERE syntax. "
"Be aware that foreign key are not resolved "
"(more information in the smart_query in the web2py documentation). "
"In addition individual property of the user block are not available, "
"but operator like contains can be applied on the history.data field.")
tp_fields = \
T("Any field of the history table including foreign table. "
"The field is encoded as tablename.fieldname. "
"For properties in the the user data block, use history.data.myproperty.")
def_plot = \
"""{
"kind": "?",
"stacked": false
}"""
if MIGRATE:
def_plot = None
db.define_table("graphs",
Field("name", "string", length=255, notnull=True, unique=True),
Field("title", "string", length=255),
Field("conditions", "text", comment=tp_conditions),
Field("field_horizontal", "string", length=255, notnull=True, comment=tp_fields),
Field("field_stacked", "string", length=255, notnull=True, comment=tp_fields),
Field("metric_vertical", "string", notnull=True, default='sum_fte', length=255),
Field("report_type", "string", default='lists',length=255, notnull=True),
Field("report_name", "string", length=255, notnull=True),
Field("plot", "text", notnull=True, default=def_plot),
Field("definition", "text"),
migrate="graphs.table")
db.graphs.metric_vertical.requires = IS_IN_SET(('count', 'sum_fte'))
\ No newline at end of file
db.graphs.report_type.requires = IS_IN_SET(('lists', 'metrics1d', 'metrics2d'))
\ No newline at end of file
......@@ -2,33 +2,36 @@
""" graphs
"""
table = 'graphs'
#-------------------------------------------------------------------------------
#
# FIELDS CONFIGURATiON
#
#-------------------------------------------------------------------------------
fieldsModifier = dbui.FieldsModifier('graphs')
fieldsModifier = dbui.FieldsModifier(table)
fieldsModifier.configure_field('conditions', height=110)
fieldsModifier.configure_field('definition', height=110)
fieldsModifier.configure_field('plot',
height=240,
hideLabel=True)
#-------------------------------------------------------------------------------
#
# FORM CONFIGURATiON
#
#-------------------------------------------------------------------------------
formModifier = dbui.FormModifier('graphs')
formModifier = dbui.FormModifier(table)
formModifier.merge_fields('name',
'title',
'report_type',
'report_name',
'definition',
'conditions',
title=T('General'))
formModifier.merge_fields('field_horizontal',
'field_stacked',
'metric_vertical',
title=T('Count by'))
formModifier.merge_fields('plot',
title=T('Configure'))
formModifier.set_mapper(dbui.map_tabpanel)
......@@ -40,12 +43,11 @@ formModifier.configure(fieldDefaults={"labelWidth": 135},
# GRID CONFIGURATiON
#
#-------------------------------------------------------------------------------
gridModifier = dbui.GridModifier('graphs')
gridModifier = dbui.GridModifier(table)
gridModifier.configure_column('name', width=30)
gridModifier.hide_columns('conditions',
'field_horizontal',
'field_stacked',
'metric_vertical',
gridModifier.configure_column('report_type', width=20)
gridModifier.hide_columns('plot',
'report_name',
'title')
#-------------------------------------------------------------------------------
......@@ -53,5 +55,5 @@ gridModifier.hide_columns('conditions',
# STORE CONFIGURATiON
#
#-------------------------------------------------------------------------------
storeModifier = dbui.StoreModifier('graphs')
storeModifier = dbui.StoreModifier(table)
storeModifier.orderby(db.graphs.name)
\ No newline at end of file
......@@ -8,8 +8,7 @@ virtdb.define_table('graph_selector',
Field('id_teams', 'reference teams', label=T('Team')),
Field('category', 'string'),
Field('id_people_categories', 'reference people_categories', label= T("Quality")),
Field('id_projects', 'reference projects', label=T('Project')),
Field('metric', 'string'))
Field('id_projects', 'reference projects', label=T('Project')))
virtdb.graph_selector.category.requires = IS_IN_SET(PEOPLE_CATEGORIES)
virtdb.graph_selector.id_people_categories.requires = IS_IN_DB(db, 'people_categories.code')
......@@ -17,8 +16,6 @@ virtdb.graph_selector.id_people_categories.requires = IS_IN_DB(db, 'people_categ
virtdb.graph_selector.id_projects.requires = IS_IN_DB(db, 'projects.project')
virtdb.graph_selector.id_teams.requires = IS_IN_DB(db, 'teams.team')
virtdb.graph_selector.metric.requires = IS_IN_SET(('count', 'sum_fte'))
#-------------------------------------------------------------------------------
#
# FIELDS CONFIGURATiON
......
static/docs/database.png

98.9 KB | W: | H:

static/docs/database.png

94 KB | W: | H:

static/docs/database.png
static/docs/database.png
static/docs/database.png
static/docs/database.png
  • 2-up
  • Swipe
  • Onion skin
......@@ -22,4 +22,4 @@
<type label="Upload" length="0" sql="upload" quote="'"/>
<type label="Password" length="0" sql="password" quote="'"/>
</group>
</datatypes><table x="303" y="21" name="people"><row name="id" null="0" autoincrement="1"><datatype>integer</datatype></row><row name="first_name" null="0" autoincrement="0"><datatype>string</datatype></row><row name="last_name" null="0" autoincrement="0"><datatype>string</datatype></row><row name="initials" null="0" autoincrement="0"><datatype>string</datatype></row><row name="birth_date" null="1" autoincrement="0"><datatype>date</datatype></row><row name="note" null="1" autoincrement="0"><datatype>text</datatype></row><key type="PRIMARY" name=""><part>id</part></key></table><table x="302" y="174" name="teams"><row name="id" null="0" autoincrement="1"><datatype>integer</datatype></row><row name="team" null="0" autoincrement="0"><datatype>string</datatype></row><row name="domain" null="1" autoincrement="0"><datatype>string</datatype></row><key type="PRIMARY" name=""><part>id</part></key></table><table x="303" y="285" name="projects"><row name="id" null="0" autoincrement="1"><datatype>integer</datatype></row><row name="project" null="0" autoincrement="0"><datatype>string</datatype></row><row name="agencies" null="1" autoincrement="0"><datatype>string</datatype></row><key type="PRIMARY" name=""><part>id</part></key></table><table x="618" y="19" name="people_categories"><row name="id" null="0" autoincrement="1"><datatype>integer</datatype></row><row name="code" null="0" autoincrement="0"><datatype>string</datatype></row><row name="category" null="0" autoincrement="0"><datatype>string</datatype></row><row name="definition" null="1" autoincrement="0"><datatype>string</datatype></row><key type="PRIMARY" name=""><part>id</part></key></table><table x="452" y="71" name="history"><row name="id" null="0" autoincrement="1"><datatype>integer</datatype></row><row name="id_people" null="0" autoincrement="0"><datatype>integer</datatype><relation table="people" row="id" /></row><row name="id_teams" null="0" autoincrement="0"><datatype>integer</datatype><relation table="teams" row="id" /></row><row name="id_projects" null="0" autoincrement="0"><datatype>integer</datatype><relation table="projects" row="id" /></row><row name="id_people_categories" null="0" autoincrement="0"><datatype>integer</datatype><relation table="people_categories" row="id" /></row><row name="id_fundings" null="0" autoincrement="0"><datatype>integer</datatype><relation table="fundings" row="id" /></row><row name="id_events" null="0" autoincrement="0"><datatype>integer</datatype><relation table="events" row="id" /></row><row name="start_date" null="0" autoincrement="0"><datatype>date</datatype></row><row name="end_date" null="1" autoincrement="0"><datatype>date</datatype></row><row name="percentage" null="1" autoincrement="0"><datatype>integer</datatype></row><row name="note" null="0" autoincrement="0"><datatype>text</datatype></row><row name="data" null="1" autoincrement="0"><datatype>string</datatype></row><key type="PRIMARY" name=""><part>id</part></key></table><table x="633" y="149" name="fundings"><row name="id" null="0" autoincrement="1"><datatype>integer</datatype></row><row name="agency" null="0" autoincrement="0"><datatype>string</datatype></row><row name="definition" null="1" autoincrement="0"><datatype>text</datatype></row><key type="PRIMARY" name=""><part>id</part></key></table><table x="638" y="266" name="events"><row name="id" null="0" autoincrement="1"><datatype>integer</datatype></row><row name="event" null="0" autoincrement="0"><datatype>string</datatype></row><row name="definitiion" null="1" autoincrement="0"><datatype>text</datatype></row><row name="data" null="1" autoincrement="0"><datatype>text</datatype></row><key type="PRIMARY" name=""><part>id</part></key></table><table x="806" y="19" name="lists"><row name="id" null="0" autoincrement="1"><datatype>integer</datatype></row><row name="name" null="0" autoincrement="0"><datatype>string</datatype></row><row name="title" null="1" autoincrement="0"><datatype>string</datatype></row><row name="conditions" null="1" autoincrement="0"><datatype>text</datatype></row><row name="group_field" null="1" autoincrement="0"><datatype>string</datatype></row><row name="sorters" null="1" autoincrement="0"><datatype>integer</datatype></row><row name="columns" null="0" autoincrement="0"><datatype>text</datatype></row><row name="features" null="1" autoincrement="0"><datatype>text</datatype></row><row name="definition" null="1" autoincrement="0"><datatype>text</datatype></row><key type="PRIMARY" name=""><part>id</part></key></table><table x="903" y="18" name="metrics2d"><row name="id" null="0" autoincrement="1"><datatype>integer</datatype></row><row name="name" null="0" autoincrement="0"><datatype>string</datatype></row><row name="title" null="1" autoincrement="0"><datatype>string</datatype></row><row name="conditions" null="1" autoincrement="0"><datatype>text</datatype></row><row name="group_field_x" null="0" autoincrement="0"><datatype>string</datatype></row><row name="group_field_y" null="0" autoincrement="0"><datatype>string</datatype></row><row name="metric_field_z" null="0" autoincrement="0"><datatype>string</datatype></row><row name="aggregate_z" null="0" autoincrement="0"><datatype>string</datatype></row><row name="definition" null="1" autoincrement="0"><datatype>text</datatype></row><key type="PRIMARY" name=""><part>id</part></key></table><table x="909" y="224" name="graphs"><row name="id" null="0" autoincrement="1"><datatype>integer</datatype></row><row name="name" null="0" autoincrement="0"><datatype>string</datatype></row><row name="title" null="1" autoincrement="0"><datatype>string</datatype></row><row name="conditions" null="1" autoincrement="0"><datatype>text</datatype></row><row name="field_horizontal" null="0" autoincrement="0"><datatype>string</datatype></row><row name="field_stacked" null="1" autoincrement="0"><datatype>string</datatype></row><row name="metric_vertical" null="0" autoincrement="0"><datatype>text</datatype></row><row name="definition" null="1" autoincrement="0"><datatype>text</datatype></row><key type="PRIMARY" name=""><part>id</part></key></table><table x="777" y="225" name="metrics1d"><row name="id" null="0" autoincrement="1"><datatype>integer</datatype></row><row name="name" null="0" autoincrement="0"><datatype>string</datatype></row><row name="title" null="1" autoincrement="0"><datatype>string</datatype></row><row name="conditions" null="1" autoincrement="0"><datatype>text</datatype></row><row name="group_field" null="1" autoincrement="0"><datatype>string</datatype></row><row name="columns" null="0" autoincrement="0"><datatype>text</datatype></row><row name="definition" null="1" autoincrement="0"><datatype>text</datatype></row><key type="PRIMARY" name=""><part>id</part></key></table></sql>
\ No newline at end of file
</datatypes><table x="303" y="21" name="people"><row name="id" null="0" autoincrement="1"><datatype>integer</datatype></row><row name="first_name" null="0" autoincrement="0"><datatype>string</datatype></row><row name="last_name" null="0" autoincrement="0"><datatype>string</datatype></row><row name="initials" null="0" autoincrement="0"><datatype>string</datatype></row><row name="birth_date" null="1" autoincrement="0"><datatype>date</datatype></row><row name="note" null="1" autoincrement="0"><datatype>text</datatype></row><key type="PRIMARY" name=""><part>id</part></key></table><table x="302" y="174" name="teams"><row name="id" null="0" autoincrement="1"><datatype>integer</datatype></row><row name="team" null="0" autoincrement="0"><datatype>string</datatype></row><row name="domain" null="1" autoincrement="0"><datatype>string</datatype></row><key type="PRIMARY" name=""><part>id</part></key></table><table x="303" y="285" name="projects"><row name="id" null="0" autoincrement="1"><datatype>integer</datatype></row><row name="project" null="0" autoincrement="0"><datatype>string</datatype></row><row name="agencies" null="1" autoincrement="0"><datatype>string</datatype></row><key type="PRIMARY" name=""><part>id</part></key></table><table x="618" y="19" name="people_categories"><row name="id" null="0" autoincrement="1"><datatype>integer</datatype></row><row name="code" null="0" autoincrement="0"><datatype>string</datatype></row><row name="category" null="0" autoincrement="0"><datatype>string</datatype></row><row name="definition" null="1" autoincrement="0"><datatype>string</datatype></row><key type="PRIMARY" name=""><part>id</part></key></table><table x="452" y="71" name="history"><row name="id" null="0" autoincrement="1"><datatype>integer</datatype></row><row name="id_people" null="0" autoincrement="0"><datatype>integer</datatype><relation table="people" row="id" /></row><row name="id_teams" null="0" autoincrement="0"><datatype>integer</datatype><relation table="teams" row="id" /></row><row name="id_projects" null="0" autoincrement="0"><datatype>integer</datatype><relation table="projects" row="id" /></row><row name="id_people_categories" null="0" autoincrement="0"><datatype>integer</datatype><relation table="people_categories" row="id" /></row><row name="id_fundings" null="0" autoincrement="0"><datatype>integer</datatype><relation table="fundings" row="id" /></row><row name="id_events" null="0" autoincrement="0"><datatype>integer</datatype><relation table="events" row="id" /></row><row name="start_date" null="0" autoincrement="0"><datatype>date</datatype></row><row name="end_date" null="1" autoincrement="0"><datatype>date</datatype></row><row name="percentage" null="1" autoincrement="0"><datatype>integer</datatype></row><row name="note" null="0" autoincrement="0"><datatype>text</datatype></row><row name="data" null="1" autoincrement="0"><datatype>string</datatype></row><key type="PRIMARY" name=""><part>id</part></key></table><table x="633" y="149" name="fundings"><row name="id" null="0" autoincrement="1"><datatype>integer</datatype></row><row name="agency" null="0" autoincrement="0"><datatype>string</datatype></row><row name="definition" null="1" autoincrement="0"><datatype>text</datatype></row><key type="PRIMARY" name=""><part>id</part></key></table><table x="638" y="266" name="events"><row name="id" null="0" autoincrement="1"><datatype>integer</datatype></row><row name="event" null="0" autoincrement="0"><datatype>string</datatype></row><row name="definitiion" null="1" autoincrement="0"><datatype>text</datatype></row><row name="data" null="1" autoincrement="0"><datatype>text</datatype></row><key type="PRIMARY" name=""><part>id</part></key></table><table x="806" y="19" name="lists"><row name="id" null="0" autoincrement="1"><datatype>integer</datatype></row><row name="name" null="0" autoincrement="0"><datatype>string</datatype></row><row name="title" null="1" autoincrement="0"><datatype>string</datatype></row><row name="conditions" null="1" autoincrement="0"><datatype>text</datatype></row><row name="group_field" null="1" autoincrement="0"><datatype>string</datatype></row><row name="sorters" null="1" autoincrement="0"><datatype>integer</datatype></row><row name="columns" null="0" autoincrement="0"><datatype>text</datatype></row><row name="features" null="1" autoincrement="0"><datatype>text</datatype></row><row name="definition" null="1" autoincrement="0"><datatype>text</datatype></row><key type="PRIMARY" name=""><part>id</part></key></table><table x="903" y="18" name="metrics2d"><row name="id" null="0" autoincrement="1"><datatype>integer</datatype></row><row name="name" null="0" autoincrement="0"><datatype>string</datatype></row><row name="title" null="1" autoincrement="0"><datatype>string</datatype></row><row name="conditions" null="1" autoincrement="0"><datatype>text</datatype></row><row name="group_field_x" null="0" autoincrement="0"><datatype>string</datatype></row><row name="group_field_y" null="0" autoincrement="0"><datatype>string</datatype></row><row name="metric_field_z" null="0" autoincrement="0"><datatype>string</datatype></row><row name="aggregate_z" null="0" autoincrement="0"><datatype>string</datatype></row><row name="definition" null="1" autoincrement="0"><datatype>text</datatype></row><key type="PRIMARY" name=""><part>id</part></key></table><table x="909" y="224" name="graphs"><row name="id" null="0" autoincrement="1"><datatype>integer</datatype></row><row name="name" null="0" autoincrement="0"><datatype>string</datatype></row><row name="title" null="1" autoincrement="0"><datatype>string</datatype></row><row name="report_type" null="0" autoincrement="0"><datatype>string</datatype></row><row name="report_name" null="1" autoincrement="0"><datatype>string</datatype></row><row name="plot" null="0" autoincrement="0"><datatype>text</datatype></row><row name="definition" null="1" autoincrement="0"><datatype>text</datatype></row><key type="PRIMARY" name=""><part>id</part></key></table><table x="777" y="225" name="metrics1d"><row name="id" null="0" autoincrement="1"><datatype>integer</datatype></row><row name="name" null="0" autoincrement="0"><datatype>string</datatype></row><row name="title" null="1" autoincrement="0"><datatype>string</datatype></row><row name="conditions" null="1" autoincrement="0"><datatype>text</datatype></row><row name="group_field" null="1" autoincrement="0"><datatype>string</datatype></row><row name="columns" null="0" autoincrement="0"><datatype>text</datatype></row><row name="definition" null="1" autoincrement="0"><datatype>text</datatype></row><key type="PRIMARY" name=""><part>id</part></key></table></sql>
\ No newline at end of file
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