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

Consolidate grid and graph.

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