Commit 7f0ae64a authored by LE GAC Renaud's avatar LE GAC Renaud
Browse files

Consolidate grid and graph.

parent a6519369
...@@ -3,7 +3,6 @@ ...@@ -3,7 +3,6 @@
""" """
import json import json
import traceback import traceback
import urllib.parse
from datetime import datetime from datetime import datetime
...@@ -71,8 +70,6 @@ def metric2d(): ...@@ -71,8 +70,6 @@ def metric2d():
if graph.ax is not None: if graph.ax is not None:
img = graph.to_svg() img = graph.to_svg()
img = urllib.parse.quote(img)
response.view = "plugin_event/grid_and_graph.html" response.view = "plugin_event/grid_and_graph.html"
return dict(cfg_store=store, grid=grid, img=img, title=title) return dict(cfg_store=store, grid=grid, img=img, title=title)
...@@ -83,12 +80,12 @@ def metric2d(): ...@@ -83,12 +80,12 @@ def metric2d():
elif extension == "pdf": elif extension == "pdf":
data = graph.to_pdf() data = graph.to_pdf()
response.view = "plugin_event/graph.pdf" response.view = "plugin_event/graph.pdf"
return dict(data=data, title=title) return dict(data=data)
elif extension == "png": elif extension == "png":
data = graph.to_png() data = graph.to_png()
response.view = "plugin_event/graph.png" response.view = "plugin_event/graph.png"
return dict(data=data, title=title) return dict(data=data)
except (IndexError, ReportException, TypeError, ValueError): except (IndexError, ReportException, TypeError, ValueError):
return CODE(traceback.format_exc()).xml() return CODE(traceback.format_exc()).xml()
......
"""report_tools module """report_tools module
""" """
import base64
import io
import json import json
import matplotlib as mpl import matplotlib as mpl
import pandas as pd import pandas as pd
...@@ -10,8 +12,8 @@ from .dataframes import to_extjs_gridcolumns ...@@ -10,8 +12,8 @@ from .dataframes import to_extjs_gridcolumns
from .event import Event from .event import Event
from gluon import current from gluon import current
from gluon.storage import Storage from gluon.storage import Storage
from io import StringIO
from .model_report import AGGREGATE_FUNCS from .model_report import AGGREGATE_FUNCS
from matplotlib.figure import Figure
from .matplotlib_tools import ticks_and_labels from .matplotlib_tools import ticks_and_labels
from plugin_dbui import Selector, Store from plugin_dbui import Selector, Store
...@@ -235,16 +237,36 @@ class Graph(object): ...@@ -235,16 +237,36 @@ class Graph(object):
else: else:
config_graph["ylim"] = (int(el) for el in ylim.split(",")) config_graph["ylim"] = (int(el) for el in ylim.split(","))
# setup the matplotlib back-end # setup the matplotlib style
mpl.use(backend)
mpl.rcParams['legend.fontsize'] = 7 mpl.rcParams['legend.fontsize'] = 7
# setup matplotlib style mpl.rcParams["grid.alpha"] = 0.5
mpl.rcParams["grid.linestyle"] = "dotted"
mpl.rcParams["xtick.direction"] = "in"
mpl.rcParams["xtick.top"] = True
mpl.rcParams["xtick.bottom"] = True
mpl.rcParams["xtick.major.size"] = 8
mpl.rcParams["xtick.minor.size"] = 4
mpl.rcParams["xtick.labelsize"] = 8
mpl.rcParams["ytick.direction"] = "in"
mpl.rcParams["ytick.left"] = True
mpl.rcParams["ytick.right"] = True
mpl.rcParams["ytick.major.size"] = 8
mpl.rcParams["ytick.minor.size"] = 4
mpl.rcParams["ytick.labelsize"] = 8
mpl.rcParams['xtick.major.size'] = 8 mpl.rcParams['xtick.major.size'] = 8
mpl.rcParams['xtick.minor.size'] = 4 mpl.rcParams['xtick.minor.size'] = 4
mpl.rcParams['ytick.major.size'] = 8 mpl.rcParams['ytick.major.size'] = 8
mpl.rcParams['ytick.minor.size'] = 4 mpl.rcParams['ytick.minor.size'] = 4
# create the figure and subplots
self.fig = fig = Figure()
self.ax = ax = fig.subplots(nrows=1, ncols=1)
config_graph["ax"] = ax
# select the tool to render the plot # select the tool to render the plot
if kind in ("bar", "barh"): if kind in ("bar", "barh"):
func = Graph.bar_like func = Graph.bar_like
...@@ -253,11 +275,11 @@ class Graph(object): ...@@ -253,11 +275,11 @@ class Graph(object):
func = Graph.line_like func = Graph.line_like
# produce the plot without summary rows or columns # produce the plot without summary rows or columns
self.ax = func(metric2d.df0, func(metric2d.df0,
group_field_x, group_field_x,
group_field_y, group_field_y,
metric_field_z, metric_field_z,
config_graph) config_graph)
@staticmethod @staticmethod
def bar_like(df, def bar_like(df,
...@@ -369,19 +391,16 @@ class Graph(object): ...@@ -369,19 +391,16 @@ class Graph(object):
Args: Args:
fmt (str): possible values are pdf, png and svg. fmt (str): possible values are pdf, png and svg.
Returns:
bytes string
""" """
if self.ax is None: if self.ax is None:
return "" return ""
fig = self.ax.get_figure() buf = io.BytesIO()
self.fig.savefig(buf, format=fmt)
fi = StringIO() data = base64.b64encode(buf.getbuffer()).decode("ascii")
fig.savefig(fi, format=fmt)
data = fi.getvalue()
fi.close()
fig.clear()
mpl.pyplot.close(fig)
return data return data
...@@ -593,11 +612,14 @@ class Metric2D(BaseReport): ...@@ -593,11 +612,14 @@ class Metric2D(BaseReport):
df.index = df.index.get_level_values(1) df.index = df.index.get_level_values(1)
# add summaries for rows and columns # add summaries for rows and columns
df = self._add_summaries(df)
# move the index to column in order to ease the data extraction # move the index to column in order to ease the data extraction
df = (self._add_summaries(df)
.reset_index()
.rename(columns={"index": field_y}))
# update the attribute df for a later use # update the attribute df for a later use
self.df = df = df.reset_index() self.df = df
if self.is_summary: if self.is_summary:
self.df0 = self.df0.reset_index() self.df0 = self.df0.reset_index()
......
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
#-------------------------------------------------------------------------- #--------------------------------------------------------------------------
import json import json
import plugin_dbui import plugin_dbui
import urllib import urllib.parse
from datetime import datetime from datetime import datetime
...@@ -35,7 +35,7 @@ ...@@ -35,7 +35,7 @@
# it should contains the current "vars" use to generate the image # it should contains the current "vars" use to generate the image
myapp = request.application myapp = request.application
myvars = urllib.urlencode(request.vars) myvars = urllib.parse.urlencode(request.vars)
url_pdf = "/%s/plugin_event/metric2d.pdf?%s" % (myapp, myvars) url_pdf = "/%s/plugin_event/metric2d.pdf?%s" % (myapp, myvars)
url_png = "/%s/plugin_event/metric2d.png?%s" % (myapp, myvars) url_png = "/%s/plugin_event/metric2d.png?%s" % (myapp, myvars)
...@@ -106,7 +106,7 @@ ...@@ -106,7 +106,7 @@
// //
cfgPanelImg = { cfgPanelImg = {
border: false, border: false,
html: '<img src="data:image/svg+xml;utf8,{{=img}}" height=400></img>', html: '<img src="data:image/svg+xml;base64,{{=img}}" height=400></img>',
itemId: "graphPanel", itemId: "graphPanel",
padding: "0 0 0 10" padding: "0 0 0 10"
}; };
......
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